1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2018 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/>. */
21 #include "output/pivot-table.h"
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"
31 #include "gl/minmax.h"
32 #include "gl/xalloc.h"
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)
41 size_t index = indexes[dim_index];
42 assert (index < d->n_leaves);
43 for (const struct pivot_category *c = d->presentation_leaves[index];
49 row_ofs -= 1 + c->extra_depth;
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_)
62 const struct cell_style *cell = cell_ ? cell_ : &in->cell_style;
63 const struct font_style *font = font_ ? font_ : &in->font_style;
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)
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,
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,
101 struct string s = DS_EMPTY_INITIALIZER;
102 int opts = style_idx << TAB_STYLE_SHIFT;
105 bool numeric = pivot_value_format_body (value, show_values,
109 if (value->font_style && value->font_style->markup)
114 table_joint_text (t, x1, y1, x2, y2, opts, ds_cstr (&s));
119 if (value->cell_style || value->font_style)
120 table_add_style (t, x1, y1,
121 area_style_override (t->container, style,
125 for (size_t i = 0; i < value->n_footnotes; i++)
126 table_add_footnote (t, x1, y1, footnotes[value->footnotes[i]->idx]);
128 if (value->n_subscripts)
129 table_add_subscripts (t, x1, y1,
130 value->subscripts, value->n_subscripts);
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)
144 struct string s = DS_EMPTY_INITIALIZER;
145 pivot_value_format_body (value, show_values, show_variables, &s);
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),
156 for (size_t i = 0; i < value->n_footnotes; i++)
157 text->footnotes[i] = footnotes[value->footnotes[i]->idx];
163 get_table_rule (const struct table_border_style *styles,
164 enum pivot_border style_idx)
166 return styles[style_idx].stroke | (style_idx << TAB_RULE_STYLE_SHIFT);
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)
174 int rule = get_table_rule (styles, style_idx);
176 table_hline (t, rule, b0, b1, a);
178 table_vline (t, rule, a, b0, b1);
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)
198 enum table_axis b = !a;
199 int b_size = a_axis->label_depth;
200 int a_ofs = b_axis->label_depth;
202 if (!a_axis->n_dimensions || !n_columns || !b_size)
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++)
209 const struct pivot_dimension *d = a_axis->dimensions[dim_index];
210 if (d->hide_all_labels)
213 for (int row_ofs = 0; row_ofs < d->label_depth; row_ofs++)
215 for (size_t x1 = 0; x1 < n_columns; )
217 const struct pivot_category *c = find_category (
218 d, dim_index, column_enumeration + x1 * stride, row_ofs);
226 for (x2 = x1 + 1; x2 < n_columns; x2++)
228 const struct pivot_category *c2 = find_category (
229 d, dim_index, column_enumeration + x2 * stride, row_ofs);
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)
240 int bb[TABLE_N_AXES][2];
241 bb[a][0] = x1 + a_ofs;
242 bb[a][1] = x2 + a_ofs - 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);
251 if (pivot_category_is_leaf (c) && x2 + 1 <= n_columns)
253 enum pivot_border style
254 = (y1 == 0 && a_axis->label_depth > d->label_depth
257 draw_line (t, borders, style, b, x2 + a_ofs, y1,
260 if (pivot_category_is_leaf (c) && x1 > 0)
262 enum pivot_border style
263 = (y1 == 0 && a_axis->label_depth > d->label_depth
266 draw_line (t, borders, style, b, x1 + a_ofs, y1,
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);
278 if (d->root->show_label_in_corner && a_ofs > 0)
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);
291 draw_line (t, borders, dim_col_horz, a, bottom_row + 1, a_ofs,
294 bottom_row -= d->label_depth;
299 pivot_table_submit_layer (const struct pivot_table *pt,
300 const size_t *layer_indexes)
302 const size_t *pindexes[PIVOT_N_AXES]
303 = { [PIVOT_AXIS_LAYER] = layer_indexes };
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]);
311 int stub[TABLE_N_AXES] = {
312 [H] = pt->axes[PIVOT_AXIS_ROW].label_depth,
313 [V] = pt->axes[PIVOT_AXIS_COLUMN].label_depth,
315 struct table *table = table_create (body[H] + stub[H],
317 stub[H], 0, stub[V], 0);
319 for (size_t i = 0; i < PIVOT_N_AREAS; i++)
320 table->styles[i] = area_style_override (table->container, &pt->areas[i],
323 for (size_t i = 0; i < PIVOT_N_BORDERS; i++)
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;
335 struct footnote **footnotes = xcalloc (pt->n_footnotes, sizeof *footnotes);
336 for (size_t i = 0; i < pt->n_footnotes; i++)
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));
351 compose_headings (table,
352 &pt->axes[PIVOT_AXIS_COLUMN], H, &pt->axes[PIVOT_AXIS_ROW],
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);
365 compose_headings (table,
366 &pt->axes[PIVOT_AXIS_ROW], V, &pt->axes[PIVOT_AXIS_COLUMN],
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);
379 size_t *dindexes = xcalloc (pt->n_dimensions, sizeof *dindexes);
381 PIVOT_ENUMERATION_FOR_EACH (pindexes[PIVOT_AXIS_ROW], row_enumeration,
382 &pt->axes[PIVOT_AXIS_ROW])
385 PIVOT_ENUMERATION_FOR_EACH (pindexes[PIVOT_AXIS_COLUMN],
387 &pt->axes[PIVOT_AXIS_COLUMN])
389 pivot_table_convert_indexes_ptod (pt, pindexes, dindexes);
390 const struct pivot_value *value = pivot_table_get (pt, dindexes);
392 x + stub[H], y + stub[V],
393 x + stub[H], y + stub[V],
394 &pt->areas[PIVOT_AREA_DATA], PIVOT_AREA_DATA,
396 pt->show_values, pt->show_variables, false);
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);
411 if (table_nc (table) && table_nr (table))
414 table, get_table_rule (pt->borders, PIVOT_BORDER_INNER_TOP),
415 0, table_nc (table) - 1, 0);
417 table, get_table_rule (pt->borders, PIVOT_BORDER_INNER_BOTTOM),
418 0, table_nc (table) - 1, table_nr (table));
420 table, get_table_rule (pt->borders, PIVOT_BORDER_INNER_LEFT),
421 0, 0, table_nr (table) - 1);
423 table, get_table_rule (pt->borders, PIVOT_BORDER_INNER_RIGHT),
424 table_nc (table), 0, table_nr (table) - 1);
428 table, get_table_rule (pt->borders, PIVOT_BORDER_DATA_TOP),
429 0, table_nc (table) - 1, stub[V]);
432 table, get_table_rule (pt->borders, PIVOT_BORDER_DATA_LEFT),
433 stub[H], 0, table_nr (table) - 1);
436 free (column_enumeration);
437 free (row_enumeration);
439 struct table_item *ti = table_item_create (table, NULL, NULL);
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);
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++)
454 const struct pivot_dimension *d = layer_axis->dimensions[i];
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);
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,
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];
482 table_item_set_layers (ti, layers);
483 table_item_layers_destroy (layers);
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);
496 table_item_submit (ti);
500 pivot_table_submit (struct pivot_table *pt)
502 pivot_table_assign_label_depth (CONST_CAST (struct pivot_table *, pt));
504 int old_decimal = settings_get_decimal_char (FMT_COMMA);
505 if (pt->decimal == '.' || pt->decimal == ',')
506 settings_set_decimal_char (pt->decimal);
508 if (pt->print_all_layers)
510 size_t *layer_indexes;
512 PIVOT_AXIS_FOR_EACH (layer_indexes, &pt->axes[PIVOT_AXIS_LAYER])
514 if (pt->paginate_layers)
515 text_item_submit (text_item_create (TEXT_ITEM_EJECT_PAGE, ""));
516 pivot_table_submit_layer (pt, layer_indexes);
520 pivot_table_submit_layer (pt, pt->current_layer);
522 settings_set_decimal_char (old_decimal);
524 pivot_table_unref (pt);