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 <cairo/cairo-pdf.h>
23 #include <pango/pango-layout.h>
24 #include <pango/pangocairo.h>
26 #include "output/driver-provider.h"
27 #include "output/output-item.h"
29 #include "gl/xalloc.h"
32 #define _(msgid) gettext (msgid)
34 /* This file uses TABLE_HORZ and TABLE_VERT enough to warrant abbreviating. */
38 struct xr_page_style *
39 xr_page_style_ref (const struct xr_page_style *ps_)
41 struct xr_page_style *ps = CONST_CAST (struct xr_page_style *, ps_);
42 assert (ps->ref_cnt > 0);
47 struct xr_page_style *
48 xr_page_style_unshare (struct xr_page_style *old)
50 assert (old->ref_cnt > 0);
51 if (old->ref_cnt == 1)
54 xr_page_style_unref (old);
56 struct xr_page_style *new = xmemdup (old, sizeof *old);
58 for (int i = 0; i < 2; i++)
59 page_heading_copy (&new->headings[i], &old->headings[i]);
65 xr_page_style_unref (struct xr_page_style *ps)
69 assert (ps->ref_cnt > 0);
72 for (int i = 0; i < 2; i++)
73 page_heading_uninit (&ps->headings[i]);
80 xr_page_style_equals (const struct xr_page_style *a,
81 const struct xr_page_style *b)
83 for (int i = 0; i < TABLE_N_AXES; i++)
84 for (int j = 0; j < 2; j++)
85 if (a->margins[i][j] != b->margins[i][j])
88 for (int i = 0; i < 2; i++)
89 if (!page_heading_equals (&a->headings[i], &b->headings[i]))
92 return a->initial_page_number == b->initial_page_number;
97 struct xr_page_style *page_style;
98 struct xr_fsm_style *fsm_style;
100 int heading_heights[2];
102 /* Current output item. */
104 struct output_iterator iter;
105 struct output_item *root_item;
109 /* Grouping, for constructing the outline for PDFs. */
112 const struct output_item *item;
116 size_t n_nodes, allocated_nodes;
118 /* Current output page. */
123 static void xr_pager_run (struct xr_pager *);
125 /* Conversions to and from points. */
129 return x / (double) XR_POINT;
133 pango_to_xr (int pango)
135 return (XR_POINT != PANGO_SCALE
136 ? ceil (pango * (1. * XR_POINT / PANGO_SCALE))
143 return (XR_POINT != PANGO_SCALE
144 ? ceil (xr * (1. / XR_POINT * PANGO_SCALE))
149 get_layout_height (PangoLayout *layout)
152 pango_layout_get_size (layout, &w, &h);
157 xr_render_page_heading (cairo_t *cairo, const PangoFontDescription *font,
158 const struct page_heading *ph, int page_number,
159 int width, int base_y, double font_resolution)
161 PangoContext *context = pango_cairo_create_context (cairo);
162 pango_cairo_context_set_resolution (context, font_resolution);
163 PangoLayout *layout = pango_layout_new (context);
164 g_object_unref (context);
166 pango_layout_set_font_description (layout, font);
169 for (size_t i = 0; i < ph->n; i++)
171 const struct page_paragraph *pp = &ph->paragraphs[i];
173 char *markup = output_driver_substitute_heading_vars (pp->markup,
175 pango_layout_set_markup (layout, markup, -1);
178 pango_layout_set_alignment (
180 (pp->halign == TABLE_HALIGN_LEFT ? PANGO_ALIGN_LEFT
181 : pp->halign == TABLE_HALIGN_CENTER ? PANGO_ALIGN_CENTER
182 : pp->halign == TABLE_HALIGN_MIXED ? PANGO_ALIGN_LEFT
183 : PANGO_ALIGN_RIGHT));
184 pango_layout_set_width (layout, xr_to_pango (width));
187 cairo_translate (cairo, 0, xr_to_pt (y + base_y));
188 pango_cairo_show_layout (cairo, layout);
189 cairo_restore (cairo);
191 y += pango_to_xr (get_layout_height (layout));
194 g_object_unref (G_OBJECT (layout));
200 xr_measure_headings (const struct xr_page_style *ps,
201 const struct xr_fsm_style *fs,
202 int heading_heights[2])
204 cairo_surface_t *surface = cairo_recording_surface_create (
205 CAIRO_CONTENT_COLOR, NULL);
206 cairo_t *cairo = cairo_create (surface);
207 for (int i = 0; i < 2; i++)
209 int *h = &heading_heights[i];
210 *h = xr_render_page_heading (cairo, fs->font,
211 &ps->headings[i], -1, fs->size[H], 0,
212 fs->font_resolution);
214 *h += fs->object_spacing;
216 cairo_destroy (cairo);
217 cairo_surface_destroy (surface);
221 xr_pager_create (const struct xr_page_style *ps_,
222 const struct xr_fsm_style *fs_)
224 struct xr_page_style *ps = xr_page_style_ref (ps_);
225 struct xr_fsm_style *fs = xr_fsm_style_ref (fs_);
227 int heading_heights[2];
228 xr_measure_headings (ps, fs, heading_heights);
229 int total = heading_heights[0] + heading_heights[1];
230 if (total > 0 && total < fs->size[V])
232 fs = xr_fsm_style_unshare (fs);
233 ps = xr_page_style_unshare (ps);
235 for (int i = 0; i < 2; i++)
236 ps->margins[V][i] += heading_heights[i];
237 fs->size[V] -= total;
240 struct xr_pager *p = xmalloc (sizeof *p);
241 *p = (struct xr_pager) { .page_style = ps, .fsm_style = fs };
246 xr_pager_destroy (struct xr_pager *p)
252 xr_page_style_unref (p->page_style);
253 xr_fsm_style_unref (p->fsm_style);
255 xr_fsm_destroy (p->fsm);
256 output_iterator_destroy (&p->iter);
257 output_item_unref (p->root_item);
261 cairo_restore (p->cr);
262 cairo_destroy (p->cr);
269 xr_pager_has_item (const struct xr_pager *p)
271 return p->root_item != NULL;
275 xr_pager_add_item (struct xr_pager *p, const struct output_item *item)
277 assert (!p->root_item);
278 p->root_item = output_item_ref (item);
279 output_iterator_init (&p->iter, item);
284 xr_pager_has_page (const struct xr_pager *p)
290 xr_pager_add_page (struct xr_pager *p, cairo_t *cr)
297 const struct xr_fsm_style *fs = p->fsm_style;
298 const struct xr_page_style *ps = p->page_style;
300 xr_to_pt (ps->margins[H][0]),
301 xr_to_pt (ps->margins[V][0]));
303 int page_number = p->page_index++ + ps->initial_page_number;
304 if (p->heading_heights[0])
305 xr_render_page_heading (cr, fs->font, &ps->headings[0], page_number,
306 fs->size[H], -p->heading_heights[0],
307 fs->font_resolution);
309 if (p->heading_heights[1])
310 xr_render_page_heading (cr, fs->font, &ps->headings[1], page_number,
311 fs->size[H], fs->size[V] + fs->object_spacing,
312 fs->font_resolution);
314 cairo_surface_t *surface = cairo_get_target (cr);
315 if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_PDF)
317 char *page_label = xasprintf ("%d", page_number);
318 cairo_pdf_surface_set_page_label (surface, page_label);
326 xr_pager_finish_page (struct xr_pager *p)
330 cairo_restore (p->cr);
331 cairo_destroy (p->cr);
337 xr_pager_needs_new_page (struct xr_pager *p)
339 if (p->root_item && (!p->cr || p->y >= p->fsm_style->size[V]))
341 xr_pager_finish_page (p);
349 add_outline (cairo_t *cr, int parent_id,
350 const char *utf8, const char *link_attribs,
351 cairo_pdf_outline_flags_t flags)
353 cairo_surface_t *surface = cairo_get_target (cr);
354 return (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_PDF
355 ? cairo_pdf_surface_add_outline (surface, parent_id,
356 utf8, link_attribs, flags)
361 xr_pager_run (struct xr_pager *p)
363 if (!p->root_item || !p->cr || p->y >= p->fsm_style->size[V])
368 /* Make sure we've got an object to render. */
371 /* If there are no remaining objects to render, then we're done. */
374 output_item_unref (p->root_item);
379 /* Prepare to render the current object. */
380 p->fsm = xr_fsm_create_for_printing (p->iter.cur, p->fsm_style,
382 p->label = output_item_get_label (p->iter.cur);
384 while (p->n_nodes > p->iter.n)
388 size_t i = p->n_nodes - 1;
389 if (p->nodes[i].item == p->iter.nodes[i].group)
394 while (p->n_nodes < p->iter.n)
396 if (p->n_nodes >= p->allocated_nodes)
397 p->nodes = x2nrealloc (p->nodes, &p->allocated_nodes,
399 size_t i = p->n_nodes++;
400 p->nodes[i] = (struct outline_node) {
401 .item = p->iter.nodes[i].group,
404 output_iterator_next (&p->iter);
407 char *dest_name = NULL;
408 if (p->page_style->include_outline)
410 static int counter = 0;
411 dest_name = xasprintf ("dest%d", counter++);
412 char *attrs = xasprintf ("name='%s'", dest_name);
413 cairo_tag_begin (p->cr, CAIRO_TAG_DEST, attrs);
417 int spacing = p->fsm_style->object_spacing;
418 int chunk = xr_fsm_draw_slice (p->fsm, p->cr,
419 p->fsm_style->size[V] - p->y);
420 p->y += chunk + spacing;
421 cairo_translate (p->cr, 0, xr_to_pt (chunk + spacing));
423 if (p->page_style->include_outline)
425 cairo_tag_end (p->cr, CAIRO_TAG_DEST);
427 if (chunk && p->slice_idx++ == 0)
429 char *attrs = xasprintf ("dest='%s'", dest_name);
431 int parent_group_id = CAIRO_PDF_OUTLINE_ROOT;
432 for (size_t i = 0; i < p->n_nodes; i++)
434 struct outline_node *node = &p->nodes[i];
437 const char *label = output_item_get_label (node->item);
438 node->group_id = add_outline (
439 p->cr, parent_group_id, label, attrs,
440 CAIRO_PDF_OUTLINE_FLAG_OPEN);
442 parent_group_id = node->group_id;
445 add_outline (p->cr, parent_group_id, p->label, attrs, 0);
451 if (xr_fsm_is_empty (p->fsm))
453 xr_fsm_destroy (p->fsm);