+/* PSPP - a program for statistical analysis.
+ Copyright (C) 2009, 2010, 2014, 2020 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
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+#include "output/cairo-pager.h"
+
+#include <math.h>
+#include <pango/pango-layout.h>
+#include <pango/pangocairo.h>
+
+#include "output/driver-provider.h"
+
+#include "gl/xalloc.h"
+
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+\f
+struct xr_page_style *
+xr_page_style_ref (const struct xr_page_style *ps_)
+{
+ struct xr_page_style *ps = CONST_CAST (struct xr_page_style *, ps_);
+ assert (ps->ref_cnt > 0);
+ ps->ref_cnt++;
+ return ps;
+}
+
+void
+xr_page_style_unref (struct xr_page_style *ps)
+{
+ if (ps)
+ {
+ assert (ps->ref_cnt > 0);
+ if (!--ps->ref_cnt)
+ {
+ for (int i = 0; i < 2; i++)
+ page_heading_uninit (&ps->headings[i]);
+ if (ps->font)
+ pango_font_description_free (ps->font);
+ free (ps);
+ }
+ }
+}
+
+static bool
+pfd_equals (const PangoFontDescription *a,
+ const PangoFontDescription *b)
+{
+ return a && b ? pango_font_description_equal (a, b) : !a && !b;
+}
+
+bool
+xr_page_style_equals (const struct xr_page_style *a,
+ const struct xr_page_style *b)
+{
+ for (int i = 0; i < TABLE_N_AXES; i++)
+ {
+ if (a->size[i] != b->size[i])
+ return false;
+
+ for (int j = 0; j < 2; j++)
+ if (a->margins[i][j] != b->margins[i][j])
+ return false;
+ }
+
+ for (int i = 0; i < 2; i++)
+ if (!page_heading_equals (&a->headings[i], &b->headings[i]))
+ return false;
+
+ return (pfd_equals (a->font, b->font)
+ && a->font_scale == b->font_scale
+ && a->initial_page_number == b->initial_page_number
+ && a->object_spacing == b->object_spacing);
+}
+\f
+struct xr_pager
+ {
+ struct xr_page_style *page_style;
+ int page_index;
+ int heading_heights[2];
+
+ /* Current output item. */
+ struct xr_fsm *fsm;
+ struct xr_fsm_style *fsm_style;
+ struct output_item *item;
+
+ /* Current output page. */
+ cairo_t *cr;
+ int y;
+ };
+
+static void xr_pager_run (struct xr_pager *);
+
+/* Conversions to and from points. */
+static double
+xr_to_pt (int x)
+{
+ return x / (double) XR_POINT;
+}
+
+static int
+pango_to_xr (int pango)
+{
+ return (XR_POINT != PANGO_SCALE
+ ? ceil (pango * (1. * XR_POINT / PANGO_SCALE))
+ : pango);
+}
+
+static int
+xr_to_pango (int xr)
+{
+ return (XR_POINT != PANGO_SCALE
+ ? ceil (xr * (1. / XR_POINT * PANGO_SCALE))
+ : xr);
+}
+
+static int
+get_layout_height (PangoLayout *layout)
+{
+ int w, h;
+ pango_layout_get_size (layout, &w, &h);
+ return h;
+}
+
+static int
+xr_render_page_heading (cairo_t *cairo, const PangoFontDescription *font,
+ const struct page_heading *ph, int page_number,
+ int width, bool draw, int base_y)
+{
+ PangoLayout *layout = pango_cairo_create_layout (cairo);
+ pango_layout_set_font_description (layout, font);
+
+ int y = 0;
+ for (size_t i = 0; i < ph->n; i++)
+ {
+ const struct page_paragraph *pp = &ph->paragraphs[i];
+
+ char *markup = output_driver_substitute_heading_vars (pp->markup,
+ page_number);
+ pango_layout_set_markup (layout, markup, -1);
+ free (markup);
+
+ pango_layout_set_alignment (
+ layout,
+ (pp->halign == TABLE_HALIGN_LEFT ? PANGO_ALIGN_LEFT
+ : pp->halign == TABLE_HALIGN_CENTER ? PANGO_ALIGN_CENTER
+ : pp->halign == TABLE_HALIGN_MIXED ? PANGO_ALIGN_LEFT
+ : PANGO_ALIGN_RIGHT));
+ pango_layout_set_width (layout, xr_to_pango (width));
+ if (draw)
+ {
+ cairo_save (cairo);
+ cairo_translate (cairo, 0, xr_to_pt (y + base_y));
+ pango_cairo_show_layout (cairo, layout);
+ cairo_restore (cairo);
+ }
+
+ y += pango_to_xr (get_layout_height (layout));
+ }
+
+ g_object_unref (G_OBJECT (layout));
+
+ return y;
+}
+
+static void
+xr_measure_headings (struct xr_pager *p)
+{
+ const struct xr_page_style *ps = p->page_style;
+
+ cairo_surface_t *surface = cairo_recording_surface_create (
+ CAIRO_CONTENT_COLOR, NULL);
+ cairo_t *cairo = cairo_create (surface);
+ for (int i = 0; i < 2; i++)
+ {
+ int h = xr_render_page_heading (cairo, ps->font, &ps->headings[i], -1,
+ ps->size[TABLE_HORZ], false, 0);
+
+ /* If the heading is nonempty, add a margin above or below it. */
+ if (h)
+ h += ps->object_spacing;
+
+ p->heading_heights[i] = h;
+ }
+ cairo_destroy (cairo);
+ cairo_surface_destroy (surface);
+
+ int total = p->heading_heights[0] + p->heading_heights[1];
+ if (total >= ps->size[TABLE_VERT])
+ p->heading_heights[0] = p->heading_heights[1] = 0;
+}
+
+struct xr_pager *
+xr_pager_create (const struct xr_page_style *ps)
+{
+ struct xr_pager *p = xmalloc (sizeof *p);
+ *p = (struct xr_pager) { .page_style = xr_page_style_ref (ps) };
+
+ xr_measure_headings (p);
+
+ return p;
+}
+
+void
+xr_pager_destroy (struct xr_pager *p)
+{
+ if (p)
+ {
+ xr_fsm_destroy (p->fsm);
+ xr_fsm_style_unref (p->fsm_style);
+ output_item_unref (p->item);
+
+ xr_page_style_unref (p->page_style);
+ if (p->cr)
+ cairo_destroy (p->cr);
+ free (p);
+ }
+}
+
+bool
+xr_pager_has_item (const struct xr_pager *p)
+{
+ return p->item != NULL;
+}
+
+void
+xr_pager_add_item (struct xr_pager *p, const struct xr_fsm_style *fsm_style,
+ const struct output_item *item)
+{
+ assert (!p->item);
+ p->item = output_item_ref (item);
+ p->fsm_style = xr_fsm_style_ref (fsm_style);
+ xr_pager_run (p);
+}
+
+bool
+xr_pager_has_page (const struct xr_pager *p)
+{
+ return p->cr;
+}
+
+void
+xr_pager_add_page (struct xr_pager *p, cairo_t *cr)
+{
+ assert (!p->cr);
+ p->cr = cr;
+ p->y = 0;
+
+ const struct xr_page_style *ps = p->page_style;
+ const struct cell_color *bg = &ps->bg;
+ if (bg->alpha)
+ {
+ cairo_save (cr);
+ cairo_set_source_rgba (cr, bg->r / 255.0, bg->g / 255.0,
+ bg->b / 255.0, bg->alpha / 255.0);
+ cairo_rectangle (cr, 0, 0, ps->size[TABLE_HORZ], ps->size[TABLE_VERT]);
+ cairo_fill (cr);
+ cairo_restore (cr);
+ }
+ cairo_translate (cr,
+ xr_to_pt (ps->margins[TABLE_HORZ][0]),
+ xr_to_pt (ps->margins[TABLE_VERT][0]));
+
+ int page_number = p->page_index++ + ps->initial_page_number;
+ if (p->heading_heights[0])
+ xr_render_page_heading (cr, ps->font, &ps->headings[0], page_number,
+ ps->size[TABLE_HORZ], true, 0);
+
+ if (p->heading_heights[1])
+ xr_render_page_heading (
+ cr, ps->font, &ps->headings[1], page_number,
+ ps->size[TABLE_HORZ], true,
+ ps->size[TABLE_VERT] - p->heading_heights[1] + ps->object_spacing);
+
+ cairo_translate (cr, 0, xr_to_pt (p->heading_heights[0]));
+
+ xr_pager_run (p);
+}
+
+bool
+xr_pager_needs_new_page (struct xr_pager *p)
+{
+ int vsize = (p->page_style->size[TABLE_VERT]
+ - p->heading_heights[0] - p->heading_heights[1]);
+ if (p->item && p->y >= vsize)
+ {
+ cairo_destroy (p->cr);
+ p->cr = NULL;
+ return true;
+ }
+ else
+ return false;
+}
+
+static void
+xr_pager_run (struct xr_pager *p)
+{
+ int vsize = (p->page_style->size[TABLE_VERT]
+ - p->heading_heights[0] - p->heading_heights[1]);
+ if (p->item && p->cr && p->y < vsize)
+ {
+ if (!p->fsm)
+ {
+ /* XXX fsm_style needs to account for heading_heights */
+ p->fsm = xr_fsm_create (p->item, p->fsm_style, p->cr);
+ if (!p->fsm)
+ {
+ xr_fsm_style_unref (p->fsm_style);
+ p->fsm_style = NULL;
+ output_item_unref (p->item);
+ p->item = NULL;
+
+ return;
+ }
+ }
+
+ for (;;)
+ {
+ int spacing = p->page_style->object_spacing;
+ int chunk = xr_fsm_draw_slice (p->fsm, p->cr, vsize - p->y);
+ p->y += chunk + spacing;
+ cairo_translate (p->cr, 0, xr_to_pt (chunk + spacing));
+
+ if (xr_fsm_is_empty (p->fsm))
+ {
+ xr_fsm_destroy (p->fsm);
+ p->fsm = NULL;
+ xr_fsm_style_unref (p->fsm_style);
+ p->fsm_style = NULL;
+ output_item_unref (p->item);
+ p->item = NULL;
+ return;
+ }
+ else if (!chunk)
+ {
+ assert (p->y > 0);
+ p->y = INT_MAX;
+ return;
+ }
+ }
+ }
+}