/* PSPP - a program for statistical analysis.
- Copyright (C) 2009, 2010, 2011, 2013, 2014 Free Software Foundation, Inc.
+ Copyright (C) 2009, 2010, 2011, 2013, 2014, 2016 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
#include <math.h>
#include <stdio.h>
+#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "libpspp/hash-functions.h"
#include "libpspp/hmap.h"
#include "output/render.h"
+#include "output/tab.h"
#include "output/table-item.h"
#include "output/table.h"
#include "gl/minmax.h"
#include "gl/xalloc.h"
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+
/* This file uses TABLE_HORZ and TABLE_VERT enough to warrant abbreviating. */
#define H TABLE_HORZ
#define V TABLE_VERT
};
static struct render_page *render_page_create (const struct render_params *,
- const struct table *);
+ struct table *);
+struct render_page *render_page_ref (const struct render_page *page_);
static void render_page_unref (struct render_page *);
/* Returns the offset in struct render_page's cp[axis] array of the rule with
int overflow[TABLE_N_AXES][2];
};
-/* Returns a hash value for (X,Y). */
+/* Returns a hash value for (,Y). */
static unsigned int
-hash_overflow (int x, int y)
+hash_cell (int x, int y)
{
return hash_int (x + (y << 16), 0);
}
const struct render_overflow *of;
HMAP_FOR_EACH_WITH_HASH (of, struct render_overflow, node,
- hash_overflow (x, y), &page->overflows)
+ hash_cell (x, y), &page->overflows)
if (x == of->d[H] && y == of->d[V])
return of;
}
distribute_spanned_width (int width,
struct render_row *rows, const int *rules, int n)
{
- int total_unspanned;
- double w, d0, d1, d;
- int x;
-
/* Sum up the unspanned widths of the N rows for use as weights. */
- total_unspanned = 0;
- for (x = 0; x < n; x++)
+ int total_unspanned = 0;
+ for (int x = 0; x < n; x++)
total_unspanned += rows[x].unspanned;
- for (x = 0; x < n - 1; x++)
+ for (int x = 0; x < n - 1; x++)
total_unspanned += rules[x + 1];
if (total_unspanned >= width)
return;
unspanned weights when 'total_unspanned' is 0 (because that would cause a
division by zero).
- This implementation uses floating-point types and operators, but all the
- values involved are integers. For integers smaller than 53 bits, this
- should not lose any precision, and it should degrade gracefully for larger
- values.
-
The calculation we want to do is this:
w0 = width / n
the rule on the right. That way each rule contributes to both the cell on
its left and on its right.)
*/
- d0 = n;
- d1 = 2.0 * (total_unspanned > 0 ? total_unspanned : 1.0);
- d = d0 * d1;
+ long long int d0 = n;
+ long long int d1 = 2LL * MAX (total_unspanned, 1);
+ long long int d = d0 * d1;
if (total_unspanned > 0)
- d *= 2.0;
- w = floor (d / 2.0);
- for (x = 0; x < n; x++)
+ d *= 2;
+ long long int w = d / 2;
+ for (int x = 0; x < n; x++)
{
w += width * d1;
if (total_unspanned > 0)
{
- double unspanned = rows[x].unspanned * 2.0;
+ long long int unspanned = rows[x].unspanned * 2LL;
if (x < n - 1)
unspanned += rules[x + 1];
if (x > 0)
switch (type)
{
case TAL_0:
- case TAL_GAP:
return RENDER_LINE_NONE;
case TAL_1:
return RENDER_LINE_SINGLE;
for (d[b] = 0; d[b] < table->n[b]; d[b]++)
rules |= 1u << table_get_rule (table, a, d[H], d[V]);
+ /* Turn off TAL_NONE because it has width 0 and we needn't bother. However,
+ if the device doesn't support margins, make sure that there is at least a
+ small gap between cells (but we don't need any at the left or right edge
+ of the table). */
+ if (rules & (1u << TAL_0))
+ {
+ rules &= ~(1u << TAL_0);
+ if (z > 0 && z < table->n[a] && !params->supports_margins && a == H)
+ rules |= 1u << TAL_1;
+ }
+
/* Calculate maximum width of the rules that are present. */
width = 0;
if (rules & (1u << TAL_1)
- || (z > 0 && z < table->n[a] && rules & (1u << TAL_GAP)))
+ || (z > 0 && z < table->n[a] && rules & (1u << TAL_0)))
width = params->line_widths[a][RENDER_LINE_SINGLE];
if (rules & (1u << TAL_2))
width = MAX (width, params->line_widths[a][RENDER_LINE_DOUBLE]);
const struct render_row *rows_max,
int w_min, int w_max, const int *rules)
{
- /* This implementation uses floating-point types and operators, but all the
- values involved are integers. For integers smaller than 53 bits, this
- should not lose any precision, and it should degrade gracefully for larger
- values. */
const int n = table->n[H];
- const double avail = params->size[H] - w_min;
- const double wanted = w_max - w_min;
- struct render_page *page;
- double w;
- int *cph;
- int x;
+ const long long int avail = params->size[H] - w_min;
+ const long long int wanted = w_max - w_min;
assert (wanted > 0);
- page = render_page_allocate (params, table);
+ struct render_page *page = render_page_allocate (params, table);
- cph = page->cp[H];
+ int *cph = page->cp[H];
*cph = 0;
- w = (int) wanted / 2;
- for (x = 0; x < n; x++)
+ long long int w = wanted / 2;
+ for (int x = 0; x < n; x++)
{
- int extra;
-
w += avail * (rows_max[x].width - rows_min[x].width);
- extra = w / wanted;
+ int extra = w / wanted;
w -= extra * wanted;
cph[1] = cph[0] + rules[x];
size is PARAMS->size, but the caller is responsible for actually breaking it
up to fit on such a device, using the render_break abstraction. */
static struct render_page *
-render_page_create (const struct render_params *params,
- const struct table *table_)
+render_page_create (const struct render_params *params, struct table *table)
{
struct render_page *page;
- struct table *table;
enum { MIN, MAX };
struct render_row *columns[2];
struct render_row *rows;
int i;
enum table_axis axis;
- table = table_ref (table_);
nc = table_nc (table);
nr = table_nr (table);
struct table_cell cell;
table_get_cell (table, x, y, &cell);
- if (y == cell.d[V][0] && table_cell_colspan (&cell) == 1)
+ if (y == cell.d[V][0])
{
- int w[2];
- int i;
-
- params->measure_cell_width (params->aux, &cell, &w[MIN], &w[MAX]);
- for (i = 0; i < 2; i++)
- if (columns[i][x].unspanned < w[i])
- columns[i][x].unspanned = w[i];
+ if (table_cell_colspan (&cell) == 1)
+ {
+ int w[2];
+ int i;
+
+ params->measure_cell_width (params->aux, &cell,
+ &w[MIN], &w[MAX]);
+ for (i = 0; i < 2; i++)
+ if (columns[i][x].unspanned < w[i])
+ columns[i][x].unspanned = w[i];
+ }
}
x = cell.d[H][1];
table_cell_free (&cell);
table_cell_free (&cell);
}
+ /* In pathological cases, spans can cause the minimum width of a column to
+ exceed the maximum width. This bollixes our interpolation algorithm
+ later, so fix it up. */
+ for (i = 0; i < nc; i++)
+ if (columns[MIN][i].width > columns[MAX][i].width)
+ columns[MAX][i].width = columns[MIN][i].width;
+
/* Decide final column widths. */
for (i = 0; i < 2; i++)
table_widths[i] = calculate_table_width (table_nc (table),
return page;
}
+/* Increases PAGE's reference count. */
+struct render_page *
+render_page_ref (const struct render_page *page_)
+{
+ struct render_page *page = CONST_CAST (struct render_page *, page_);
+ page->ref_cnt++;
+ return page;
+}
+
/* Decreases PAGE's reference count and destroys PAGE if this causes the
reference count to fall to zero. */
static void
hmap_destroy (&page->overflows);
table_unref (page->table);
-
+
for (i = 0; i < TABLE_N_AXES; ++i)
{
free (page->join_crossing[i]);
/* Returns the size of PAGE along AXIS. (This might be larger than the page
size specified in the parameters passed to render_page_create(). Use a
render_break to break up a render_page into page-sized chunks.) */
-int
+static int
render_page_get_size (const struct render_page *page, enum table_axis axis)
{
return page->cp[axis][page->n[axis] * 2 + 1];
}
-int
+static int
render_page_get_best_breakpoint (const struct render_page *page, int height)
{
int y;
return !(z & 1);
}
+bool
+render_direction_rtl (void)
+{
+ /* TRANSLATORS: Do not translate this string. If the script of your language
+ reads from right to left (eg Persian, Arabic, Hebrew etc), then replace
+ this string with "output-direction-rtl". Otherwise either leave it
+ untranslated or copy it verbatim. */
+ const char *dir = _("output-direction-ltr");
+ if ( 0 == strcmp ("output-direction-rtl", dir))
+ return true;
+
+ if ( 0 != strcmp ("output-direction-ltr", dir))
+ fprintf (stderr, "This localisation has been incorrectly translated. Complain to the translator.\n");
+
+ return false;
+}
+
static void
-render_rule (const struct render_page *page, const int d[TABLE_N_AXES])
+render_rule (const struct render_page *page, const int ofs[TABLE_N_AXES],
+ const int d[TABLE_N_AXES])
{
enum render_line_style styles[TABLE_N_AXES][2];
enum table_axis a;
{
int bb[TABLE_N_AXES][2];
- bb[H][0] = page->cp[H][d[H]];
- bb[H][1] = page->cp[H][d[H] + 1];
- bb[V][0] = page->cp[V][d[V]];
- bb[V][1] = page->cp[V][d[V] + 1];
+ bb[H][0] = ofs[H] + page->cp[H][d[H]];
+ bb[H][1] = ofs[H] + page->cp[H][d[H] + 1];
+ if (render_direction_rtl ())
+ {
+ int temp = bb[H][0];
+ bb[H][0] = render_page_get_size (page, H) - bb[H][1];
+ bb[H][1] = render_page_get_size (page, H) - temp;
+ }
+ bb[V][0] = ofs[V] + page->cp[V][d[V]];
+ bb[V][1] = ofs[V] + page->cp[V][d[V] + 1];
page->params->draw_line (page->params->aux, bb, styles);
}
}
static void
-render_cell (const struct render_page *page, const struct table_cell *cell)
+render_cell (const struct render_page *page, const int ofs[TABLE_N_AXES],
+ const struct table_cell *cell)
{
const struct render_overflow *of;
int bb[TABLE_N_AXES][2];
int clip[TABLE_N_AXES][2];
- bb[H][0] = clip[H][0] = page->cp[H][cell->d[H][0] * 2 + 1];
- bb[H][1] = clip[H][1] = page->cp[H][cell->d[H][1] * 2];
- bb[V][0] = clip[V][0] = page->cp[V][cell->d[V][0] * 2 + 1];
- bb[V][1] = clip[V][1] = page->cp[V][cell->d[V][1] * 2];
+ bb[H][0] = clip[H][0] = ofs[H] + page->cp[H][cell->d[H][0] * 2 + 1];
+ bb[H][1] = clip[H][1] = ofs[H] + page->cp[H][cell->d[H][1] * 2];
+ if (render_direction_rtl ())
+ {
+ int temp = bb[H][0];
+ bb[H][0] = clip[H][0] = render_page_get_size (page, H) - bb[H][1];
+ bb[H][1] = clip[H][1] = render_page_get_size (page, H) - temp;
+ }
+ bb[V][0] = clip[V][0] = ofs[V] + page->cp[V][cell->d[V][0] * 2 + 1];
+ bb[V][1] = clip[V][1] = ofs[V] + page->cp[V][cell->d[V][1] * 2];
of = find_overflow (page, cell->d[H][0], cell->d[V][0]);
if (of)
{
bb[axis][0] -= of->overflow[axis][0];
if (cell->d[axis][0] == 0 && !page->is_edge_cutoff[axis][0])
- clip[axis][0] = page->cp[axis][cell->d[axis][0] * 2];
+ clip[axis][0] = ofs[axis] + page->cp[axis][cell->d[axis][0] * 2];
}
if (of->overflow[axis][1])
{
bb[axis][1] += of->overflow[axis][1];
if (cell->d[axis][1] == page->n[axis] && !page->is_edge_cutoff[axis][1])
- clip[axis][1] = page->cp[axis][cell->d[axis][1] * 2 + 1];
+ clip[axis][1] = ofs[axis] + page->cp[axis][cell->d[axis][1] * 2 + 1];
}
}
}
/* Draws the cells of PAGE indicated in BB. */
static void
render_page_draw_cells (const struct render_page *page,
- int bb[TABLE_N_AXES][2])
+ int ofs[TABLE_N_AXES], int bb[TABLE_N_AXES][2])
{
int x, y;
int d[TABLE_N_AXES];
d[H] = x;
d[V] = y;
- render_rule (page, d);
+ render_rule (page, ofs, d);
x++;
}
else
table_get_cell (page->table, x / 2, y / 2, &cell);
if (y / 2 == bb[V][0] / 2 || y / 2 == cell.d[V][0])
- render_cell (page, &cell);
+ render_cell (page, ofs, &cell);
x = rule_ofs (cell.d[H][1]);
table_cell_free (&cell);
}
/* Renders PAGE, by calling the 'draw_line' and 'draw_cell' functions from the
render_params provided to render_page_create(). */
-void
-render_page_draw (const struct render_page *page)
+static void
+render_page_draw (const struct render_page *page, int ofs[TABLE_N_AXES])
{
int bb[TABLE_N_AXES][2];
bb[V][0] = 0;
bb[V][1] = page->n[V] * 2 + 1;
- render_page_draw_cells (page, bb);
+ render_page_draw_cells (page, ofs, bb);
}
/* Returns the greatest value i, 0 <= i < n, such that cp[i] <= x0. */
/* Renders the cells of PAGE that intersect (X,Y)-(X+W,Y+H), by calling the
'draw_line' and 'draw_cell' functions from the render_params provided to
render_page_create(). */
-void
+static void
render_page_draw_region (const struct render_page *page,
- int x, int y, int w, int h)
+ int ofs[TABLE_N_AXES], int clip[TABLE_N_AXES][2])
{
int bb[TABLE_N_AXES][2];
- bb[H][0] = get_clip_min_extent (x, page->cp[H], page->n[H] * 2 + 1);
- bb[H][1] = get_clip_max_extent (x + w, page->cp[H], page->n[H] * 2 + 1);
- bb[V][0] = get_clip_min_extent (y, page->cp[V], page->n[V] * 2 + 1);
- bb[V][1] = get_clip_max_extent (y + h, page->cp[V], page->n[V] * 2 + 1);
+ bb[H][0] = get_clip_min_extent (clip[H][0], page->cp[H], page->n[H] * 2 + 1);
+ bb[H][1] = get_clip_max_extent (clip[H][1], page->cp[H], page->n[H] * 2 + 1);
+ bb[V][0] = get_clip_min_extent (clip[V][0], page->cp[V], page->n[V] * 2 + 1);
+ bb[V][1] = get_clip_max_extent (clip[V][1], page->cp[V], page->n[V] * 2 + 1);
- render_page_draw_cells (page, bb);
+ render_page_draw_cells (page, ofs, bb);
}
-\f
+
/* Breaking up tables to fit on a page. */
/* An iterator for breaking render_pages into smaller chunks. */
int z1, int p1);
/* Initializes render_break B for breaking PAGE along AXIS.
-
- Ownership of PAGE is transferred to B. The caller must use
- render_page_ref() if it needs to keep a copy of PAGE. */
+ Takes ownership of PAGE. */
static void
render_break_init (struct render_break *b, struct render_page *page,
enum table_axis axis)
struct render_pager
{
- int width;
- struct render_page *page;
+ const struct render_params *params;
+
+ struct render_page **pages;
+ size_t n_pages, allocated_pages;
+
+ size_t cur_page;
struct render_break x_break;
struct render_break y_break;
};
-/* Creates and returns a new render_pager for breaking PAGE into smaller
- chunks. Takes ownership of PAGE. */
+static const struct render_page *
+render_pager_add_table (struct render_pager *p, struct table *table)
+{
+ struct render_page *page;
+
+ if (p->n_pages >= p->allocated_pages)
+ p->pages = x2nrealloc (p->pages, &p->allocated_pages, sizeof *p->pages);
+ page = p->pages[p->n_pages++] = render_page_create (p->params, table);
+ return page;
+}
+
+static void
+render_pager_start_page (struct render_pager *p)
+{
+ render_break_init (&p->x_break, render_page_ref (p->pages[p->cur_page++]),
+ H);
+ render_break_init_empty (&p->y_break);
+}
+
+static void
+add_footnote_page (struct render_pager *p, const struct table_item *item)
+{
+ const struct footnote **f;
+ size_t n_footnotes = table_collect_footnotes (item, &f);
+ if (!n_footnotes)
+ return;
+
+ struct tab_table *t = tab_create (2, n_footnotes);
+
+ for (size_t i = 0; i < n_footnotes; i++)
+ if (f[i])
+ {
+ tab_text_format (t, 0, i, TAB_LEFT, "%s.", f[i]->marker);
+ tab_text (t, 1, i, TAB_LEFT, f[i]->content);
+ }
+ render_pager_add_table (p, &t->table);
+
+ free (f);
+}
+
+static void
+add_text_page (struct render_pager *p, const struct table_item_text *t)
+{
+ if (!t)
+ return;
+
+ struct tab_table *tab = tab_create (1, 1);
+ tab_text (tab, 0, 0, TAB_LEFT, t->content);
+ for (size_t i = 0; i < t->n_footnotes; i++)
+ tab_add_footnote (tab, 0, 0, t->footnotes[i]);
+ render_pager_add_table (p, &tab->table);
+}
+
+/* Creates and returns a new render_pager for rendering TABLE_ITEM on the
+ device with the given PARAMS. */
struct render_pager *
render_pager_create (const struct render_params *params,
const struct table_item *table_item)
{
- struct render_pager *p = xmalloc (sizeof *p);
- p->width = params->size[H];
- p->page = render_page_create (params, table_item_get_table (table_item));
- render_break_init (&p->x_break, p->page, H);
- render_break_init_empty (&p->y_break);
+ struct render_pager *p;
+
+ p = xzalloc (sizeof *p);
+ p->params = params;
+
+ /* Title. */
+ add_text_page (p, table_item_get_title (table_item));
+
+ /* Body. */
+ render_pager_add_table (p, table_ref (table_item_get_table (table_item)));
+
+ /* Caption. */
+ add_text_page (p, table_item_get_caption (table_item));
+
+ /* Footnotes. */
+ add_footnote_page (p, table_item);
+
+ render_pager_start_page (p);
+
return p;
}
{
if (p)
{
+ size_t i;
+
render_break_destroy (&p->x_break);
render_break_destroy (&p->y_break);
- render_page_unref (p->page);
+ for (i = 0; i < p->n_pages; i++)
+ render_page_unref (p->pages[i]);
+ free (p->pages);
free (p);
}
}
while (!render_break_has_next (&p->y_break))
{
render_break_destroy (&p->y_break);
- if (render_break_has_next (&p->x_break))
+ if (!render_break_has_next (&p->x_break))
{
- struct render_page *x_slice;
-
- x_slice = render_break_next (&p->x_break, p->width);
- render_break_init (&p->y_break, x_slice, V);
+ render_break_destroy (&p->x_break);
+ if (p->cur_page >= p->n_pages)
+ {
+ render_break_init_empty (&p->x_break);
+ render_break_init_empty (&p->y_break);
+ return false;
+ }
+ render_pager_start_page (p);
}
else
- {
- render_break_init_empty (&p->y_break);
- return false;
- }
+ render_break_init (&p->y_break,
+ render_break_next (&p->x_break, p->params->size[H]), V);
}
return true;
}
int
render_pager_draw_next (struct render_pager *p, int space)
{
- struct render_page *page = (render_pager_has_next (p)
- ? render_break_next (&p->y_break, space)
- : NULL);
- if (page)
+ int ofs[TABLE_N_AXES] = { 0, 0 };
+ size_t start_page = SIZE_MAX;
+
+ while (render_pager_has_next (p))
{
- int used = render_page_get_size (page, V);
+ struct render_page *page;
- render_page_draw (page);
+ if (start_page == p->cur_page)
+ break;
+ start_page = p->cur_page;
+
+ page = render_break_next (&p->y_break, space - ofs[V]);
+ if (!page)
+ break;
+
+ render_page_draw (page, ofs);
+ ofs[V] += render_page_get_size (page, V);
render_page_unref (page);
- return used;
}
- else
- return 0;
+ return ofs[V];
}
/* Draws all of P's content. */
void
render_pager_draw (const struct render_pager *p)
{
- render_page_draw (p->page);
+ render_pager_draw_region (p, 0, 0, INT_MAX, INT_MAX);
}
/* Draws the region of P's content that lies in the region (X,Y)-(X+W,Y+H).
render_pager_draw_region (const struct render_pager *p,
int x, int y, int w, int h)
{
- render_page_draw_region (p->page, x, y, w, h);
+ int ofs[TABLE_N_AXES] = { 0, 0 };
+ int clip[TABLE_N_AXES][2];
+ size_t i;
+
+ clip[H][0] = x;
+ clip[H][1] = x + w;
+ for (i = 0; i < p->n_pages; i++)
+ {
+ const struct render_page *page = p->pages[i];
+ int size = render_page_get_size (page, V);
+
+ clip[V][0] = MAX (y, ofs[V]) - ofs[V];
+ clip[V][1] = MIN (y + h, ofs[V] + size) - ofs[V];
+ if (clip[V][1] > clip[V][0])
+ render_page_draw_region (page, ofs, clip);
+
+ ofs[V] += size;
+ }
}
/* Returns the size of P's content along AXIS; i.e. the content's width if AXIS
int
render_pager_get_size (const struct render_pager *p, enum table_axis axis)
{
- return render_page_get_size (p->page, axis);
+ int size = 0;
+ size_t i;
+
+ for (i = 0; i < p->n_pages; i++)
+ {
+ int subsize = render_page_get_size (p->pages[i], axis);
+ size = axis == H ? MAX (size, subsize) : size + subsize;
+ }
+
+ return size;
}
int
render_pager_get_best_breakpoint (const struct render_pager *p, int height)
{
- return render_page_get_best_breakpoint (p->page, height);
+ int y = 0;
+ size_t i;
+
+ for (i = 0; i < p->n_pages; i++)
+ {
+ int size = render_page_get_size (p->pages[i], V);
+ if (y + size >= height)
+ return render_page_get_best_breakpoint (p->pages[i], height - y) + y;
+ y += size;
+ }
+
+ return height;
}
\f
/* render_page_select() and helpers. */
const struct table_cell *);
/* Creates and returns a new render_page whose contents are a subregion of
- PAGE's contents. The new render_page includes cells Z0 through Z1 along
- AXIS, plus any headers on AXIS.
+ PAGE's contents. The new render_page includes cells Z0 through Z1
+ (exclusive) along AXIS, plus any headers on AXIS.
If P0 is nonzero, then it is a number of pixels to exclude from the left or
top (according to AXIS) of cell Z0. Similarly, P1 is a number of pixels to
of = xzalloc (sizeof *of);
cell_to_subpage (s, cell, of->d);
hmap_insert (&s->subpage->overflows, &of->node,
- hash_overflow (of->d[H], of->d[V]));
+ hash_cell (of->d[H], of->d[V]));
old = find_overflow (s->page, cell->d[H][0], cell->d[V][0]);
if (old != NULL)