1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2009, 2010, 2011, 2013, 2014, 2016 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/>. */
19 #include "output/tab.h"
26 #include "data/data-out.h"
27 #include "data/format.h"
28 #include "data/settings.h"
29 #include "data/value.h"
30 #include "data/variable.h"
31 #include "libpspp/assertion.h"
32 #include "libpspp/compiler.h"
33 #include "libpspp/i18n.h"
34 #include "libpspp/misc.h"
35 #include "libpspp/pool.h"
36 #include "output/driver.h"
37 #include "output/table-item.h"
38 #include "output/table-provider.h"
39 #include "output/text-item.h"
41 #include "gl/minmax.h"
42 #include "gl/xalloc.h"
45 #define _(msgid) gettext (msgid)
48 static const bool debugging = true;
52 #define TAB_JOIN (1u << TAB_FIRST_AVAILABLE)
55 struct tab_joined_cell
57 int d[TABLE_N_AXES][2]; /* Table region, same as struct table_cell. */
61 const struct footnote **footnotes;
63 const struct area_style *style;
66 static const struct table_class tab_table_class;
68 /* Creates and returns a new table with NC columns and NR rows and initially no
69 header rows or columns. The table's cells are initially empty. */
71 tab_create (int nc, int nr)
75 t = pool_create_container (struct tab_table, container);
76 table_init (&t->table, &tab_table_class, nc, nr);
78 t->cc = pool_calloc (t->container, nr * nc, sizeof *t->cc);
79 t->ct = pool_calloc (t->container, nr * nc, sizeof *t->ct);
81 t->rh = pool_nmalloc (t->container, nc, nr + 1);
82 memset (t->rh, TAL_0, nc * (nr + 1));
84 t->rv = pool_nmalloc (t->container, nr, nc + 1);
85 memset (t->rv, TAL_0, nr * (nc + 1));
87 memset (t->styles, 0, sizeof t->styles);
88 memset (t->rule_colors, 0, sizeof t->rule_colors);
94 /* Sets the number of header rows on each side of TABLE to L on the
95 left, R on the right, T on the top, B on the bottom. Header rows
96 are repeated when a table is broken across multiple columns or
99 tab_headers (struct tab_table *table, int l, int r, int t, int b)
101 table_set_hl (&table->table, l);
102 table_set_hr (&table->table, r);
103 table_set_ht (&table->table, t);
104 table_set_hb (&table->table, b);
109 /* Draws a vertical line to the left of cells at horizontal position X
110 from Y1 to Y2 inclusive in style STYLE, if style is not -1. */
112 tab_vline (struct tab_table *t, int style, int x, int y1, int y2)
116 if (x < 0 || x > tab_nc (t)
117 || y1 < 0 || y1 >= tab_nr (t)
118 || y2 < 0 || y2 >= tab_nr (t))
120 printf (_("bad vline: x=%d y=(%d,%d) in table size (%d,%d)\n"),
121 x, y1, y2, tab_nc (t), tab_nr (t));
127 assert (x <= tab_nc (t));
130 assert (y2 <= tab_nr (t));
135 for (y = y1; y <= y2; y++)
136 t->rv[x + (tab_nc (t) + 1) * y] = style;
140 /* Draws a horizontal line above cells at vertical position Y from X1
141 to X2 inclusive in style STYLE, if style is not -1. */
143 tab_hline (struct tab_table *t, int style, int x1, int x2, int y)
147 if (y < 0 || y > tab_nr (t)
148 || x1 < 0 || x1 >= tab_nc (t)
149 || x2 < 0 || x2 >= tab_nc (t))
151 printf (_("bad hline: x=(%d,%d) y=%d in table size (%d,%d)\n"),
152 x1, x2, y, tab_nc (t), tab_nr (t));
158 assert (y <= tab_nr (t));
161 assert (x2 < tab_nc (t));
166 for (x = x1; x <= x2; x++)
167 t->rh[x + tab_nc (t) * y] = style;
171 /* Draws a box around cells (X1,Y1)-(X2,Y2) inclusive with horizontal
172 lines of style F_H and vertical lines of style F_V. Fills the
173 interior of the box with horizontal lines of style I_H and vertical
174 lines of style I_V. Any of the line styles may be -1 to avoid
175 drawing those lines. This is distinct from 0, which draws a null
178 tab_box (struct tab_table *t, int f_h, int f_v, int i_h, int i_v,
179 int x1, int y1, int x2, int y2)
183 if (x1 < 0 || x1 >= tab_nc (t)
184 || x2 < 0 || x2 >= tab_nc (t)
185 || y1 < 0 || y1 >= tab_nr (t)
186 || y2 < 0 || y2 >= tab_nr (t))
188 printf (_("bad box: (%d,%d)-(%d,%d) in table size (%d,%d)\n"),
189 x1, y1, x2, y2, tab_nc (t), tab_nr (t));
198 assert (x2 < tab_nc (t));
199 assert (y2 < tab_nr (t));
204 for (x = x1; x <= x2; x++)
206 t->rh[x + tab_nc (t) * y1] = f_h;
207 t->rh[x + tab_nc (t) * (y2 + 1)] = f_h;
213 for (y = y1; y <= y2; y++)
215 t->rv[x1 + (tab_nc (t) + 1) * y] = f_v;
216 t->rv[(x2 + 1) + (tab_nc (t) + 1) * y] = f_v;
224 for (y = y1 + 1; y <= y2; y++)
228 for (x = x1; x <= x2; x++)
229 t->rh[x + tab_nc (t) * y] = i_h;
236 for (x = x1 + 1; x <= x2; x++)
240 for (y = y1; y <= y2; y++)
241 t->rv[x + (tab_nc (t) + 1) * y] = i_v;
249 do_tab_text (struct tab_table *table, int c, int r, unsigned opt, char *text)
253 assert (c < tab_nc (table));
254 assert (r < tab_nr (table));
258 if (c < 0 || r < 0 || c >= tab_nc (table) || r >= tab_nr (table))
260 printf ("tab_text(): bad cell (%d,%d) in table size (%d,%d)\n",
261 c, r, tab_nc (table), tab_nr (table));
266 table->cc[c + r * tab_nc (table)] = text;
267 table->ct[c + r * tab_nc (table)] = opt;
270 /* Sets cell (C,R) in TABLE, with options OPT, to have text value
273 tab_text (struct tab_table *table, int c, int r, unsigned opt,
276 do_tab_text (table, c, r, opt, pool_strdup (table->container, text));
279 /* Sets cell (C,R) in TABLE, with options OPT, to have text value
280 FORMAT, which is formatted as if passed to printf. */
282 tab_text_format (struct tab_table *table, int c, int r, unsigned opt,
283 const char *format, ...)
287 va_start (args, format);
288 do_tab_text (table, c, r, opt,
289 pool_vasprintf (table->container, format, args));
293 static struct tab_joined_cell *
294 add_joined_cell (struct tab_table *table, int x1, int y1, int x2, int y2,
297 struct tab_joined_cell *j;
303 assert (y2 < tab_nr (table));
304 assert (x2 < tab_nc (table));
308 if (x1 < 0 || x1 >= tab_nc (table)
309 || y1 < 0 || y1 >= tab_nr (table)
310 || x2 < x1 || x2 >= tab_nc (table)
311 || y2 < y1 || y2 >= tab_nr (table))
313 printf ("tab_joint_text(): bad cell "
314 "(%d,%d)-(%d,%d) in table size (%d,%d)\n",
315 x1, y1, x2, y2, tab_nc (table), tab_nr (table));
320 tab_box (table, -1, -1, TAL_0, TAL_0, x1, y1, x2, y2);
322 j = pool_alloc (table->container, sizeof *j);
323 j->d[TABLE_HORZ][0] = x1;
324 j->d[TABLE_VERT][0] = y1;
325 j->d[TABLE_HORZ][1] = ++x2;
326 j->d[TABLE_VERT][1] = ++y2;
332 void **cc = &table->cc[x1 + y1 * tab_nc (table)];
333 unsigned short *ct = &table->ct[x1 + y1 * tab_nc (table)];
334 const int ofs = tab_nc (table) - (x2 - x1);
338 for (y = y1; y < y2; y++)
342 for (x = x1; x < x2; x++)
345 *ct++ = opt | TAB_JOIN;
356 /* Joins cells (X1,X2)-(Y1,Y2) inclusive in TABLE, and sets them with
357 options OPT to have text value TEXT. */
359 tab_joint_text (struct tab_table *table, int x1, int y1, int x2, int y2,
360 unsigned opt, const char *text)
362 char *s = pool_strdup (table->container, text);
363 if (x1 == x2 && y1 == y2)
364 do_tab_text (table, x1, y1, opt, s);
366 add_joined_cell (table, x1, y1, x2, y2, opt)->text = s;
370 tab_create_footnote (struct tab_table *table, size_t idx, const char *content,
371 const char *marker, struct area_style *style)
373 struct footnote *f = pool_alloc (table->container, sizeof *f);
375 f->content = pool_strdup (table->container, content);
376 f->marker = pool_strdup (table->container, marker);
382 tab_add_footnote (struct tab_table *table, int x, int y,
383 const struct footnote *f)
385 int index = x + y * tab_nc (table);
386 unsigned short opt = table->ct[index];
387 struct tab_joined_cell *j;
390 j = table->cc[index];
393 char *text = table->cc[index];
395 j = add_joined_cell (table, x, y, x, y, table->ct[index]);
396 j->text = text ? text : xstrdup ("");
399 j->footnotes = pool_realloc (table->container, j->footnotes,
400 (j->n_footnotes + 1) * sizeof *j->footnotes);
402 j->footnotes[j->n_footnotes++] = f;
406 tab_add_style (struct tab_table *table, int x, int y,
407 const struct area_style *style)
409 int index = x + y * tab_nc (table);
410 unsigned short opt = table->ct[index];
411 struct tab_joined_cell *j;
414 j = table->cc[index];
417 char *text = table->cc[index];
419 j = add_joined_cell (table, x, y, x, y, table->ct[index]);
420 j->text = text ? text : xstrdup ("");
427 tab_cell_is_empty (const struct tab_table *table, int c, int r)
429 return table->cc[c + r * tab_nc (table)] == NULL;
434 /* Writes STRING to the output. OPTIONS may be any valid combination of TAB_*
437 This function is obsolete. Please do not add new uses of it. Instead, use
438 a text_item (see output/text-item.h). */
440 tab_output_text (int options UNUSED, const char *string)
442 text_item_submit (text_item_create (TEXT_ITEM_LOG, string));
445 /* Same as tab_output_text(), but FORMAT is passed through printf-like
446 formatting before output. */
448 tab_output_text_format (int options, const char *format, ...)
453 va_start (args, format);
454 text = xvasprintf (format, args);
457 tab_output_text (options, text);
462 /* Table class implementation. */
465 tab_destroy (struct table *table)
467 struct tab_table *t = tab_cast (table);
468 pool_destroy (t->container);
472 tab_get_cell (const struct table *table, int x, int y,
473 struct table_cell *cell)
475 const struct tab_table *t = tab_cast (table);
476 int index = x + y * tab_nc (t);
477 unsigned short opt = t->ct[index];
478 const void *cc = t->cc[index];
481 cell->n_footnotes = 0;
483 int style_idx = (opt & TAB_STYLE_MASK) >> TAB_STYLE_SHIFT;
484 const struct area_style *style = t->styles[style_idx];
489 static const struct area_style styles[3][3] = {
490 #define S(H,V) [H][V] = { AREA_STYLE_INITIALIZER__, \
491 .cell_style.halign = H, \
492 .cell_style.valign = V }
493 S(TABLE_HALIGN_LEFT, TABLE_VALIGN_TOP),
494 S(TABLE_HALIGN_LEFT, TABLE_VALIGN_CENTER),
495 S(TABLE_HALIGN_LEFT, TABLE_VALIGN_BOTTOM),
496 S(TABLE_HALIGN_CENTER, TABLE_VALIGN_TOP),
497 S(TABLE_HALIGN_CENTER, TABLE_VALIGN_CENTER),
498 S(TABLE_HALIGN_CENTER, TABLE_VALIGN_BOTTOM),
499 S(TABLE_HALIGN_RIGHT, TABLE_VALIGN_TOP),
500 S(TABLE_HALIGN_RIGHT, TABLE_VALIGN_CENTER),
501 S(TABLE_HALIGN_RIGHT, TABLE_VALIGN_BOTTOM),
504 enum table_halign halign
505 = ((opt & TAB_HALIGN) == TAB_LEFT ? TABLE_HALIGN_LEFT
506 : (opt & TAB_HALIGN) == TAB_CENTER ? TABLE_HALIGN_CENTER
507 : TABLE_HALIGN_RIGHT);
508 enum table_valign valign
509 = ((opt & TAB_VALIGN) == TAB_TOP ? TABLE_VALIGN_TOP
510 : (opt & TAB_VALIGN) == TAB_MIDDLE ? TABLE_VALIGN_CENTER
511 : TABLE_VALIGN_BOTTOM);
513 cell->style = &styles[halign][valign];
518 const struct tab_joined_cell *jc = cc;
519 cell->text = jc->text;
521 cell->footnotes = jc->footnotes;
522 cell->n_footnotes = jc->n_footnotes;
524 cell->d[TABLE_HORZ][0] = jc->d[TABLE_HORZ][0];
525 cell->d[TABLE_HORZ][1] = jc->d[TABLE_HORZ][1];
526 cell->d[TABLE_VERT][0] = jc->d[TABLE_VERT][0];
527 cell->d[TABLE_VERT][1] = jc->d[TABLE_VERT][1];
530 cell->style = jc->style;
534 cell->d[TABLE_HORZ][0] = x;
535 cell->d[TABLE_HORZ][1] = x + 1;
536 cell->d[TABLE_VERT][0] = y;
537 cell->d[TABLE_VERT][1] = y + 1;
538 cell->text = CONST_CAST (char *, cc ? cc : "");
543 tab_get_rule (const struct table *table, enum table_axis axis, int x, int y,
544 struct cell_color *color)
546 const struct tab_table *t = tab_cast (table);
547 uint8_t raw = (axis == TABLE_VERT
548 ? t->rh[x + tab_nc (t) * y]
549 : t->rv[x + (tab_nc (t) + 1) * y]);
550 struct cell_color *p = t->rule_colors[(raw & TAB_RULE_STYLE_MASK)
551 >> TAB_RULE_STYLE_SHIFT];
554 return (raw & TAB_RULE_TYPE_MASK) >> TAB_RULE_TYPE_SHIFT;
557 static const struct table_class tab_table_class = {
564 tab_cast (const struct table *table)
566 assert (table->klass == &tab_table_class);
567 return UP_CAST (table, struct tab_table, table);