fc41cb870f36d6439c3955982d1bed0760a42eaf
[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       if (value->n_subscripts)
129         table_add_subscripts (t, x1, y1,
130                               value->subscripts, value->n_subscripts);
131     }
132 }
133
134 static struct table_item_text *
135 pivot_value_to_table_item_text (const struct pivot_value *value,
136                                 const struct area_style *area,
137                                 struct footnote **footnotes,
138                                 enum settings_value_show show_values,
139                                 enum settings_value_show show_variables)
140 {
141   if (!value)
142     return NULL;
143
144   struct string s = DS_EMPTY_INITIALIZER;
145   pivot_value_format_body (value, show_values, show_variables, &s);
146
147   struct table_item_text *text = xmalloc (sizeof *text);
148   *text = (struct table_item_text) {
149     .content = ds_steal_cstr (&s),
150     .footnotes = xnmalloc (value->n_footnotes, sizeof *text->footnotes),
151     .n_footnotes = value->n_footnotes,
152     .style = area_style_override (
153       NULL, area, value->cell_style, value->font_style),
154   };
155
156   for (size_t i = 0; i < value->n_footnotes; i++)
157     text->footnotes[i] = footnotes[value->footnotes[i]->idx];
158
159   return text;
160 }
161
162 static int
163 get_table_rule (const struct table_border_style *styles,
164                 enum pivot_border style_idx)
165 {
166   return styles[style_idx].stroke | (style_idx << TAB_RULE_STYLE_SHIFT);
167 }
168
169 static void
170 draw_line (struct table *t, const struct table_border_style *styles,
171            enum pivot_border style_idx,
172            enum table_axis axis, int a, int b0, int b1)
173 {
174   int rule = get_table_rule (styles, style_idx);
175   if (axis == H)
176     table_hline (t, rule, b0, b1, a);
177   else
178     table_vline (t, rule, a, b0, b1);
179 }
180
181 static void
182 compose_headings (struct table *t,
183                   const struct pivot_axis *a_axis, enum table_axis a,
184                   const struct pivot_axis *b_axis,
185                   const struct table_border_style *borders,
186                   enum pivot_border dim_col_horz,
187                   enum pivot_border dim_col_vert,
188                   enum pivot_border cat_col_horz,
189                   enum pivot_border cat_col_vert,
190                   const size_t *column_enumeration, size_t n_columns,
191                   const struct area_style *label_style, int label_style_idx,
192                   const struct area_style *corner_style,
193                   struct footnote **footnotes,
194                   enum settings_value_show show_values,
195                   enum settings_value_show show_variables,
196                   bool rotate_inner_labels, bool rotate_outer_labels)
197 {
198   enum table_axis b = !a;
199   int b_size = a_axis->label_depth;
200   int a_ofs = b_axis->label_depth;
201
202   if (!a_axis->n_dimensions || !n_columns || !b_size)
203     return;
204
205   int bottom_row = b_size - 1;
206   const int stride = MAX (1, a_axis->n_dimensions);
207   for (int dim_index = 0; dim_index < a_axis->n_dimensions; dim_index++)
208     {
209       const struct pivot_dimension *d = a_axis->dimensions[dim_index];
210       if (d->hide_all_labels)
211         continue;
212
213       for (int row_ofs = 0; row_ofs < d->label_depth; row_ofs++)
214         {
215           for (size_t x1 = 0; x1 < n_columns; )
216             {
217               const struct pivot_category *c = find_category (
218                 d, dim_index, column_enumeration + x1 * stride, row_ofs);
219               if (!c)
220                 {
221                   x1++;
222                   continue;
223                 }
224
225               size_t x2;
226               for (x2 = x1 + 1; x2 < n_columns; x2++)
227                 {
228                   const struct pivot_category *c2 = find_category (
229                     d, dim_index, column_enumeration + x2 * stride, row_ofs);
230                   if (c != c2)
231                     break;
232                 }
233
234               int y1 = bottom_row - row_ofs - c->extra_depth;
235               int y2 = bottom_row - row_ofs + 1;
236               bool is_outer_row = y1 == 0;
237               bool is_inner_row = y2 == b_size;
238               if (pivot_category_is_leaf (c) || c->show_label)
239                 {
240                   int bb[TABLE_N_AXES][2];
241                   bb[a][0] = x1 + a_ofs;
242                   bb[a][1] = x2 + a_ofs - 1;
243                   bb[b][0] = y1;
244                   bb[b][1] = y2 - 1;
245                   bool rotate = ((rotate_inner_labels && is_inner_row)
246                                  || (rotate_outer_labels && is_outer_row));
247                   fill_cell (t, bb[H][0], bb[V][0], bb[H][1], bb[V][1],
248                              label_style, label_style_idx, c->name, footnotes,
249                              show_values, show_variables, rotate);
250
251                   if (pivot_category_is_leaf (c) && x2 + 1 <= n_columns)
252                     {
253                       enum pivot_border style
254                         = (y1 == 0 && a_axis->label_depth > d->label_depth
255                            ? dim_col_vert
256                            : cat_col_vert);
257                       draw_line (t, borders, style, b, x2 + a_ofs, y1,
258                                  t->n[b] - 1);
259                     }
260                   if (pivot_category_is_leaf (c) && x1 > 0)
261                     {
262                       enum pivot_border style
263                         = (y1 == 0 && a_axis->label_depth > d->label_depth
264                            ? dim_col_vert
265                            : cat_col_vert);
266                       draw_line (t, borders, style, b, x1 + a_ofs, y1,
267                                  t->n[b] - 1);
268                     }
269                 }
270               if (c->parent && c->parent->show_label)
271                 draw_line (t, borders, cat_col_horz, a, y1,
272                            x1 + a_ofs, x2 + a_ofs - 1);
273
274               x1 = x2;
275             }
276         }
277
278       if (d->root->show_label_in_corner && a_ofs > 0)
279         {
280           int bb[TABLE_N_AXES][2];
281           bb[a][0] = a_ofs - 1;
282           bb[a][1] = a_ofs - 1;
283           bb[b][0] = bottom_row - d->label_depth + 1;
284           bb[b][1] = bottom_row;
285           fill_cell (t, bb[H][0], bb[V][0], bb[H][1], bb[V][1],
286                      corner_style, PIVOT_AREA_CORNER, d->root->name, footnotes,
287                      show_values, show_variables, false);
288         }
289
290       if (dim_index > 1)
291         draw_line (t, borders, dim_col_horz, a, bottom_row + 1, a_ofs,
292                    t->n[a] - 1);
293
294       bottom_row -= d->label_depth;
295     }
296 }
297
298 static void
299 pivot_table_submit_layer (const struct pivot_table *pt,
300                           const size_t *layer_indexes)
301 {
302   const size_t *pindexes[PIVOT_N_AXES]
303     = { [PIVOT_AXIS_LAYER] = layer_indexes };
304
305   size_t body[TABLE_N_AXES];
306   size_t *column_enumeration = pivot_table_enumerate_axis (
307     pt, PIVOT_AXIS_COLUMN, layer_indexes, pt->omit_empty, &body[H]);
308   size_t *row_enumeration = pivot_table_enumerate_axis (
309     pt, PIVOT_AXIS_ROW, layer_indexes, pt->omit_empty, &body[V]);
310
311   int stub[TABLE_N_AXES] = {
312     [H] = pt->axes[PIVOT_AXIS_ROW].label_depth,
313     [V] = pt->axes[PIVOT_AXIS_COLUMN].label_depth,
314   };
315   struct table *table = table_create (body[H] + stub[H],
316                                       body[V] + stub[V],
317                                       stub[H], 0, stub[V], 0);
318
319   for (size_t i = 0; i < PIVOT_N_AREAS; i++)
320     table->styles[i] = area_style_override (table->container, &pt->areas[i],
321                                             NULL, NULL);
322
323   for (size_t i = 0; i < PIVOT_N_BORDERS; i++)
324     {
325       const struct table_border_style *in = &pt->borders[i];
326       table->rule_colors[i] = pool_alloc (table->container,
327                                           sizeof *table->rule_colors[i]);
328       struct cell_color *out = table->rule_colors[i];
329       out->alpha = in->color.alpha;
330       out->r = in->color.r;
331       out->g = in->color.g;
332       out->b = in->color.b;
333     }
334
335   struct footnote **footnotes = xcalloc (pt->n_footnotes, sizeof *footnotes);
336   for (size_t i = 0; i < pt->n_footnotes; i++)
337     {
338       char *content = pivot_value_to_string (
339         pt->footnotes[i]->content, pt->show_values, pt->show_variables);
340       char *marker = pivot_value_to_string (
341         pt->footnotes[i]->marker, pt->show_values, pt->show_variables);
342       footnotes[i] = table_create_footnote (
343         table, i, content, marker,
344         area_style_override (table->container, &pt->areas[PIVOT_AREA_FOOTER],
345                              pt->footnotes[i]->content->cell_style,
346                              pt->footnotes[i]->content->font_style));
347       free (marker);
348       free (content);
349     }
350
351   compose_headings (table,
352                     &pt->axes[PIVOT_AXIS_COLUMN], H, &pt->axes[PIVOT_AXIS_ROW],
353                     pt->borders,
354                     PIVOT_BORDER_DIM_COL_HORZ,
355                     PIVOT_BORDER_DIM_COL_VERT,
356                     PIVOT_BORDER_CAT_COL_HORZ,
357                     PIVOT_BORDER_CAT_COL_VERT,
358                     column_enumeration, body[H],
359                     &pt->areas[PIVOT_AREA_COLUMN_LABELS],
360                     PIVOT_AREA_COLUMN_LABELS,
361                     &pt->areas[PIVOT_AREA_CORNER], footnotes,
362                     pt->show_values, pt->show_variables,
363                     pt->rotate_inner_column_labels, false);
364
365   compose_headings (table,
366                     &pt->axes[PIVOT_AXIS_ROW], V, &pt->axes[PIVOT_AXIS_COLUMN],
367                     pt->borders,
368                     PIVOT_BORDER_DIM_ROW_VERT,
369                     PIVOT_BORDER_DIM_ROW_HORZ,
370                     PIVOT_BORDER_CAT_ROW_VERT,
371                     PIVOT_BORDER_CAT_ROW_HORZ,
372                     row_enumeration, body[V],
373                     &pt->areas[PIVOT_AREA_ROW_LABELS],
374                     PIVOT_AREA_ROW_LABELS,
375                     &pt->areas[PIVOT_AREA_CORNER], footnotes,
376                     pt->show_values, pt->show_variables,
377                     false, pt->rotate_outer_row_labels);
378
379   size_t *dindexes = xcalloc (pt->n_dimensions, sizeof *dindexes);
380   size_t y = 0;
381   PIVOT_ENUMERATION_FOR_EACH (pindexes[PIVOT_AXIS_ROW], row_enumeration,
382                               &pt->axes[PIVOT_AXIS_ROW])
383     {
384       size_t x = 0;
385       PIVOT_ENUMERATION_FOR_EACH (pindexes[PIVOT_AXIS_COLUMN],
386                                   column_enumeration,
387                                   &pt->axes[PIVOT_AXIS_COLUMN])
388         {
389           pivot_table_convert_indexes_ptod (pt, pindexes, dindexes);
390           const struct pivot_value *value = pivot_table_get (pt, dindexes);
391           fill_cell (table,
392                      x + stub[H], y + stub[V],
393                      x + stub[H], y + stub[V],
394                      &pt->areas[PIVOT_AREA_DATA], PIVOT_AREA_DATA,
395                      value, footnotes,
396                      pt->show_values, pt->show_variables, false);
397
398           x++;
399         }
400
401       y++;
402     }
403   free (dindexes);
404
405   if (pt->corner_text && stub[H] && stub[V])
406     fill_cell (table, 0, 0, stub[H] - 1, stub[V] - 1,
407                &pt->areas[PIVOT_AREA_CORNER], PIVOT_AREA_CORNER,
408                pt->corner_text, footnotes,
409                pt->show_values, pt->show_variables, false);
410
411   if (table_nc (table) && table_nr (table))
412     {
413       table_hline (
414         table, get_table_rule (pt->borders, PIVOT_BORDER_INNER_TOP),
415         0, table_nc (table) - 1, 0);
416       table_hline (
417         table, get_table_rule (pt->borders, PIVOT_BORDER_INNER_BOTTOM),
418         0, table_nc (table) - 1, table_nr (table));
419       table_vline (
420         table, get_table_rule (pt->borders, PIVOT_BORDER_INNER_LEFT),
421         0, 0, table_nr (table) - 1);
422       table_vline (
423         table, get_table_rule (pt->borders, PIVOT_BORDER_INNER_RIGHT),
424         table_nc (table), 0, table_nr (table) - 1);
425
426       if (stub[V])
427         table_hline (
428           table, get_table_rule (pt->borders, PIVOT_BORDER_DATA_TOP),
429           0, table_nc (table) - 1, stub[V]);
430       if (stub[H])
431         table_vline (
432           table, get_table_rule (pt->borders, PIVOT_BORDER_DATA_LEFT),
433           stub[H], 0, table_nr (table) - 1);
434
435     }
436   free (column_enumeration);
437   free (row_enumeration);
438
439   struct table_item *ti = table_item_create (table, NULL, NULL);
440
441   if (pt->title)
442     {
443       struct table_item_text *title = pivot_value_to_table_item_text (
444         pt->title, &pt->areas[PIVOT_AREA_TITLE], footnotes,
445         pt->show_values, pt->show_variables);
446       table_item_set_title (ti, title);
447       table_item_text_destroy (title);
448     }
449
450   const struct pivot_axis *layer_axis = &pt->axes[PIVOT_AXIS_LAYER];
451   struct table_item_layers *layers = NULL;
452   for (size_t i = 0; i < layer_axis->n_dimensions; i++)
453     {
454       const struct pivot_dimension *d = layer_axis->dimensions[i];
455       if (d->n_leaves)
456         {
457           if (!layers)
458             {
459               layers = xzalloc (sizeof *layers);
460               layers->style = area_style_override (
461                 NULL, &pt->areas[PIVOT_AREA_LAYERS], NULL, NULL);
462               layers->layers = xnmalloc (layer_axis->n_dimensions,
463                                          sizeof *layers->layers);
464             }
465
466           const struct pivot_value *name
467             = d->data_leaves[layer_indexes[i]]->name;
468           struct table_item_layer *layer = &layers->layers[layers->n_layers++];
469           struct string s = DS_EMPTY_INITIALIZER;
470           pivot_value_format_body (name, pt->show_values, pt->show_variables,
471                                    &s);
472           layer->content = ds_steal_cstr (&s);
473           layer->n_footnotes = name->n_footnotes;
474           layer->footnotes = xnmalloc (layer->n_footnotes,
475                                        sizeof *layer->footnotes);
476           for (size_t i = 0; i < name->n_footnotes; i++)
477             layer->footnotes[i] = footnotes[name->footnotes[i]->idx];
478         }
479     }
480   if (layers)
481     {
482       table_item_set_layers (ti, layers);
483       table_item_layers_destroy (layers);
484     }
485
486   if (pt->caption)
487     {
488       struct table_item_text *caption = pivot_value_to_table_item_text (
489         pt->caption, &pt->areas[PIVOT_AREA_CAPTION], footnotes,
490         pt->show_values, pt->show_variables);
491       table_item_set_caption (ti, caption);
492       table_item_text_destroy (caption);
493     }
494
495   free (footnotes);
496   table_item_submit (ti);
497 }
498
499 void
500 pivot_table_submit (struct pivot_table *pt)
501 {
502   pivot_table_assign_label_depth (CONST_CAST (struct pivot_table *, pt));
503
504   int old_decimal = settings_get_decimal_char (FMT_COMMA);
505   if (pt->decimal == '.' || pt->decimal == ',')
506     settings_set_decimal_char (pt->decimal);
507
508   if (pt->print_all_layers)
509     {
510       size_t *layer_indexes;
511
512       PIVOT_AXIS_FOR_EACH (layer_indexes, &pt->axes[PIVOT_AXIS_LAYER])
513         {
514           if (pt->paginate_layers)
515             text_item_submit (text_item_create (TEXT_ITEM_EJECT_PAGE, ""));
516           pivot_table_submit_layer (pt, layer_indexes);
517         }
518     }
519   else
520     pivot_table_submit_layer (pt, pt->current_layer);
521
522   settings_set_decimal_char (old_decimal);
523
524   pivot_table_unref (pt);
525 }