1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2007, 2009, 2010 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "data/file-name.h"
26 #include "data/settings.h"
27 #include "libpspp/assertion.h"
28 #include "libpspp/cast.h"
29 #include "libpspp/compiler.h"
30 #include "libpspp/message.h"
31 #include "libpspp/start-date.h"
32 #include "libpspp/string-map.h"
33 #include "libpspp/version.h"
34 #include "output/cairo.h"
35 #include "output/chart-item-provider.h"
36 #include "output/driver-provider.h"
37 #include "output/message-item.h"
38 #include "output/options.h"
39 #include "output/render.h"
40 #include "output/tab.h"
41 #include "output/table-item.h"
42 #include "output/text-item.h"
45 #include "gl/minmax.h"
46 #include "gl/xalloc.h"
49 #define _(msgid) gettext (msgid)
51 /* This file uses TABLE_HORZ and TABLE_VERT enough to warrant abbreviating. */
55 /* Line styles bit shifts. */
67 make_box_index (int left, int right, int top, int bottom)
69 return ((left << LNS_LEFT) | (right << LNS_RIGHT)
70 | (top << LNS_TOP) | (bottom << LNS_BOTTOM));
73 /* Character attributes. */
74 #define ATTR_EMPHASIS 0x100 /* Bold-face. */
75 #define ATTR_BOX 0x200 /* Line drawing character. */
80 unsigned short *chars; /* Characters and attributes. */
81 int n_chars; /* Length. */
82 int allocated_chars; /* Allocated "chars" elements. */
85 /* How to emphasize text. */
88 EMPH_BOLD, /* Overstrike for bold. */
89 EMPH_UNDERLINE, /* Overstrike for underlining. */
90 EMPH_NONE /* No emphasis. */
93 /* ASCII output driver. */
96 struct output_driver driver;
98 /* User parameters. */
99 bool append; /* Append if output file already exists? */
100 bool headers; /* Print headers at top of page? */
101 bool paginate; /* Insert formfeeds? */
102 bool squeeze_blank_lines; /* Squeeze multiple blank lines into one? */
103 enum emphasis_style emphasis; /* How to emphasize text. */
104 char *chart_file_name; /* Name of files used for charts. */
106 int width; /* Page width. */
107 int length; /* Page length minus margins and header. */
108 bool auto_width; /* Use viewwidth as page width? */
109 bool auto_length; /* Use viewlength as page width? */
111 int top_margin; /* Top margin in lines. */
112 int bottom_margin; /* Bottom margin in lines. */
114 char *box[LNS_COUNT]; /* Line & box drawing characters. */
115 char *init; /* Device initialization string. */
117 /* Internal state. */
121 char *file_name; /* Output file name. */
122 FILE *file; /* Output file. */
123 bool error; /* Output error? */
124 int page_number; /* Current page number. */
125 struct ascii_line *lines; /* Page content. */
126 int allocated_lines; /* Number of lines allocated. */
127 int chart_cnt; /* Number of charts so far. */
131 static const struct output_driver_class ascii_driver_class;
133 static void ascii_submit (struct output_driver *, const struct output_item *);
135 static int vertical_margins (const struct ascii_driver *);
137 static const char *get_default_box (int right, int bottom, int left, int top);
138 static bool update_page_size (struct ascii_driver *, bool issue_error);
139 static int parse_page_size (struct driver_option *);
141 static void ascii_close_page (struct ascii_driver *);
142 static bool ascii_open_page (struct ascii_driver *);
144 static void ascii_draw_line (void *, int bb[TABLE_N_AXES][2],
145 enum render_line_style styles[TABLE_N_AXES][2]);
146 static void ascii_measure_cell_width (void *, const struct table_cell *,
148 static int ascii_measure_cell_height (void *, const struct table_cell *,
150 static void ascii_draw_cell (void *, const struct table_cell *,
151 int bb[TABLE_N_AXES][2],
152 int clip[TABLE_N_AXES][2]);
154 static struct ascii_driver *
155 ascii_driver_cast (struct output_driver *driver)
157 assert (driver->class == &ascii_driver_class);
158 return UP_CAST (driver, struct ascii_driver, driver);
161 static struct driver_option *
162 opt (struct output_driver *d, struct string_map *options, const char *key,
163 const char *default_value)
165 return driver_option_get (d, options, key, default_value);
168 static struct output_driver *
169 ascii_create (const char *file_name, enum settings_output_devices device_type,
170 struct string_map *o)
172 struct output_driver *d;
173 struct ascii_driver *a;
175 int right, bottom, left, top;
177 a = xzalloc (sizeof *a);
179 output_driver_init (&a->driver, &ascii_driver_class, file_name, device_type);
180 a->append = parse_boolean (opt (d, o, "append", "false"));
181 a->headers = parse_boolean (opt (d, o, "headers", "false"));
182 a->paginate = parse_boolean (opt (d, o, "paginate", "false"));
183 a->squeeze_blank_lines = parse_boolean (opt (d, o, "squeeze", "true"));
184 a->emphasis = parse_enum (opt (d, o, "emphasis", "none"),
186 "underline", EMPH_UNDERLINE,
190 a->chart_file_name = parse_chart_file_name (opt (d, o, "charts", file_name));
192 a->top_margin = parse_int (opt (d, o, "top-margin", "0"), 0, INT_MAX);
193 a->bottom_margin = parse_int (opt (d, o, "bottom-margin", "0"), 0, INT_MAX);
195 a->width = parse_page_size (opt (d, o, "width", "79"));
196 paper_length = parse_page_size (opt (d, o, "length", "66"));
197 a->auto_width = a->width < 0;
198 a->auto_length = paper_length < 0;
199 a->length = paper_length - vertical_margins (a);
201 for (right = 0; right < 4; right++)
202 for (bottom = 0; bottom < 4; bottom++)
203 for (left = 0; left < 4; left++)
204 for (top = 0; top < 4; top++)
206 int indx = make_box_index (left, right, top, bottom);
207 const char *default_value;
210 sprintf (name, "box[%d%d%d%d]", right, bottom, left, top);
211 default_value = get_default_box (right, bottom, left, top);
212 a->box[indx] = parse_string (opt (d, o, name, default_value));
214 a->init = parse_string (opt (d, o, "init", ""));
216 a->command_name = NULL;
217 a->title = xstrdup ("");
218 a->subtitle = xstrdup ("");
219 a->file_name = xstrdup (file_name);
224 a->allocated_lines = 0;
227 if (!update_page_size (a, true))
233 output_driver_destroy (d);
238 get_default_box (int right, int bottom, int left, int top)
240 switch ((top << 12) | (left << 8) | (bottom << 4) | (right << 0))
245 case 0x0100: case 0x0101: case 0x0001:
248 case 0x1000: case 0x1010: case 0x0010:
251 case 0x0300: case 0x0303: case 0x0003:
252 case 0x0200: case 0x0202: case 0x0002:
256 return left > 1 || top > 1 || right > 1 || bottom > 1 ? "#" : "+";
261 parse_page_size (struct driver_option *option)
263 int dim = atol (option->default_value);
265 if (option->value != NULL)
267 if (!strcmp (option->value, "auto"))
275 value = strtol (option->value, &tail, 0);
276 if (dim >= 1 && errno != ERANGE && *tail == '\0')
279 error (0, 0, _("%s: %s must be positive integer or `auto'"),
280 option->driver_name, option->name);
284 driver_option_destroy (option);
290 vertical_margins (const struct ascii_driver *a)
292 return a->top_margin + a->bottom_margin + (a->headers ? 3 : 0);
295 /* Re-calculates the page width and length based on settings,
296 margins, and, if "auto" is set, the size of the user's
297 terminal window or GUI output window. */
299 update_page_size (struct ascii_driver *a, bool issue_error)
301 enum { MIN_WIDTH = 6, MIN_LENGTH = 6 };
304 a->width = settings_get_viewwidth ();
306 a->length = settings_get_viewlength () - vertical_margins (a);
308 if (a->width < MIN_WIDTH || a->length < MIN_LENGTH)
312 _("ascii: page excluding margins and headers "
313 "must be at least %d characters wide by %d lines long, but "
314 "as configured is only %d characters by %d lines"),
315 MIN_WIDTH, MIN_LENGTH,
316 a->width, a->length);
317 if (a->width < MIN_WIDTH)
318 a->width = MIN_WIDTH;
319 if (a->length < MIN_LENGTH)
320 a->length = MIN_LENGTH;
328 ascii_destroy (struct output_driver *driver)
330 struct ascii_driver *a = ascii_driver_cast (driver);
334 ascii_close_page (a);
337 fn_close (a->file_name, a->file);
338 free (a->command_name);
342 free (a->chart_file_name);
343 for (i = 0; i < LNS_COUNT; i++)
346 for (i = 0; i < a->allocated_lines; i++)
347 free (a->lines[i].chars);
353 ascii_flush (struct output_driver *driver)
355 struct ascii_driver *a = ascii_driver_cast (driver);
358 ascii_close_page (a);
360 if (fn_close (a->file_name, a->file) != 0)
361 error (0, errno, _("ascii: closing output file `%s'"),
368 ascii_init_caption_cell (const char *caption, struct table_cell *cell)
370 cell->contents = caption;
371 cell->options = TAB_LEFT;
372 cell->destructor = NULL;
376 ascii_output_table_item (struct ascii_driver *a,
377 const struct table_item *table_item)
379 const char *caption = table_item_get_caption (table_item);
380 struct render_params params;
381 struct render_page *page;
382 struct render_break x_break;
386 update_page_size (a, false);
390 /* XXX doesn't do well with very large captions */
391 struct table_cell cell;
392 ascii_init_caption_cell (caption, &cell);
393 caption_height = ascii_measure_cell_height (a, &cell, a->width);
398 params.draw_line = ascii_draw_line;
399 params.measure_cell_width = ascii_measure_cell_width;
400 params.measure_cell_height = ascii_measure_cell_height;
401 params.draw_cell = ascii_draw_cell,
403 params.size[H] = a->width;
404 params.size[V] = a->length - caption_height;
405 params.font_size[H] = 1;
406 params.font_size[V] = 1;
407 for (i = 0; i < RENDER_N_LINES; i++)
409 int width = i == RENDER_LINE_NONE ? 0 : 1;
410 params.line_widths[H][i] = width;
411 params.line_widths[V][i] = width;
414 if (a->file == NULL && !ascii_open_page (a))
417 page = render_page_create (¶ms, table_item_get_table (table_item));
418 for (render_break_init (&x_break, page, H);
419 render_break_has_next (&x_break); )
421 struct render_page *x_slice;
422 struct render_break y_break;
424 x_slice = render_break_next (&x_break, a->width);
425 for (render_break_init (&y_break, x_slice, V);
426 render_break_has_next (&y_break); )
428 struct render_page *y_slice;
434 space = a->length - a->y - caption_height;
435 if (render_break_next_size (&y_break) > space)
438 ascii_close_page (a);
439 if (!ascii_open_page (a))
444 y_slice = render_break_next (&y_break, space);
447 struct table_cell cell;
448 int bb[TABLE_N_AXES][2];
450 ascii_init_caption_cell (caption, &cell);
454 bb[V][1] = caption_height;
455 ascii_draw_cell (a, &cell, bb, bb);
456 a->y += caption_height;
459 render_page_draw (y_slice);
460 a->y += render_page_get_size (y_slice, V);
461 render_page_unref (y_slice);
463 render_break_destroy (&y_break);
465 render_break_destroy (&x_break);
469 ascii_output_text (struct ascii_driver *a, const char *text)
471 struct table_item *table_item;
473 table_item = table_item_create (table_from_string (TAB_LEFT, text), NULL);
474 ascii_output_table_item (a, table_item);
475 table_item_unref (table_item);
479 ascii_submit (struct output_driver *driver,
480 const struct output_item *output_item)
482 struct ascii_driver *a = ascii_driver_cast (driver);
484 output_driver_track_current_command (output_item, &a->command_name);
489 if (is_table_item (output_item))
490 ascii_output_table_item (a, to_table_item (output_item));
492 else if (is_chart_item (output_item) && a->chart_file_name != NULL)
494 struct chart_item *chart_item = to_chart_item (output_item);
497 file_name = xr_draw_png_chart (chart_item, a->chart_file_name,
499 if (file_name != NULL)
501 struct text_item *text_item;
503 text_item = text_item_create_format (
504 TEXT_ITEM_PARAGRAPH, _("See %s for a chart."), file_name);
506 ascii_submit (driver, &text_item->output_item);
507 text_item_unref (text_item);
511 #endif /* HAVE_CAIRO */
512 else if (is_text_item (output_item))
514 const struct text_item *text_item = to_text_item (output_item);
515 enum text_item_type type = text_item_get_type (text_item);
516 const char *text = text_item_get_text (text_item);
520 case TEXT_ITEM_TITLE:
522 a->title = xstrdup (text);
525 case TEXT_ITEM_SUBTITLE:
527 a->subtitle = xstrdup (text);
530 case TEXT_ITEM_COMMAND_CLOSE:
533 case TEXT_ITEM_BLANK_LINE:
538 case TEXT_ITEM_EJECT_PAGE:
540 ascii_close_page (a);
544 ascii_output_text (a, text);
548 else if (is_message_item (output_item))
550 const struct message_item *message_item = to_message_item (output_item);
551 const struct msg *msg = message_item_get_msg (message_item);
552 char *s = msg_to_string (msg, a->command_name);
553 ascii_output_text (a, s);
558 const struct output_driver_factory txt_driver_factory =
559 { "txt", ascii_create };
560 const struct output_driver_factory list_driver_factory =
561 { "list", ascii_create };
563 static const struct output_driver_class ascii_driver_class =
578 static void ascii_expand_line (struct ascii_driver *, int y, int length);
579 static void ascii_layout_cell (struct ascii_driver *,
580 const struct table_cell *,
581 int bb[TABLE_N_AXES][2],
582 int clip[TABLE_N_AXES][2], enum wrap_mode wrap,
583 int *width, int *height);
586 ascii_draw_line (void *a_, int bb[TABLE_N_AXES][2],
587 enum render_line_style styles[TABLE_N_AXES][2])
589 struct ascii_driver *a = a_;
590 unsigned short int value;
594 /* Clip to the page. */
595 if (bb[H][0] >= a->width || bb[V][0] + a->y >= a->length)
597 x1 = MIN (bb[H][1], a->width);
598 y1 = MIN (bb[V][1] + a->y, a->length);
601 value = ATTR_BOX | make_box_index (styles[V][0], styles[V][1],
602 styles[H][0], styles[H][1]);
603 for (y = bb[V][0] + a->y; y < y1; y++)
605 ascii_expand_line (a, y, x1);
606 for (x = bb[H][0]; x < x1; x++)
607 a->lines[y].chars[x] = value;
612 ascii_measure_cell_width (void *a_, const struct table_cell *cell,
613 int *min_width, int *max_width)
615 struct ascii_driver *a = a_;
616 int bb[TABLE_N_AXES][2];
617 int clip[TABLE_N_AXES][2];
624 clip[H][0] = clip[H][1] = clip[V][0] = clip[V][1] = 0;
625 ascii_layout_cell (a, cell, bb, clip, WRAP_WORD, max_width, &h);
627 if (strchr (cell->contents, ' '))
630 ascii_layout_cell (a, cell, bb, clip, WRAP_WORD, min_width, &h);
633 *min_width = *max_width;
637 ascii_measure_cell_height (void *a_, const struct table_cell *cell, int width)
639 struct ascii_driver *a = a_;
640 int bb[TABLE_N_AXES][2];
641 int clip[TABLE_N_AXES][2];
648 clip[H][0] = clip[H][1] = clip[V][0] = clip[V][1] = 0;
649 ascii_layout_cell (a, cell, bb, clip, WRAP_WORD, &w, &h);
654 ascii_draw_cell (void *a_, const struct table_cell *cell,
655 int bb[TABLE_N_AXES][2], int clip[TABLE_N_AXES][2])
657 struct ascii_driver *a = a_;
660 ascii_layout_cell (a, cell, bb, clip, WRAP_WORD, &w, &h);
663 /* Ensures that at least the first LENGTH characters of line Y in
664 ascii driver A have been cleared out. */
666 ascii_expand_line (struct ascii_driver *a, int y, int length)
668 struct ascii_line *line = &a->lines[y];
669 if (line->n_chars < length)
672 if (line->allocated_chars < length)
674 line->allocated_chars = MAX (length, MIN (length * 2, a->width));
675 line->chars = xnrealloc (line->chars, line->allocated_chars,
676 sizeof *line->chars);
678 for (x = line->n_chars; x < length; x++)
679 line->chars[x] = ' ';
680 line->n_chars = length;
685 text_draw (struct ascii_driver *a, const struct table_cell *cell,
686 int bb[TABLE_N_AXES][2], int clip[TABLE_N_AXES][2],
687 int y, const char *string, int n)
689 int x0 = MAX (0, clip[H][0]);
690 int y0 = MAX (0, clip[V][0] + a->y);
692 int y1 = MIN (a->length, clip[V][1] + a->y);
696 if (y < y0 || y >= y1)
699 switch (cell->options & TAB_ALIGNMENT)
705 x = (bb[H][0] + bb[H][1] - n + 1) / 2;
727 int attr = cell->options & TAB_EMPH ? ATTR_EMPHASIS : 0;
730 ascii_expand_line (a, y, x + n);
731 for (i = 0; i < n; i++)
732 a->lines[y].chars[x + i] = string[i] | attr;
737 ascii_layout_cell (struct ascii_driver *a, const struct table_cell *cell,
738 int bb[TABLE_N_AXES][2], int clip[TABLE_N_AXES][2],
739 enum wrap_mode wrap, int *width, int *height)
741 size_t length = strlen (cell->contents);
746 for (y = bb[V][0]; y < bb[V][1] && pos < length; y++)
748 const char *line = &cell->contents[pos];
749 const char *new_line;
752 /* Find line length without considering word wrap. */
753 line_len = MIN (bb[H][1] - bb[H][0], length - pos);
754 new_line = memchr (line, '\n', line_len);
755 if (new_line != NULL)
756 line_len = new_line - line;
759 if (pos + line_len < length && wrap != WRAP_CHAR)
761 size_t space_len = line_len;
762 while (space_len > 0 && !isspace ((unsigned char) line[space_len]))
765 line_len = space_len;
766 else if (wrap == WRAP_WORD)
768 while (pos + line_len < length
769 && !isspace ((unsigned char) line[line_len]))
773 if (line_len > *width)
777 text_draw (a, cell, bb, clip, y, line, line_len);
781 if (pos < length && isspace ((unsigned char) cell->contents[pos]))
784 *height = y - bb[V][0];
787 /* ascii_close_page () and support routines. */
790 ascii_open_page (struct ascii_driver *a)
799 a->file = fn_open (a->file_name, a->append ? "a" : "w");
803 fputs (a->init, a->file);
807 error (0, errno, _("ascii: opening output file `%s'"),
816 if (a->length > a->allocated_lines)
818 a->lines = xnrealloc (a->lines, a->length, sizeof *a->lines);
819 for (i = a->allocated_lines; i < a->length; i++)
821 struct ascii_line *line = &a->lines[i];
823 line->allocated_chars = 0;
825 a->allocated_lines = a->length;
828 for (i = 0; i < a->length; i++)
829 a->lines[i].n_chars = 0;
834 /* Writes LINE to A's output file. */
836 output_line (struct ascii_driver *a, const struct ascii_line *line)
841 length = line->n_chars;
842 while (length > 0 && line->chars[length - 1] == ' ')
845 for (i = 0; i < length; i++)
847 int attribute = line->chars[i] & (ATTR_BOX | ATTR_EMPHASIS);
848 int ch = line->chars[i] & ~(ATTR_BOX | ATTR_EMPHASIS);
853 fputs (a->box[ch], a->file);
857 if (a->emphasis == EMPH_BOLD)
858 fprintf (a->file, "%c\b%c", ch, ch);
859 else if (a->emphasis == EMPH_UNDERLINE)
860 fprintf (a->file, "_\b%c", ch);
871 putc ('\n', a->file);
875 output_title_line (FILE *out, int width, const char *left, const char *right)
877 struct string s = DS_EMPTY_INITIALIZER;
878 ds_put_char_multiple (&s, ' ', width);
881 size_t length = MIN (strlen (left), width);
882 memcpy (ds_end (&s) - width, left, length);
886 size_t length = MIN (strlen (right), width);
887 memcpy (ds_end (&s) - length, right, length);
889 ds_put_char (&s, '\n');
890 fputs (ds_cstr (&s), out);
895 ascii_close_page (struct ascii_driver *a)
904 if (!a->top_margin && !a->bottom_margin && a->squeeze_blank_lines
905 && !a->paginate && a->page_number > 1)
906 putc ('\n', a->file);
908 for (i = 0; i < a->top_margin; i++)
909 putc ('\n', a->file);
914 r1 = xasprintf (_("%s - Page %d"), get_start_date (), a->page_number);
915 r2 = xasprintf ("%s - %s" , version, host_system);
917 output_title_line (a->file, a->width, a->title, r1);
918 output_title_line (a->file, a->width, a->subtitle, r2);
919 putc ('\n', a->file);
926 for (y = 0; y < a->allocated_lines; y++)
928 struct ascii_line *line = &a->lines[y];
930 if (a->squeeze_blank_lines && y > 0 && line->n_chars == 0)
936 putc ('\n', a->file);
940 output_line (a, line);
943 if (!a->squeeze_blank_lines)
944 for (y = a->allocated_lines; y < a->length; y++)
945 putc ('\n', a->file);
947 for (i = 0; i < a->bottom_margin; i++)
948 putc ('\n', a->file);
950 putc ('\f', a->file);