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 /* This file uses TABLE_HORZ and TABLE_VERT enough to warrant abbreviating. */
36 struct xr_page_style *
37 xr_page_style_ref (const struct xr_page_style *ps_)
39 struct xr_page_style *ps = CONST_CAST (struct xr_page_style *, ps_);
40 assert (ps->ref_cnt > 0);
45 struct xr_page_style *
46 xr_page_style_unshare (struct xr_page_style *old)
48 assert (old->ref_cnt > 0);
49 if (old->ref_cnt == 1)
52 xr_page_style_unref (old);
54 struct xr_page_style *new = xmemdup (old, sizeof *old);
56 for (int i = 0; i < 2; i++)
57 page_heading_copy (&new->headings[i], &old->headings[i]);
63 xr_page_style_unref (struct xr_page_style *ps)
67 assert (ps->ref_cnt > 0);
70 for (int i = 0; i < 2; i++)
71 page_heading_uninit (&ps->headings[i]);
78 xr_page_style_equals (const struct xr_page_style *a,
79 const struct xr_page_style *b)
81 for (int i = 0; i < TABLE_N_AXES; i++)
82 for (int j = 0; j < 2; j++)
83 if (a->margins[i][j] != b->margins[i][j])
86 for (int i = 0; i < 2; i++)
87 if (!page_heading_equals (&a->headings[i], &b->headings[i]))
90 return (a->initial_page_number == b->initial_page_number
91 && a->object_spacing == b->object_spacing);
96 struct xr_page_style *page_style;
97 struct xr_fsm_style *fsm_style;
99 int heading_heights[2];
101 /* Current output item. */
103 struct output_item *item;
105 /* Current output page. */
110 static void xr_pager_run (struct xr_pager *);
112 /* Conversions to and from points. */
116 return x / (double) XR_POINT;
120 pango_to_xr (int pango)
122 return (XR_POINT != PANGO_SCALE
123 ? ceil (pango * (1. * XR_POINT / PANGO_SCALE))
130 return (XR_POINT != PANGO_SCALE
131 ? ceil (xr * (1. / XR_POINT * PANGO_SCALE))
136 get_layout_height (PangoLayout *layout)
139 pango_layout_get_size (layout, &w, &h);
144 xr_render_page_heading (cairo_t *cairo, const PangoFontDescription *font,
145 const struct page_heading *ph, int page_number,
146 int width, int base_y, double font_resolution)
148 PangoContext *context = pango_cairo_create_context (cairo);
149 pango_cairo_context_set_resolution (context, font_resolution);
150 PangoLayout *layout = pango_layout_new (context);
151 g_object_unref (context);
153 pango_layout_set_font_description (layout, font);
156 for (size_t i = 0; i < ph->n; i++)
158 const struct page_paragraph *pp = &ph->paragraphs[i];
160 char *markup = output_driver_substitute_heading_vars (pp->markup,
162 pango_layout_set_markup (layout, markup, -1);
165 pango_layout_set_alignment (
167 (pp->halign == TABLE_HALIGN_LEFT ? PANGO_ALIGN_LEFT
168 : pp->halign == TABLE_HALIGN_CENTER ? PANGO_ALIGN_CENTER
169 : pp->halign == TABLE_HALIGN_MIXED ? PANGO_ALIGN_LEFT
170 : PANGO_ALIGN_RIGHT));
171 pango_layout_set_width (layout, xr_to_pango (width));
174 cairo_translate (cairo, 0, xr_to_pt (y + base_y));
175 pango_cairo_show_layout (cairo, layout);
176 cairo_restore (cairo);
178 y += pango_to_xr (get_layout_height (layout));
181 g_object_unref (G_OBJECT (layout));
187 xr_measure_headings (const struct xr_page_style *ps,
188 const struct xr_fsm_style *fs,
189 int heading_heights[2])
191 cairo_surface_t *surface = cairo_recording_surface_create (
192 CAIRO_CONTENT_COLOR, NULL);
193 cairo_t *cairo = cairo_create (surface);
194 for (int i = 0; i < 2; i++)
196 int *h = &heading_heights[i];
197 *h = xr_render_page_heading (cairo, fs->fonts[XR_FONT_PROPORTIONAL],
198 &ps->headings[i], -1, fs->size[H], 0,
199 fs->font_resolution);
201 *h += ps->object_spacing;
203 cairo_destroy (cairo);
204 cairo_surface_destroy (surface);
208 xr_pager_create (const struct xr_page_style *ps_,
209 const struct xr_fsm_style *fs_)
211 struct xr_page_style *ps = xr_page_style_ref (ps_);
212 struct xr_fsm_style *fs = xr_fsm_style_ref (fs_);
214 int heading_heights[2];
215 xr_measure_headings (ps, fs, heading_heights);
216 int total = heading_heights[0] + heading_heights[1];
217 if (total > 0 && total < fs->size[V])
219 fs = xr_fsm_style_unshare (fs);
220 ps = xr_page_style_unshare (ps);
222 for (int i = 0; i < 2; i++)
223 ps->margins[V][i] += heading_heights[i];
224 fs->size[V] -= total;
227 struct xr_pager *p = xmalloc (sizeof *p);
228 *p = (struct xr_pager) { .page_style = ps, .fsm_style = fs };
233 xr_pager_destroy (struct xr_pager *p)
237 xr_page_style_unref (p->page_style);
238 xr_fsm_style_unref (p->fsm_style);
240 xr_fsm_destroy (p->fsm);
241 output_item_unref (p->item);
245 cairo_restore (p->cr);
246 cairo_destroy (p->cr);
253 xr_pager_has_item (const struct xr_pager *p)
255 return p->item != NULL;
259 xr_pager_add_item (struct xr_pager *p, const struct output_item *item)
262 p->item = output_item_ref (item);
267 xr_pager_has_page (const struct xr_pager *p)
273 xr_pager_add_page (struct xr_pager *p, cairo_t *cr)
280 const struct xr_fsm_style *fs = p->fsm_style;
281 const struct xr_page_style *ps = p->page_style;
282 const struct cell_color *bg = &ps->bg;
286 cairo_set_source_rgba (cr, bg->r / 255.0, bg->g / 255.0,
287 bg->b / 255.0, bg->alpha / 255.0);
288 cairo_rectangle (cr, 0, 0, fs->size[H], fs->size[V]);
293 xr_to_pt (ps->margins[H][0]),
294 xr_to_pt (ps->margins[V][0]));
296 const PangoFontDescription *font = fs->fonts[XR_FONT_PROPORTIONAL];
297 int page_number = p->page_index++ + ps->initial_page_number;
298 if (p->heading_heights[0])
299 xr_render_page_heading (cr, font, &ps->headings[0], page_number,
300 fs->size[H], -p->heading_heights[0],
301 fs->font_resolution);
303 if (p->heading_heights[1])
304 xr_render_page_heading (cr, font, &ps->headings[1], page_number,
305 fs->size[H], fs->size[V] + ps->object_spacing,
306 fs->font_resolution);
312 xr_pager_needs_new_page (struct xr_pager *p)
314 if (p->item && (!p->cr || p->y >= p->fsm_style->size[V]))
318 cairo_restore (p->cr);
319 cairo_destroy (p->cr);
329 xr_pager_run (struct xr_pager *p)
331 if (p->item && p->cr && p->y < p->fsm_style->size[V])
335 p->fsm = xr_fsm_create (p->item, p->fsm_style, p->cr);
338 output_item_unref (p->item);
347 int spacing = p->page_style->object_spacing;
348 int chunk = xr_fsm_draw_slice (p->fsm, p->cr,
349 p->fsm_style->size[V] - p->y);
350 p->y += chunk + spacing;
351 cairo_translate (p->cr, 0, xr_to_pt (chunk + spacing));
353 if (xr_fsm_is_empty (p->fsm))
355 xr_fsm_destroy (p->fsm);
357 output_item_unref (p->item);