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-output.h"
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"
33 #include "gl/minmax.h"
34 #include "gl/xalloc.h"
40 pivot_output_next_layer (const struct pivot_table *pt, size_t *indexes,
43 const struct pivot_axis *layer_axis = &pt->axes[PIVOT_AXIS_LAYER];
44 if (print && pt->look->print_all_layers)
45 return pivot_axis_iterator_next (indexes, layer_axis);
48 size_t size = layer_axis->n_dimensions * sizeof *pt->current_layer;
49 return size ? xmemdup (pt->current_layer, size) : xmalloc (1);
58 static const struct pivot_category *
59 find_category (const struct pivot_dimension *d, int dim_index,
60 const size_t *indexes, int row_ofs)
62 size_t index = indexes[dim_index];
63 assert (index < d->n_leaves);
64 for (const struct pivot_category *c = d->presentation_leaves[index];
70 row_ofs -= 1 + c->extra_depth;
77 static struct table_area_style *
78 table_area_style_override (struct pool *pool,
79 const struct table_area_style *in,
80 const struct cell_style *cell_,
81 const struct font_style *font_,
84 const struct cell_style *cell = cell_ ? cell_ : &in->cell_style;
85 const struct font_style *font = font_ ? font_ : &in->font_style;
87 struct table_area_style *out = (pool
88 ? pool_alloc (pool, sizeof *out)
89 : xmalloc (sizeof *out));
90 *out = (struct table_area_style) {
91 .cell_style.halign = rotate_label ? TABLE_HALIGN_CENTER : cell->halign,
92 .cell_style.valign = rotate_label ? TABLE_VALIGN_CENTER : cell->valign,
93 .cell_style.decimal_offset = cell->decimal_offset,
94 .cell_style.margin[H][0] = cell->margin[H][0],
95 .cell_style.margin[H][1] = cell->margin[H][1],
96 .cell_style.margin[V][0] = cell->margin[V][0],
97 .cell_style.margin[V][1] = cell->margin[V][1],
98 .font_style.fg[0] = font->fg[0],
99 .font_style.fg[1] = font->fg[1],
100 .font_style.bg[0] = font->bg[0],
101 .font_style.bg[1] = font->bg[1],
102 .font_style.typeface = (font->typeface
103 ? pool_strdup (pool, font->typeface)
105 .font_style.size = font->size,
106 .font_style.bold = font->bold,
107 .font_style.italic = font->italic,
108 .font_style.underline = font->underline,
109 .font_style.markup = font->markup,
115 format_cell (const struct pivot_value *value, int style_idx,
116 enum settings_value_show show_values,
117 enum settings_value_show show_variables,
118 bool rotate_label, struct string *s)
120 int options = style_idx << TAB_STYLE_SHIFT;
123 bool numeric = pivot_value_format_body (value, show_values,
126 options |= TAB_NUMERIC;
127 if (value->font_style && value->font_style->markup)
128 options |= TAB_MARKUP;
130 options |= TAB_ROTATE;
136 fill_cell (struct table *t, int x1, int y1, int x2, int y2,
137 const struct table_area_style *style, int style_idx,
138 const struct pivot_value *value,
139 enum settings_value_show show_values,
140 enum settings_value_show show_variables,
143 struct string s = DS_EMPTY_INITIALIZER;
144 int options = format_cell (value, style_idx,
145 show_values, show_variables, rotate_label, &s);
146 table_joint_text (t, x1, y1, x2, y2, options, ds_cstr (&s));
151 if (value->cell_style || value->font_style || rotate_label)
152 table_add_style (t, x1, y1,
153 table_area_style_override (t->container, style,
158 table_add_footnotes (t, x1, y1,
159 (struct pivot_footnote **) value->footnotes,
162 if (value->n_subscripts)
163 table_add_subscripts (t, x1, y1,
164 value->subscripts, value->n_subscripts);
169 get_table_rule (const struct table_border_style *styles,
170 enum pivot_border style_idx)
172 return styles[style_idx].stroke | (style_idx << TAB_RULE_STYLE_SHIFT);
176 draw_line (struct table *t, const struct table_border_style *styles,
177 enum pivot_border style_idx,
178 enum table_axis axis, int a, int b0, int b1)
180 int rule = get_table_rule (styles, style_idx);
182 table_hline (t, rule, b0, b1, a);
184 table_vline (t, rule, a, b0, b1);
188 compose_headings (struct table *t,
189 const struct pivot_axis *a_axis, enum table_axis a,
190 const struct pivot_axis *b_axis,
191 const struct table_border_style *borders,
192 enum pivot_border dim_col_horz,
193 enum pivot_border dim_col_vert,
194 enum pivot_border cat_col_horz,
195 enum pivot_border cat_col_vert,
196 const size_t *column_enumeration, size_t n_columns,
197 const struct table_area_style *label_style,
199 const struct table_area_style *corner_style,
200 enum settings_value_show show_values,
201 enum settings_value_show show_variables,
202 bool rotate_inner_labels, bool rotate_outer_labels)
204 enum table_axis b = !a;
205 int b_size = a_axis->label_depth;
206 int a_ofs = b_axis->label_depth;
208 if (!a_axis->n_dimensions || !n_columns || !b_size)
211 int bottom_row = b_size - 1;
212 const int stride = MAX (1, a_axis->n_dimensions);
213 for (int dim_index = 0; dim_index < a_axis->n_dimensions; dim_index++)
215 const struct pivot_dimension *d = a_axis->dimensions[dim_index];
216 if (d->hide_all_labels)
219 for (int row_ofs = 0; row_ofs < d->label_depth; row_ofs++)
221 for (size_t x1 = 0; x1 < n_columns;)
223 const struct pivot_category *c = find_category (
224 d, dim_index, column_enumeration + x1 * stride, row_ofs);
232 for (x2 = x1 + 1; x2 < n_columns; x2++)
234 const struct pivot_category *c2 = find_category (
235 d, dim_index, column_enumeration + x2 * stride, row_ofs);
240 int y1 = bottom_row - row_ofs - c->extra_depth;
241 int y2 = bottom_row - row_ofs + 1;
242 bool is_outer_row = y1 == 0;
243 bool is_inner_row = y2 == b_size;
244 if (pivot_category_is_leaf (c) || c->show_label)
246 int bb[TABLE_N_AXES][2];
247 bb[a][0] = x1 + a_ofs;
248 bb[a][1] = x2 + a_ofs - 1;
251 bool rotate = ((rotate_inner_labels && is_inner_row)
252 || (rotate_outer_labels && is_outer_row));
253 fill_cell (t, bb[H][0], bb[V][0], bb[H][1], bb[V][1],
254 label_style, label_style_idx, c->name,
255 show_values, show_variables, rotate);
257 if (pivot_category_is_leaf (c) && x2 + 1 <= n_columns)
259 enum pivot_border style
260 = (y1 == 0 && a_axis->label_depth > d->label_depth
263 draw_line (t, borders, style, b, x2 + a_ofs, y1,
266 if (pivot_category_is_leaf (c) && x1 > 0)
268 enum pivot_border style
269 = (y1 == 0 && a_axis->label_depth > d->label_depth
272 draw_line (t, borders, style, b, x1 + a_ofs, y1,
276 if (c->parent && c->parent->show_label)
277 draw_line (t, borders, cat_col_horz, a, y1,
278 x1 + a_ofs, x2 + a_ofs - 1);
284 if (d->root->show_label_in_corner && a_ofs > 0)
286 int bb[TABLE_N_AXES][2];
288 bb[a][1] = a_ofs - 1;
289 bb[b][0] = bottom_row - d->label_depth + 1;
290 bb[b][1] = bottom_row;
291 fill_cell (t, bb[H][0], bb[V][0], bb[H][1], bb[V][1],
292 corner_style, PIVOT_AREA_CORNER, d->root->name,
293 show_values, show_variables, false);
297 draw_line (t, borders, dim_col_horz, a, bottom_row + 1, a_ofs,
300 bottom_row -= d->label_depth;
304 static struct table *
305 create_aux_table (const struct pivot_table *pt, int nc, int nr,
308 struct table *table = table_create (nr, nc, 0, 0, 0, 0);
309 table->styles[style_idx] = table_area_style_override (
310 table->container, &pt->look->areas[style_idx], NULL, NULL, false);
316 add_references (const struct pivot_table *pt, const struct table *table,
317 bool *refs, size_t *n_refs)
322 for (int y = 0; y < table->n[V]; y++)
323 for (int x = 0; x < table->n[H]; )
325 struct table_cell cell;
326 table_get_cell (table, x, y, &cell);
328 if (x == cell.d[H][0] && y == cell.d[V][0])
330 for (size_t i = 0; i < cell.n_footnotes; i++)
332 const struct pivot_footnote *f = cell.footnotes[i];
333 assert (f->idx < pt->n_footnotes);
334 assert (f == pt->footnotes[f->idx]);
344 x = cell.d[TABLE_HORZ][1];
348 static struct pivot_footnote **
349 collect_footnotes (const struct pivot_table *pt,
350 const struct table *title,
351 const struct table *layers,
352 const struct table *body,
353 const struct table *caption,
354 size_t *n_footnotesp)
356 if (!pt->n_footnotes)
362 bool *refs = xzalloc (pt->n_footnotes);
364 add_references (pt, title, refs, &n_refs);
365 add_references (pt, layers, refs, &n_refs);
366 add_references (pt, body, refs, &n_refs);
367 add_references (pt, caption, refs, &n_refs);
369 struct pivot_footnote **footnotes = xnmalloc (n_refs, sizeof *footnotes);
370 size_t n_footnotes = 0;
371 for (size_t i = 0; i < pt->n_footnotes; i++)
373 footnotes[n_footnotes++] = pt->footnotes[i];
374 assert (n_footnotes == n_refs);
378 *n_footnotesp = n_footnotes;
383 pivot_output (const struct pivot_table *pt,
384 const size_t *layer_indexes,
385 struct table **titlep,
386 struct table **layersp,
387 struct table **bodyp,
388 struct table **captionp,
389 struct table **footnotesp,
390 struct pivot_footnote ***fp, size_t *nfp)
392 const size_t *pindexes[PIVOT_N_AXES]
393 = { [PIVOT_AXIS_LAYER] = layer_indexes };
395 size_t data[TABLE_N_AXES];
396 size_t *column_enumeration = pivot_table_enumerate_axis (
397 pt, PIVOT_AXIS_COLUMN, layer_indexes, pt->look->omit_empty, &data[H]);
398 size_t *row_enumeration = pivot_table_enumerate_axis (
399 pt, PIVOT_AXIS_ROW, layer_indexes, pt->look->omit_empty, &data[V]);
401 int stub[TABLE_N_AXES] = {
402 [H] = pt->axes[PIVOT_AXIS_ROW].label_depth,
403 [V] = pt->axes[PIVOT_AXIS_COLUMN].label_depth,
405 struct table *body = table_create (data[H] + stub[H],
407 stub[H], 0, stub[V], 0);
408 for (size_t i = 0; i < PIVOT_N_AREAS; i++)
409 body->styles[i] = table_area_style_override (
410 body->container, &pt->look->areas[i], NULL, NULL, false);
412 for (size_t i = 0; i < PIVOT_N_BORDERS; i++)
414 const struct table_border_style *in = &pt->look->borders[i];
415 body->rule_colors[i] = pool_alloc (body->container,
416 sizeof *body->rule_colors[i]);
417 *body->rule_colors[i] = in->color;
420 compose_headings (body,
421 &pt->axes[PIVOT_AXIS_COLUMN], H, &pt->axes[PIVOT_AXIS_ROW],
423 PIVOT_BORDER_DIM_COL_HORZ,
424 PIVOT_BORDER_DIM_COL_VERT,
425 PIVOT_BORDER_CAT_COL_HORZ,
426 PIVOT_BORDER_CAT_COL_VERT,
427 column_enumeration, data[H],
428 &pt->look->areas[PIVOT_AREA_COLUMN_LABELS],
429 PIVOT_AREA_COLUMN_LABELS,
430 &pt->look->areas[PIVOT_AREA_CORNER],
431 pt->show_values, pt->show_variables,
432 pt->rotate_outer_row_labels, false);
434 compose_headings (body,
435 &pt->axes[PIVOT_AXIS_ROW], V, &pt->axes[PIVOT_AXIS_COLUMN],
437 PIVOT_BORDER_DIM_ROW_VERT,
438 PIVOT_BORDER_DIM_ROW_HORZ,
439 PIVOT_BORDER_CAT_ROW_VERT,
440 PIVOT_BORDER_CAT_ROW_HORZ,
441 row_enumeration, data[V],
442 &pt->look->areas[PIVOT_AREA_ROW_LABELS],
443 PIVOT_AREA_ROW_LABELS,
444 &pt->look->areas[PIVOT_AREA_CORNER],
445 pt->show_values, pt->show_variables,
446 false, pt->rotate_inner_column_labels);
448 size_t *dindexes = XCALLOC (pt->n_dimensions, size_t);
450 PIVOT_ENUMERATION_FOR_EACH (pindexes[PIVOT_AXIS_ROW], row_enumeration,
451 &pt->axes[PIVOT_AXIS_ROW])
454 PIVOT_ENUMERATION_FOR_EACH (pindexes[PIVOT_AXIS_COLUMN],
456 &pt->axes[PIVOT_AXIS_COLUMN])
458 pivot_table_convert_indexes_ptod (pt, pindexes, dindexes);
459 const struct pivot_value *value = pivot_table_get (pt, dindexes);
461 x + stub[H], y + stub[V],
462 x + stub[H], y + stub[V],
463 &pt->look->areas[PIVOT_AREA_DATA], PIVOT_AREA_DATA,
464 value, pt->show_values, pt->show_variables, false);
473 if ((pt->corner_text || !pt->look->row_labels_in_corner)
474 && stub[H] && stub[V])
475 fill_cell (body, 0, 0, stub[H] - 1, stub[V] - 1,
476 &pt->look->areas[PIVOT_AREA_CORNER], PIVOT_AREA_CORNER,
477 pt->corner_text, pt->show_values, pt->show_variables, false);
479 if (body->n[H] && body->n[V])
482 body, get_table_rule (pt->look->borders, PIVOT_BORDER_INNER_TOP),
483 0, body->n[H] - 1, 0);
485 body, get_table_rule (pt->look->borders, PIVOT_BORDER_INNER_BOTTOM),
486 0, body->n[H] - 1, body->n[V]);
488 body, get_table_rule (pt->look->borders, PIVOT_BORDER_INNER_LEFT),
489 0, 0, body->n[V] - 1);
491 body, get_table_rule (pt->look->borders, PIVOT_BORDER_INNER_RIGHT),
492 body->n[H], 0, body->n[V] - 1);
496 body, get_table_rule (pt->look->borders, PIVOT_BORDER_DATA_TOP),
497 0, body->n[H] - 1, stub[V]);
500 body, get_table_rule (pt->look->borders, PIVOT_BORDER_DATA_LEFT),
501 stub[H], 0, body->n[V] - 1);
504 free (column_enumeration);
505 free (row_enumeration);
509 if (pt->title && titlep)
511 title = create_aux_table (pt, 1, 1, PIVOT_AREA_TITLE);
512 fill_cell (title, 0, 0, 0, 0,
513 &pt->look->areas[PIVOT_AREA_TITLE], PIVOT_AREA_TITLE,
514 pt->title, pt->show_values, pt->show_variables, false);
520 const struct pivot_axis *layer_axis = &pt->axes[PIVOT_AXIS_LAYER];
523 for (size_t i = 0; i < layer_axis->n_dimensions; i++)
525 const struct pivot_dimension *d = layer_axis->dimensions[i];
530 struct table *layers;
533 layers = create_aux_table (pt, 1, n_layers, PIVOT_AREA_LAYERS);
535 for (size_t i = 0; i < layer_axis->n_dimensions; i++)
537 const struct pivot_dimension *d = layer_axis->dimensions[i];
541 /* XXX This puts in the layer values, but not the variable names. */
542 const struct pivot_value *name
543 = d->data_leaves[layer_indexes[i]]->name;
544 fill_cell (layers, 0, y, 0, y,
545 &pt->look->areas[PIVOT_AREA_LAYERS], PIVOT_AREA_LAYERS,
546 name, pt->show_values, pt->show_variables, false);
554 struct table *caption;
555 if (pt->caption && captionp)
557 caption = create_aux_table (pt, 1, 1, PIVOT_AREA_CAPTION);
558 fill_cell (caption, 0, 0, 0, 0,
559 &pt->look->areas[PIVOT_AREA_CAPTION], PIVOT_AREA_CAPTION,
560 pt->caption, pt->show_values, pt->show_variables, false);
567 struct pivot_footnote **f = collect_footnotes (pt, title, layers, body,
569 struct table *footnotes;
570 if (nf && footnotesp)
572 footnotes = create_aux_table (pt, 1, nf, PIVOT_AREA_FOOTER);
574 for (size_t i = 0; i < nf; i++)
576 struct string s = DS_EMPTY_INITIALIZER;
577 pivot_value_format (f[i]->marker, pt->show_values,
578 pt->show_variables, &s);
579 ds_put_cstr (&s, ". ");
580 pivot_value_format (f[i]->content, pt->show_values,
581 pt->show_variables, &s);
583 struct pivot_value *value = pivot_value_new_user_text_nocopy (
586 fill_cell (layers, 0, i, 0, i,
587 &pt->look->areas[PIVOT_AREA_FOOTER], PIVOT_AREA_FOOTER,
588 value, pt->show_values, pt->show_variables, false);
601 *footnotesp = footnotes;
612 pivot_table_submit (struct pivot_table *pt)
614 table_item_submit (table_item_create (pt));