9cdb569d479cd32847e347187a6ae9dc96b6b404
[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 void
96 fill_cell (struct table *t, int x1, int y1, int x2, int y2,
97            const struct table_area_style *style, int style_idx,
98            const struct pivot_value *value, struct footnote **footnotes,
99            enum settings_value_show show_values,
100            enum settings_value_show show_variables,
101            bool rotate_label)
102 {
103
104   struct string s = DS_EMPTY_INITIALIZER;
105   int opts = style_idx << TAB_STYLE_SHIFT;
106   if (value)
107     {
108       bool numeric = pivot_value_format_body (value, show_values,
109                                               show_variables, &s);
110       if (numeric)
111         opts |= TAB_NUMERIC;
112       if (value->font_style && value->font_style->markup)
113         opts |= TAB_MARKUP;
114       if (rotate_label)
115         opts |= TAB_ROTATE;
116     }
117   table_joint_text (t, x1, y1, x2, y2, opts, ds_cstr (&s));
118   ds_destroy (&s);
119
120   if (value)
121     {
122       if (value->cell_style || value->font_style || rotate_label)
123         table_add_style (t, x1, y1,
124                          table_area_style_override (t->container, style,
125                                                     value->cell_style,
126                                                     value->font_style,
127                                                     rotate_label));
128
129       for (size_t i = 0; i < value->n_footnotes; i++)
130         {
131           struct footnote *f = footnotes[value->footnotes[i]->idx];
132           if (f)
133             table_add_footnote (t, x1, y1, f);
134         }
135
136       if (value->n_subscripts)
137         table_add_subscripts (t, x1, y1,
138                               value->subscripts, value->n_subscripts);
139     }
140 }
141
142 <<<<<<< HEAD
143 static struct table_item_text *
144 pivot_value_to_table_item_text (const struct pivot_value *value,
145                                 const struct table_area_style *area,
146                                 struct footnote **footnotes,
147                                 enum settings_value_show show_values,
148                                 enum settings_value_show show_variables)
149 {
150   if (!value)
151     return NULL;
152
153   struct string s = DS_EMPTY_INITIALIZER;
154   pivot_value_format_body (value, show_values, show_variables, &s);
155
156   struct table_item_text *text = xmalloc (sizeof *text);
157   *text = (struct table_item_text) {
158     .content = ds_steal_cstr (&s),
159     .footnotes = xnmalloc (value->n_footnotes, sizeof *text->footnotes),
160     .style = table_area_style_override (
161       NULL, area, value->cell_style, value->font_style, false),
162   };
163
164   for (size_t i = 0; i < value->n_footnotes; i++)
165     {
166       struct footnote *f = footnotes[value->footnotes[i]->idx];
167       if (f)
168         text->footnotes[text->n_footnotes++] = f;
169     }
170
171   return text;
172 }
173
174 =======
175 >>>>>>> 8b935a7c95 (work toward getting rid of struct table in table_item)
176 static int
177 get_table_rule (const struct table_border_style *styles,
178                 enum pivot_border style_idx)
179 {
180   return styles[style_idx].stroke | (style_idx << TAB_RULE_STYLE_SHIFT);
181 }
182
183 static void
184 draw_line (struct table *t, const struct table_border_style *styles,
185            enum pivot_border style_idx,
186            enum table_axis axis, int a, int b0, int b1)
187 {
188   int rule = get_table_rule (styles, style_idx);
189   if (axis == H)
190     table_hline (t, rule, b0, b1, a);
191   else
192     table_vline (t, rule, a, b0, b1);
193 }
194
195 static void
196 compose_headings (struct table *t,
197                   const struct pivot_axis *a_axis, enum table_axis a,
198                   const struct pivot_axis *b_axis,
199                   const struct table_border_style *borders,
200                   enum pivot_border dim_col_horz,
201                   enum pivot_border dim_col_vert,
202                   enum pivot_border cat_col_horz,
203                   enum pivot_border cat_col_vert,
204                   const size_t *column_enumeration, size_t n_columns,
205                   const struct table_area_style *label_style,
206                   int label_style_idx,
207                   const struct table_area_style *corner_style,
208                   struct footnote **footnotes,
209                   enum settings_value_show show_values,
210                   enum settings_value_show show_variables,
211                   bool rotate_inner_labels, bool rotate_outer_labels)
212 {
213   enum table_axis b = !a;
214   int b_size = a_axis->label_depth;
215   int a_ofs = b_axis->label_depth;
216
217   if (!a_axis->n_dimensions || !n_columns || !b_size)
218     return;
219
220   int bottom_row = b_size - 1;
221   const int stride = MAX (1, a_axis->n_dimensions);
222   for (int dim_index = 0; dim_index < a_axis->n_dimensions; dim_index++)
223     {
224       const struct pivot_dimension *d = a_axis->dimensions[dim_index];
225       if (d->hide_all_labels)
226         continue;
227
228       for (int row_ofs = 0; row_ofs < d->label_depth; row_ofs++)
229         {
230           for (size_t x1 = 0; x1 < n_columns;)
231             {
232               const struct pivot_category *c = find_category (
233                 d, dim_index, column_enumeration + x1 * stride, row_ofs);
234               if (!c)
235                 {
236                   x1++;
237                   continue;
238                 }
239
240               size_t x2;
241               for (x2 = x1 + 1; x2 < n_columns; x2++)
242                 {
243                   const struct pivot_category *c2 = find_category (
244                     d, dim_index, column_enumeration + x2 * stride, row_ofs);
245                   if (c != c2)
246                     break;
247                 }
248
249               int y1 = bottom_row - row_ofs - c->extra_depth;
250               int y2 = bottom_row - row_ofs + 1;
251               bool is_outer_row = y1 == 0;
252               bool is_inner_row = y2 == b_size;
253               if (pivot_category_is_leaf (c) || c->show_label)
254                 {
255                   int bb[TABLE_N_AXES][2];
256                   bb[a][0] = x1 + a_ofs;
257                   bb[a][1] = x2 + a_ofs - 1;
258                   bb[b][0] = y1;
259                   bb[b][1] = y2 - 1;
260                   bool rotate = ((rotate_inner_labels && is_inner_row)
261                                  || (rotate_outer_labels && is_outer_row));
262                   fill_cell (t, bb[H][0], bb[V][0], bb[H][1], bb[V][1],
263                              label_style, label_style_idx, c->name, footnotes,
264                              show_values, show_variables, rotate);
265
266                   if (pivot_category_is_leaf (c) && x2 + 1 <= n_columns)
267                     {
268                       enum pivot_border style
269                         = (y1 == 0 && a_axis->label_depth > d->label_depth
270                            ? dim_col_vert
271                            : cat_col_vert);
272                       draw_line (t, borders, style, b, x2 + a_ofs, y1,
273                                  t->n[b] - 1);
274                     }
275                   if (pivot_category_is_leaf (c) && x1 > 0)
276                     {
277                       enum pivot_border style
278                         = (y1 == 0 && a_axis->label_depth > d->label_depth
279                            ? dim_col_vert
280                            : cat_col_vert);
281                       draw_line (t, borders, style, b, x1 + a_ofs, y1,
282                                  t->n[b] - 1);
283                     }
284                 }
285               if (c->parent && c->parent->show_label)
286                 draw_line (t, borders, cat_col_horz, a, y1,
287                            x1 + a_ofs, x2 + a_ofs - 1);
288
289               x1 = x2;
290             }
291         }
292
293       if (d->root->show_label_in_corner && a_ofs > 0)
294         {
295           int bb[TABLE_N_AXES][2];
296           bb[a][0] = 0;
297           bb[a][1] = a_ofs - 1;
298           bb[b][0] = bottom_row - d->label_depth + 1;
299           bb[b][1] = bottom_row;
300           fill_cell (t, bb[H][0], bb[V][0], bb[H][1], bb[V][1],
301                      corner_style, PIVOT_AREA_CORNER, d->root->name, footnotes,
302                      show_values, show_variables, false);
303         }
304
305       if (dim_index > 1)
306         draw_line (t, borders, dim_col_horz, a, bottom_row + 1, a_ofs,
307                    t->n[a] - 1);
308
309       bottom_row -= d->label_depth;
310     }
311 }
312
313 struct table *
314 pivot_table_to_table (const struct pivot_table *pt,
315                       const size_t *layer_indexes)
316 {
317   const size_t *pindexes[PIVOT_N_AXES]
318     = { [PIVOT_AXIS_LAYER] = layer_indexes };
319
320   size_t body[TABLE_N_AXES];
321   size_t *column_enumeration = pivot_table_enumerate_axis (
322     pt, PIVOT_AXIS_COLUMN, layer_indexes, pt->look->omit_empty, &body[H]);
323   size_t *row_enumeration = pivot_table_enumerate_axis (
324     pt, PIVOT_AXIS_ROW, layer_indexes, pt->look->omit_empty, &body[V]);
325
326   int stub[TABLE_N_AXES] = {
327     [H] = pt->axes[PIVOT_AXIS_ROW].label_depth,
328     [V] = pt->axes[PIVOT_AXIS_COLUMN].label_depth,
329   };
330   struct table *table = table_create (body[H] + stub[H],
331                                       body[V] + stub[V],
332                                       stub[H], 0, stub[V], 0);
333
334   for (size_t i = 0; i < PIVOT_N_AREAS; i++)
335     table->styles[i] = table_area_style_override (
336       table->container, &pt->look->areas[i], NULL, NULL, false);
337
338   for (size_t i = 0; i < PIVOT_N_BORDERS; i++)
339     {
340       const struct table_border_style *in = &pt->look->borders[i];
341       table->rule_colors[i] = pool_alloc (table->container,
342                                           sizeof *table->rule_colors[i]);
343       struct cell_color *out = table->rule_colors[i];
344       out->alpha = in->color.alpha;
345       out->r = in->color.r;
346       out->g = in->color.g;
347       out->b = in->color.b;
348     }
349
350   struct footnote **footnotes = XCALLOC (pt->n_footnotes,  struct footnote *);
351   for (size_t i = 0; i < pt->n_footnotes; i++)
352     {
353       const struct pivot_footnote *pf = pt->footnotes[i];
354
355       if (!pf->show)
356         continue;
357
358       char *content = pivot_value_to_string (pf->content, pt->show_values,
359                                              pt->show_variables);
360       char *marker = pivot_value_to_string (pf->marker, pt->show_values,
361                                             pt->show_variables);
362       footnotes[i] = table_create_footnote (
363         table, i, content, marker,
364         table_area_style_override (table->container,
365                                    &pt->look->areas[PIVOT_AREA_FOOTER],
366                                    pf->content->cell_style,
367                                    pf->content->font_style,
368                                    false));
369       free (marker);
370       free (content);
371     }
372
373   compose_headings (table,
374                     &pt->axes[PIVOT_AXIS_COLUMN], H, &pt->axes[PIVOT_AXIS_ROW],
375                     pt->look->borders,
376                     PIVOT_BORDER_DIM_COL_HORZ,
377                     PIVOT_BORDER_DIM_COL_VERT,
378                     PIVOT_BORDER_CAT_COL_HORZ,
379                     PIVOT_BORDER_CAT_COL_VERT,
380                     column_enumeration, body[H],
381                     &pt->look->areas[PIVOT_AREA_COLUMN_LABELS],
382                     PIVOT_AREA_COLUMN_LABELS,
383                     &pt->look->areas[PIVOT_AREA_CORNER], footnotes,
384                     pt->show_values, pt->show_variables,
385                     pt->rotate_outer_row_labels, false);
386
387   compose_headings (table,
388                     &pt->axes[PIVOT_AXIS_ROW], V, &pt->axes[PIVOT_AXIS_COLUMN],
389                     pt->look->borders,
390                     PIVOT_BORDER_DIM_ROW_VERT,
391                     PIVOT_BORDER_DIM_ROW_HORZ,
392                     PIVOT_BORDER_CAT_ROW_VERT,
393                     PIVOT_BORDER_CAT_ROW_HORZ,
394                     row_enumeration, body[V],
395                     &pt->look->areas[PIVOT_AREA_ROW_LABELS],
396                     PIVOT_AREA_ROW_LABELS,
397                     &pt->look->areas[PIVOT_AREA_CORNER], footnotes,
398                     pt->show_values, pt->show_variables,
399                     false, pt->rotate_inner_column_labels);
400
401   size_t *dindexes = XCALLOC (pt->n_dimensions, size_t);
402   size_t y = 0;
403   PIVOT_ENUMERATION_FOR_EACH (pindexes[PIVOT_AXIS_ROW], row_enumeration,
404                               &pt->axes[PIVOT_AXIS_ROW])
405     {
406       size_t x = 0;
407       PIVOT_ENUMERATION_FOR_EACH (pindexes[PIVOT_AXIS_COLUMN],
408                                   column_enumeration,
409                                   &pt->axes[PIVOT_AXIS_COLUMN])
410         {
411           pivot_table_convert_indexes_ptod (pt, pindexes, dindexes);
412           const struct pivot_value *value = pivot_table_get (pt, dindexes);
413           fill_cell (table,
414                      x + stub[H], y + stub[V],
415                      x + stub[H], y + stub[V],
416                      &pt->look->areas[PIVOT_AREA_DATA], PIVOT_AREA_DATA,
417                      value, footnotes,
418                      pt->show_values, pt->show_variables, false);
419
420           x++;
421         }
422
423       y++;
424     }
425   free (dindexes);
426
427   if ((pt->corner_text || !pt->look->row_labels_in_corner)
428       && stub[H] && stub[V])
429     fill_cell (table, 0, 0, stub[H] - 1, stub[V] - 1,
430                &pt->look->areas[PIVOT_AREA_CORNER], PIVOT_AREA_CORNER,
431                pt->corner_text, footnotes,
432                pt->show_values, pt->show_variables, false);
433
434   if (table->n[H] && table->n[V])
435     {
436       table_hline (
437         table, get_table_rule (pt->look->borders, PIVOT_BORDER_INNER_TOP),
438         0, table->n[H] - 1, 0);
439       table_hline (
440         table, get_table_rule (pt->look->borders, PIVOT_BORDER_INNER_BOTTOM),
441         0, table->n[H] - 1, table->n[V]);
442       table_vline (
443         table, get_table_rule (pt->look->borders, PIVOT_BORDER_INNER_LEFT),
444         0, 0, table->n[V] - 1);
445       table_vline (
446         table, get_table_rule (pt->look->borders, PIVOT_BORDER_INNER_RIGHT),
447         table->n[H], 0, table->n[V] - 1);
448
449       if (stub[V])
450         table_hline (
451           table, get_table_rule (pt->look->borders, PIVOT_BORDER_DATA_TOP),
452           0, table->n[H] - 1, stub[V]);
453       if (stub[H])
454         table_vline (
455           table, get_table_rule (pt->look->borders, PIVOT_BORDER_DATA_LEFT),
456           stub[H], 0, table->n[V] - 1);
457
458     }
459   free (column_enumeration);
460   free (row_enumeration);
461
462   free (footnotes);
463
464   return table;
465 }
466
467 void
468 pivot_table_submit (struct pivot_table *pt)
469 {
470   table_item_submit (table_item_create (pt));
471 }