table: Move tab.h into table.h and rename tab_*() to table_*().
[pspp] / src / output / pivot-output.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2018 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <stdlib.h>
20
21 #include "output/pivot-table.h"
22
23 #include "data/settings.h"
24 #include "libpspp/assertion.h"
25 #include "libpspp/pool.h"
26 #include "output/table.h"
27 #include "output/table-item.h"
28 #include "output/text-item.h"
29 #include "output/table-provider.h"
30
31 #include "gl/minmax.h"
32 #include "gl/xalloc.h"
33
34 #define H TABLE_HORZ
35 #define V TABLE_VERT
36
37 static const struct pivot_category *
38 find_category (const struct pivot_dimension *d, int dim_index,
39                const size_t *indexes, int row_ofs)
40 {
41   size_t index = indexes[dim_index];
42   assert (index < d->n_leaves);
43   for (const struct pivot_category *c = d->presentation_leaves[index];
44        c; c = c->parent)
45     {
46       if (!row_ofs)
47         return c;
48
49       row_ofs -= 1 + c->extra_depth;
50       if (row_ofs < 0)
51         return NULL;
52     }
53   return NULL;
54 }
55
56 static struct area_style *
57 area_style_override (struct pool *pool,
58                      const struct area_style *in,
59                      const struct cell_style *cell_,
60                      const struct font_style *font_)
61 {
62   const struct cell_style *cell = cell_ ? cell_ : &in->cell_style;
63   const struct font_style *font = font_ ? font_ : &in->font_style;
64
65   struct area_style *out = (pool
66                             ? pool_alloc (pool, sizeof *out)
67                             : xmalloc (sizeof *out));
68   *out = (struct area_style) {
69     .cell_style.halign = cell->halign,
70     .cell_style.valign = cell->valign,
71     .cell_style.decimal_offset = cell->decimal_offset,
72     .cell_style.margin[H][0] = cell->margin[H][0],
73     .cell_style.margin[H][1] = cell->margin[H][1],
74     .cell_style.margin[V][0] = cell->margin[V][0],
75     .cell_style.margin[V][1] = cell->margin[V][1],
76     .font_style.fg[0] = font->fg[0],
77     .font_style.fg[1] = font->fg[1],
78     .font_style.bg[0] = font->bg[0],
79     .font_style.bg[1] = font->bg[1],
80     .font_style.typeface = (font->typeface
81                             ? pool_strdup (pool, font->typeface)
82                             : NULL),
83     .font_style.size = font->size,
84     .font_style.bold = font->bold,
85     .font_style.italic = font->italic,
86     .font_style.underline = font->underline,
87     .font_style.markup = font->markup,
88   };
89   return out;
90 }
91
92 static void
93 fill_cell (struct table *t, int x1, int y1, int x2, int y2,
94            const struct area_style *style, int style_idx,
95            const struct pivot_value *value, struct footnote **footnotes,
96            enum settings_value_show show_values,
97            enum settings_value_show show_variables,
98            bool rotate_label)
99 {
100
101   struct string s = DS_EMPTY_INITIALIZER;
102   int opts = style_idx << TAB_STYLE_SHIFT;
103   if (value)
104     {
105       bool numeric = pivot_value_format_body (value, show_values,
106                                               show_variables, &s);
107       if (numeric)
108         opts |= TAB_NUMERIC;
109       if (value->font_style && value->font_style->markup)
110         opts |= TAB_MARKUP;
111       if (rotate_label)
112         opts |= TAB_ROTATE;
113     }
114   table_joint_text (t, x1, y1, x2, y2, opts, ds_cstr (&s));
115   ds_destroy (&s);
116
117   if (value)
118     {
119       if (value->cell_style || value->font_style)
120         table_add_style (t, x1, y1,
121                          area_style_override (t->container, style,
122                                               value->cell_style,
123                                               value->font_style));
124       
125       for (size_t i = 0; i < value->n_footnotes; i++)
126         table_add_footnote (t, x1, y1, footnotes[value->footnotes[i]->idx]);
127     }
128 }
129
130 static struct table_item_text *
131 pivot_value_to_table_item_text (const struct pivot_value *value,
132                                 const struct area_style *area,
133                                 struct footnote **footnotes,
134                                 enum settings_value_show show_values,
135                                 enum settings_value_show show_variables)
136 {
137   if (!value)
138     return NULL;
139
140   struct string s = DS_EMPTY_INITIALIZER;
141   pivot_value_format_body (value, show_values, show_variables, &s);
142
143   struct table_item_text *text = xmalloc (sizeof *text);
144   *text = (struct table_item_text) {
145     .content = ds_steal_cstr (&s),
146     .footnotes = xnmalloc (value->n_footnotes, sizeof *text->footnotes),
147     .n_footnotes = value->n_footnotes,
148     .style = area_style_override (
149       NULL, area, value->cell_style, value->font_style),
150   };
151
152   for (size_t i = 0; i < value->n_footnotes; i++)
153     text->footnotes[i] = footnotes[value->footnotes[i]->idx];
154
155   return text;
156 }
157
158 static int
159 get_table_rule (const struct table_border_style *styles,
160                 enum pivot_border style_idx)
161 {
162   return styles[style_idx].stroke | (style_idx << TAB_RULE_STYLE_SHIFT);
163 }
164
165 static void
166 draw_line (struct table *t, const struct table_border_style *styles,
167            enum pivot_border style_idx,
168            enum table_axis axis, int a, int b0, int b1)
169 {
170   int rule = get_table_rule (styles, style_idx);
171   if (axis == H)
172     table_hline (t, rule, b0, b1, a);
173   else
174     table_vline (t, rule, a, b0, b1);
175 }
176
177 static void
178 compose_headings (struct table *t,
179                   const struct pivot_axis *a_axis, enum table_axis a,
180                   const struct pivot_axis *b_axis,
181                   const struct table_border_style *borders,
182                   enum pivot_border dim_col_horz,
183                   enum pivot_border dim_col_vert,
184                   enum pivot_border cat_col_horz,
185                   enum pivot_border cat_col_vert,
186                   const size_t *column_enumeration, size_t n_columns,
187                   const struct area_style *label_style, int label_style_idx,
188                   const struct area_style *corner_style,
189                   struct footnote **footnotes,
190                   enum settings_value_show show_values,
191                   enum settings_value_show show_variables,
192                   bool rotate_inner_labels, bool rotate_outer_labels)
193 {
194   enum table_axis b = !a;
195   int b_size = a_axis->label_depth;
196   int a_ofs = b_axis->label_depth;
197
198   if (!a_axis->n_dimensions || !n_columns || !b_size)
199     return;
200
201   int bottom_row = b_size - 1;
202   const int stride = MAX (1, a_axis->n_dimensions);
203   for (int dim_index = 0; dim_index < a_axis->n_dimensions; dim_index++)
204     {
205       const struct pivot_dimension *d = a_axis->dimensions[dim_index];
206       if (d->hide_all_labels)
207         continue;
208
209       for (int row_ofs = 0; row_ofs < d->label_depth; row_ofs++)
210         {
211           for (size_t x1 = 0; x1 < n_columns; )
212             {
213               const struct pivot_category *c = find_category (
214                 d, dim_index, column_enumeration + x1 * stride, row_ofs);
215               if (!c)
216                 {
217                   x1++;
218                   continue;
219                 }
220
221               size_t x2;
222               for (x2 = x1 + 1; x2 < n_columns; x2++)
223                 {
224                   const struct pivot_category *c2 = find_category (
225                     d, dim_index, column_enumeration + x2 * stride, row_ofs);
226                   if (c != c2)
227                     break;
228                 }
229
230               int y1 = bottom_row - row_ofs - c->extra_depth;
231               int y2 = bottom_row - row_ofs + 1;
232               bool is_outer_row = y1 == 0;
233               bool is_inner_row = y2 == b_size;
234               if (pivot_category_is_leaf (c) || c->show_label)
235                 {
236                   int bb[TABLE_N_AXES][2];
237                   bb[a][0] = x1 + a_ofs;
238                   bb[a][1] = x2 + a_ofs - 1;
239                   bb[b][0] = y1;
240                   bb[b][1] = y2 - 1;
241                   bool rotate = ((rotate_inner_labels && is_inner_row)
242                                  || (rotate_outer_labels && is_outer_row));
243                   fill_cell (t, bb[H][0], bb[V][0], bb[H][1], bb[V][1],
244                              label_style, label_style_idx, c->name, footnotes,
245                              show_values, show_variables, rotate);
246
247                   if (pivot_category_is_leaf (c) && x2 + 1 <= n_columns)
248                     {
249                       enum pivot_border style
250                         = (y1 == 0 && a_axis->label_depth > d->label_depth
251                            ? dim_col_vert
252                            : cat_col_vert);
253                       draw_line (t, borders, style, b, x2 + a_ofs, y1,
254                                  t->n[b] - 1);
255                     }
256                   if (pivot_category_is_leaf (c) && x1 > 0)
257                     {
258                       enum pivot_border style
259                         = (y1 == 0 && a_axis->label_depth > d->label_depth
260                            ? dim_col_vert
261                            : cat_col_vert);
262                       draw_line (t, borders, style, b, x1 + a_ofs, y1,
263                                  t->n[b] - 1);
264                     }
265                 }
266               if (c->parent && c->parent->show_label)
267                 draw_line (t, borders, cat_col_horz, a, y1,
268                            x1 + a_ofs, x2 + a_ofs - 1);
269
270               x1 = x2;
271             }
272         }
273
274       if (d->root->show_label_in_corner && a_ofs > 0)
275         {
276           int bb[TABLE_N_AXES][2];
277           bb[a][0] = a_ofs - 1;
278           bb[a][1] = a_ofs - 1;
279           bb[b][0] = bottom_row - d->label_depth + 1;
280           bb[b][1] = bottom_row;
281           fill_cell (t, bb[H][0], bb[V][0], bb[H][1], bb[V][1],
282                      corner_style, PIVOT_AREA_CORNER, d->root->name, footnotes,
283                      show_values, show_variables, false);
284         }
285
286       if (dim_index > 1)
287         draw_line (t, borders, dim_col_horz, a, bottom_row + 1, a_ofs,
288                    t->n[a] - 1);
289
290       bottom_row -= d->label_depth;
291     }
292 }
293
294 static void
295 pivot_table_submit_layer (const struct pivot_table *pt,
296                           const size_t *layer_indexes)
297 {
298   const size_t *pindexes[PIVOT_N_AXES]
299     = { [PIVOT_AXIS_LAYER] = layer_indexes };
300
301   size_t body[TABLE_N_AXES];
302   size_t *column_enumeration = pivot_table_enumerate_axis (
303     pt, PIVOT_AXIS_COLUMN, layer_indexes, pt->omit_empty, &body[H]);
304   size_t *row_enumeration = pivot_table_enumerate_axis (
305     pt, PIVOT_AXIS_ROW, layer_indexes, pt->omit_empty, &body[V]);
306
307   int stub[TABLE_N_AXES] = {
308     [H] = pt->axes[PIVOT_AXIS_ROW].label_depth,
309     [V] = pt->axes[PIVOT_AXIS_COLUMN].label_depth,
310   };
311   struct table *table = table_create (body[H] + stub[H],
312                                       body[V] + stub[V],
313                                       stub[H], 0, stub[V], 0);
314
315   for (size_t i = 0; i < PIVOT_N_AREAS; i++)
316     table->styles[i] = area_style_override (table->container, &pt->areas[i],
317                                             NULL, NULL);
318
319   for (size_t i = 0; i < PIVOT_N_BORDERS; i++)
320     {
321       const struct table_border_style *in = &pt->borders[i];
322       table->rule_colors[i] = pool_alloc (table->container,
323                                           sizeof *table->rule_colors[i]);
324       struct cell_color *out = table->rule_colors[i];
325       out->alpha = in->color.alpha;
326       out->r = in->color.r;
327       out->g = in->color.g;
328       out->b = in->color.b;
329     }
330
331   struct footnote **footnotes = xcalloc (pt->n_footnotes, sizeof *footnotes);
332   for (size_t i = 0; i < pt->n_footnotes; i++)
333     {
334       char *content = pivot_value_to_string (
335         pt->footnotes[i]->content, pt->show_values, pt->show_variables);
336       char *marker = pivot_value_to_string (
337         pt->footnotes[i]->marker, pt->show_values, pt->show_variables);
338       footnotes[i] = table_create_footnote (
339         table, i, content, marker,
340         area_style_override (table->container, &pt->areas[PIVOT_AREA_FOOTER],
341                              pt->footnotes[i]->content->cell_style,
342                              pt->footnotes[i]->content->font_style));
343       free (marker);
344       free (content);
345     }
346
347   compose_headings (table,
348                     &pt->axes[PIVOT_AXIS_COLUMN], H, &pt->axes[PIVOT_AXIS_ROW],
349                     pt->borders,
350                     PIVOT_BORDER_DIM_COL_HORZ,
351                     PIVOT_BORDER_DIM_COL_VERT,
352                     PIVOT_BORDER_CAT_COL_HORZ,
353                     PIVOT_BORDER_CAT_COL_VERT,
354                     column_enumeration, body[H],
355                     &pt->areas[PIVOT_AREA_COLUMN_LABELS],
356                     PIVOT_AREA_COLUMN_LABELS,
357                     &pt->areas[PIVOT_AREA_CORNER], footnotes,
358                     pt->show_values, pt->show_variables,
359                     pt->rotate_inner_column_labels, false);
360
361   compose_headings (table,
362                     &pt->axes[PIVOT_AXIS_ROW], V, &pt->axes[PIVOT_AXIS_COLUMN],
363                     pt->borders,
364                     PIVOT_BORDER_DIM_ROW_VERT,
365                     PIVOT_BORDER_DIM_ROW_HORZ,
366                     PIVOT_BORDER_CAT_ROW_VERT,
367                     PIVOT_BORDER_CAT_ROW_HORZ,
368                     row_enumeration, body[V],
369                     &pt->areas[PIVOT_AREA_ROW_LABELS],
370                     PIVOT_AREA_ROW_LABELS,
371                     &pt->areas[PIVOT_AREA_CORNER], footnotes,
372                     pt->show_values, pt->show_variables,
373                     false, pt->rotate_outer_row_labels);
374
375   size_t *dindexes = xcalloc (pt->n_dimensions, sizeof *dindexes);
376   size_t y = 0;
377   PIVOT_ENUMERATION_FOR_EACH (pindexes[PIVOT_AXIS_ROW], row_enumeration,
378                               &pt->axes[PIVOT_AXIS_ROW])
379     {
380       size_t x = 0;
381       PIVOT_ENUMERATION_FOR_EACH (pindexes[PIVOT_AXIS_COLUMN],
382                                   column_enumeration,
383                                   &pt->axes[PIVOT_AXIS_COLUMN])
384         {
385           pivot_table_convert_indexes_ptod (pt, pindexes, dindexes);
386           const struct pivot_value *value = pivot_table_get (pt, dindexes);
387           fill_cell (table,
388                      x + stub[H], y + stub[V],
389                      x + stub[H], y + stub[V],
390                      &pt->areas[PIVOT_AREA_DATA], PIVOT_AREA_DATA,
391                      value, footnotes,
392                      pt->show_values, pt->show_variables, false);
393
394           x++;
395         }
396
397       y++;
398     }
399   free (dindexes);
400
401   if (pt->corner_text && stub[H] && stub[V])
402     fill_cell (table, 0, 0, stub[H] - 1, stub[V] - 1,
403                &pt->areas[PIVOT_AREA_CORNER], PIVOT_AREA_CORNER,
404                pt->corner_text, footnotes,
405                pt->show_values, pt->show_variables, false);
406
407   if (table_nc (table) && table_nr (table))
408     {
409       table_hline (
410         table, get_table_rule (pt->borders, PIVOT_BORDER_INNER_TOP),
411         0, table_nc (table) - 1, 0);
412       table_hline (
413         table, get_table_rule (pt->borders, PIVOT_BORDER_INNER_BOTTOM),
414         0, table_nc (table) - 1, table_nr (table));
415       table_vline (
416         table, get_table_rule (pt->borders, PIVOT_BORDER_INNER_LEFT),
417         0, 0, table_nr (table) - 1);
418       table_vline (
419         table, get_table_rule (pt->borders, PIVOT_BORDER_INNER_RIGHT),
420         table_nc (table), 0, table_nr (table) - 1);
421
422       if (stub[V])
423         table_hline (
424           table, get_table_rule (pt->borders, PIVOT_BORDER_DATA_TOP),
425           0, table_nc (table) - 1, stub[V]);
426       if (stub[H])
427         table_vline (
428           table, get_table_rule (pt->borders, PIVOT_BORDER_DATA_LEFT),
429           stub[H], 0, table_nr (table) - 1);
430
431     }
432   free (column_enumeration);
433   free (row_enumeration);
434
435   struct table_item *ti = table_item_create (table, NULL, NULL);
436
437   if (pt->title)
438     {
439       struct table_item_text *title = pivot_value_to_table_item_text (
440         pt->title, &pt->areas[PIVOT_AREA_TITLE], footnotes,
441         pt->show_values, pt->show_variables);
442       table_item_set_title (ti, title);
443       table_item_text_destroy (title);
444     }
445
446   const struct pivot_axis *layer_axis = &pt->axes[PIVOT_AXIS_LAYER];
447   struct table_item_layers *layers = NULL;
448   for (size_t i = 0; i < layer_axis->n_dimensions; i++)
449     {
450       const struct pivot_dimension *d = layer_axis->dimensions[i];
451       if (d->n_leaves)
452         {
453           if (!layers)
454             {
455               layers = xzalloc (sizeof *layers);
456               layers->style = area_style_override (
457                 NULL, &pt->areas[PIVOT_AREA_LAYERS], NULL, NULL);
458               layers->layers = xnmalloc (layer_axis->n_dimensions,
459                                          sizeof *layers->layers);
460             }
461
462           const struct pivot_value *name
463             = d->data_leaves[layer_indexes[i]]->name;
464           struct table_item_layer *layer = &layers->layers[layers->n_layers++];
465           struct string s = DS_EMPTY_INITIALIZER;
466           pivot_value_format_body (name, pt->show_values, pt->show_variables,
467                                    &s);
468           layer->content = ds_steal_cstr (&s);
469           layer->n_footnotes = name->n_footnotes;
470           layer->footnotes = xnmalloc (layer->n_footnotes,
471                                        sizeof *layer->footnotes);
472           for (size_t i = 0; i < name->n_footnotes; i++)
473             layer->footnotes[i] = footnotes[name->footnotes[i]->idx];
474         }
475     }
476   if (layers)
477     {
478       table_item_set_layers (ti, layers);
479       table_item_layers_destroy (layers);
480     }
481
482   if (pt->caption)
483     {
484       struct table_item_text *caption = pivot_value_to_table_item_text (
485         pt->caption, &pt->areas[PIVOT_AREA_CAPTION], footnotes,
486         pt->show_values, pt->show_variables);
487       table_item_set_caption (ti, caption);
488       table_item_text_destroy (caption);
489     }
490
491   free (footnotes);
492   table_item_submit (ti);
493 }
494
495 void
496 pivot_table_submit (struct pivot_table *pt)
497 {
498   pivot_table_assign_label_depth (CONST_CAST (struct pivot_table *, pt));
499
500   int old_decimal = settings_get_decimal_char (FMT_COMMA);
501   if (pt->decimal == '.' || pt->decimal == ',')
502     settings_set_decimal_char (pt->decimal);
503
504   if (pt->print_all_layers)
505     {
506       size_t *layer_indexes;
507
508       PIVOT_AXIS_FOR_EACH (layer_indexes, &pt->axes[PIVOT_AXIS_LAYER])
509         {
510           if (pt->paginate_layers)
511             text_item_submit (text_item_create (TEXT_ITEM_EJECT_PAGE, ""));
512           pivot_table_submit_layer (pt, layer_indexes);
513         }
514     }
515   else
516     pivot_table_submit_layer (pt, pt->current_layer);
517
518   settings_set_decimal_char (old_decimal);
519
520   pivot_table_unref (pt);
521 }