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