1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2010, 2014, 2020 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/>. */
19 #include "output/cairo-pager.h"
22 #include <pango/pango-layout.h>
23 #include <pango/pangocairo.h>
25 #include "output/driver-provider.h"
27 #include "gl/xalloc.h"
30 #define _(msgid) gettext (msgid)
32 struct xr_page_style *
33 xr_page_style_ref (const struct xr_page_style *ps_)
35 struct xr_page_style *ps = CONST_CAST (struct xr_page_style *, ps_);
36 assert (ps->ref_cnt > 0);
42 xr_page_style_unref (struct xr_page_style *ps)
46 assert (ps->ref_cnt > 0);
49 for (int i = 0; i < 2; i++)
50 page_heading_uninit (&ps->headings[i]);
52 pango_font_description_free (ps->font);
59 pfd_equals (const PangoFontDescription *a,
60 const PangoFontDescription *b)
62 return a && b ? pango_font_description_equal (a, b) : !a && !b;
66 xr_page_style_equals (const struct xr_page_style *a,
67 const struct xr_page_style *b)
69 for (int i = 0; i < TABLE_N_AXES; i++)
71 if (a->size[i] != b->size[i])
74 for (int j = 0; j < 2; j++)
75 if (a->margins[i][j] != b->margins[i][j])
79 for (int i = 0; i < 2; i++)
80 if (!page_heading_equals (&a->headings[i], &b->headings[i]))
83 return (pfd_equals (a->font, b->font)
84 && a->font_scale == b->font_scale
85 && a->initial_page_number == b->initial_page_number
86 && a->object_spacing == b->object_spacing);
91 struct xr_page_style *page_style;
93 int heading_heights[2];
95 /* Current output item. */
97 struct xr_fsm_style *fsm_style;
98 struct output_item *item;
100 /* Current output page. */
105 static void xr_pager_run (struct xr_pager *);
107 /* Conversions to and from points. */
111 return x / (double) XR_POINT;
115 pango_to_xr (int pango)
117 return (XR_POINT != PANGO_SCALE
118 ? ceil (pango * (1. * XR_POINT / PANGO_SCALE))
125 return (XR_POINT != PANGO_SCALE
126 ? ceil (xr * (1. / XR_POINT * PANGO_SCALE))
131 get_layout_height (PangoLayout *layout)
134 pango_layout_get_size (layout, &w, &h);
139 xr_render_page_heading (cairo_t *cairo, const PangoFontDescription *font,
140 const struct page_heading *ph, int page_number,
141 int width, bool draw, int base_y)
143 PangoLayout *layout = pango_cairo_create_layout (cairo);
144 pango_layout_set_font_description (layout, font);
147 for (size_t i = 0; i < ph->n; i++)
149 const struct page_paragraph *pp = &ph->paragraphs[i];
151 char *markup = output_driver_substitute_heading_vars (pp->markup,
153 pango_layout_set_markup (layout, markup, -1);
156 pango_layout_set_alignment (
158 (pp->halign == TABLE_HALIGN_LEFT ? PANGO_ALIGN_LEFT
159 : pp->halign == TABLE_HALIGN_CENTER ? PANGO_ALIGN_CENTER
160 : pp->halign == TABLE_HALIGN_MIXED ? PANGO_ALIGN_LEFT
161 : PANGO_ALIGN_RIGHT));
162 pango_layout_set_width (layout, xr_to_pango (width));
166 cairo_translate (cairo, 0, xr_to_pt (y + base_y));
167 pango_cairo_show_layout (cairo, layout);
168 cairo_restore (cairo);
171 y += pango_to_xr (get_layout_height (layout));
174 g_object_unref (G_OBJECT (layout));
180 xr_measure_headings (struct xr_pager *p)
182 const struct xr_page_style *ps = p->page_style;
184 cairo_surface_t *surface = cairo_recording_surface_create (
185 CAIRO_CONTENT_COLOR, NULL);
186 cairo_t *cairo = cairo_create (surface);
187 for (int i = 0; i < 2; i++)
189 int h = xr_render_page_heading (cairo, ps->font, &ps->headings[i], -1,
190 ps->size[TABLE_HORZ], false, 0);
192 /* If the heading is nonempty, add a margin above or below it. */
194 h += ps->object_spacing;
196 p->heading_heights[i] = h;
198 cairo_destroy (cairo);
199 cairo_surface_destroy (surface);
201 int total = p->heading_heights[0] + p->heading_heights[1];
202 if (total >= ps->size[TABLE_VERT])
203 p->heading_heights[0] = p->heading_heights[1] = 0;
207 xr_pager_create (const struct xr_page_style *ps)
209 struct xr_pager *p = xmalloc (sizeof *p);
210 *p = (struct xr_pager) { .page_style = xr_page_style_ref (ps) };
212 xr_measure_headings (p);
218 xr_pager_destroy (struct xr_pager *p)
222 xr_fsm_destroy (p->fsm);
223 xr_fsm_style_unref (p->fsm_style);
224 output_item_unref (p->item);
226 xr_page_style_unref (p->page_style);
228 cairo_destroy (p->cr);
234 xr_pager_has_item (const struct xr_pager *p)
236 return p->item != NULL;
240 xr_pager_add_item (struct xr_pager *p, const struct xr_fsm_style *fsm_style,
241 const struct output_item *item)
244 p->item = output_item_ref (item);
245 p->fsm_style = xr_fsm_style_ref (fsm_style);
250 xr_pager_has_page (const struct xr_pager *p)
256 xr_pager_add_page (struct xr_pager *p, cairo_t *cr)
262 const struct xr_page_style *ps = p->page_style;
263 const struct cell_color *bg = &ps->bg;
267 cairo_set_source_rgba (cr, bg->r / 255.0, bg->g / 255.0,
268 bg->b / 255.0, bg->alpha / 255.0);
269 cairo_rectangle (cr, 0, 0, ps->size[TABLE_HORZ], ps->size[TABLE_VERT]);
274 xr_to_pt (ps->margins[TABLE_HORZ][0]),
275 xr_to_pt (ps->margins[TABLE_VERT][0]));
277 int page_number = p->page_index++ + ps->initial_page_number;
278 if (p->heading_heights[0])
279 xr_render_page_heading (cr, ps->font, &ps->headings[0], page_number,
280 ps->size[TABLE_HORZ], true, 0);
282 if (p->heading_heights[1])
283 xr_render_page_heading (
284 cr, ps->font, &ps->headings[1], page_number,
285 ps->size[TABLE_HORZ], true,
286 ps->size[TABLE_VERT] - p->heading_heights[1] + ps->object_spacing);
288 cairo_translate (cr, 0, xr_to_pt (p->heading_heights[0]));
294 xr_pager_needs_new_page (struct xr_pager *p)
296 int vsize = (p->page_style->size[TABLE_VERT]
297 - p->heading_heights[0] - p->heading_heights[1]);
298 if (p->item && p->y >= vsize)
300 cairo_destroy (p->cr);
309 xr_pager_run (struct xr_pager *p)
311 int vsize = (p->page_style->size[TABLE_VERT]
312 - p->heading_heights[0] - p->heading_heights[1]);
313 if (p->item && p->cr && p->y < vsize)
317 /* XXX fsm_style needs to account for heading_heights */
318 p->fsm = xr_fsm_create (p->item, p->fsm_style, p->cr);
321 xr_fsm_style_unref (p->fsm_style);
323 output_item_unref (p->item);
332 int spacing = p->page_style->object_spacing;
333 int chunk = xr_fsm_draw_slice (p->fsm, p->cr, vsize - p->y);
334 p->y += chunk + spacing;
335 cairo_translate (p->cr, 0, xr_to_pt (chunk + spacing));
337 if (xr_fsm_is_empty (p->fsm))
339 xr_fsm_destroy (p->fsm);
341 xr_fsm_style_unref (p->fsm_style);
343 output_item_unref (p->item);