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/chart-item.h"
27 #include "output/driver-provider.h"
28 #include "output/group-item.h"
29 #include "output/message-item.h"
30 #include "output/page-eject-item.h"
31 #include "output/page-setup-item.h"
32 #include "output/table-item.h"
33 #include "output/text-item.h"
35 #include "gl/xalloc.h"
38 #define _(msgid) gettext (msgid)
40 /* This file uses TABLE_HORZ and TABLE_VERT enough to warrant abbreviating. */
44 struct xr_page_style *
45 xr_page_style_ref (const struct xr_page_style *ps_)
47 struct xr_page_style *ps = CONST_CAST (struct xr_page_style *, ps_);
48 assert (ps->ref_cnt > 0);
53 struct xr_page_style *
54 xr_page_style_unshare (struct xr_page_style *old)
56 assert (old->ref_cnt > 0);
57 if (old->ref_cnt == 1)
60 xr_page_style_unref (old);
62 struct xr_page_style *new = xmemdup (old, sizeof *old);
64 for (int i = 0; i < 2; i++)
65 page_heading_copy (&new->headings[i], &old->headings[i]);
71 xr_page_style_unref (struct xr_page_style *ps)
75 assert (ps->ref_cnt > 0);
78 for (int i = 0; i < 2; i++)
79 page_heading_uninit (&ps->headings[i]);
86 xr_page_style_equals (const struct xr_page_style *a,
87 const struct xr_page_style *b)
89 for (int i = 0; i < TABLE_N_AXES; i++)
90 for (int j = 0; j < 2; j++)
91 if (a->margins[i][j] != b->margins[i][j])
94 for (int i = 0; i < 2; i++)
95 if (!page_heading_equals (&a->headings[i], &b->headings[i]))
98 return a->initial_page_number == b->initial_page_number;
103 struct xr_page_style *page_style;
104 struct xr_fsm_style *fsm_style;
106 int heading_heights[2];
108 /* Current output item. */
110 struct output_item *item;
113 /* Grouping, for constructing the outline for PDFs.
115 The 'group_ids' were returned by cairo_pdf_surface_add_outline() and
116 represent the groups within which upcoming output is nested. The
117 'group_opens' will be passed to cairo_pdf_surface_add_outline() when the
118 next item is rendered (we defer it so that the location associated with
119 the outline item can be the first object actually output in it). */
121 size_t n_group_ids, allocated_group_ids;
122 struct group_open_item **group_opens;
123 size_t n_opens, allocated_opens;
125 /* Current output page. */
130 static void xr_pager_run (struct xr_pager *);
132 /* Conversions to and from points. */
136 return x / (double) XR_POINT;
140 pango_to_xr (int pango)
142 return (XR_POINT != PANGO_SCALE
143 ? ceil (pango * (1. * XR_POINT / PANGO_SCALE))
150 return (XR_POINT != PANGO_SCALE
151 ? ceil (xr * (1. / XR_POINT * PANGO_SCALE))
156 get_layout_height (PangoLayout *layout)
159 pango_layout_get_size (layout, &w, &h);
164 xr_render_page_heading (cairo_t *cairo, const PangoFontDescription *font,
165 const struct page_heading *ph, int page_number,
166 int width, int base_y, double font_resolution)
168 PangoContext *context = pango_cairo_create_context (cairo);
169 pango_cairo_context_set_resolution (context, font_resolution);
170 PangoLayout *layout = pango_layout_new (context);
171 g_object_unref (context);
173 pango_layout_set_font_description (layout, font);
176 for (size_t i = 0; i < ph->n; i++)
178 const struct page_paragraph *pp = &ph->paragraphs[i];
180 char *markup = output_driver_substitute_heading_vars (pp->markup,
182 pango_layout_set_markup (layout, markup, -1);
185 pango_layout_set_alignment (
187 (pp->halign == TABLE_HALIGN_LEFT ? PANGO_ALIGN_LEFT
188 : pp->halign == TABLE_HALIGN_CENTER ? PANGO_ALIGN_CENTER
189 : pp->halign == TABLE_HALIGN_MIXED ? PANGO_ALIGN_LEFT
190 : PANGO_ALIGN_RIGHT));
191 pango_layout_set_width (layout, xr_to_pango (width));
194 cairo_translate (cairo, 0, xr_to_pt (y + base_y));
195 pango_cairo_show_layout (cairo, layout);
196 cairo_restore (cairo);
198 y += pango_to_xr (get_layout_height (layout));
201 g_object_unref (G_OBJECT (layout));
207 xr_measure_headings (const struct xr_page_style *ps,
208 const struct xr_fsm_style *fs,
209 int heading_heights[2])
211 cairo_surface_t *surface = cairo_recording_surface_create (
212 CAIRO_CONTENT_COLOR, NULL);
213 cairo_t *cairo = cairo_create (surface);
214 for (int i = 0; i < 2; i++)
216 int *h = &heading_heights[i];
217 *h = xr_render_page_heading (cairo, fs->font,
218 &ps->headings[i], -1, fs->size[H], 0,
219 fs->font_resolution);
221 *h += fs->object_spacing;
223 cairo_destroy (cairo);
224 cairo_surface_destroy (surface);
228 xr_pager_create (const struct xr_page_style *ps_,
229 const struct xr_fsm_style *fs_)
231 struct xr_page_style *ps = xr_page_style_ref (ps_);
232 struct xr_fsm_style *fs = xr_fsm_style_ref (fs_);
234 int heading_heights[2];
235 xr_measure_headings (ps, fs, heading_heights);
236 int total = heading_heights[0] + heading_heights[1];
237 if (total > 0 && total < fs->size[V])
239 fs = xr_fsm_style_unshare (fs);
240 ps = xr_page_style_unshare (ps);
242 for (int i = 0; i < 2; i++)
243 ps->margins[V][i] += heading_heights[i];
244 fs->size[V] -= total;
247 struct xr_pager *p = xmalloc (sizeof *p);
248 *p = (struct xr_pager) { .page_style = ps, .fsm_style = fs };
253 xr_pager_destroy (struct xr_pager *p)
258 for (size_t i = 0; i < p->n_opens; i++)
259 group_open_item_unref (p->group_opens[i]);
260 free (p->group_opens);
262 xr_page_style_unref (p->page_style);
263 xr_fsm_style_unref (p->fsm_style);
265 xr_fsm_destroy (p->fsm);
266 output_item_unref (p->item);
270 cairo_restore (p->cr);
271 cairo_destroy (p->cr);
278 xr_pager_has_item (const struct xr_pager *p)
280 return p->item != NULL;
284 xr_pager_add_item (struct xr_pager *p, const struct output_item *item)
287 p->item = output_item_ref (item);
293 xr_pager_has_page (const struct xr_pager *p)
299 xr_pager_add_page (struct xr_pager *p, cairo_t *cr)
306 const struct xr_fsm_style *fs = p->fsm_style;
307 const struct xr_page_style *ps = p->page_style;
309 xr_to_pt (ps->margins[H][0]),
310 xr_to_pt (ps->margins[V][0]));
312 int page_number = p->page_index++ + ps->initial_page_number;
313 if (p->heading_heights[0])
314 xr_render_page_heading (cr, fs->font, &ps->headings[0], page_number,
315 fs->size[H], -p->heading_heights[0],
316 fs->font_resolution);
318 if (p->heading_heights[1])
319 xr_render_page_heading (cr, fs->font, &ps->headings[1], page_number,
320 fs->size[H], fs->size[V] + fs->object_spacing,
321 fs->font_resolution);
323 cairo_surface_t *surface = cairo_get_target (cr);
324 if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_PDF)
326 char *page_label = xasprintf ("%d", page_number);
327 cairo_pdf_surface_set_page_label (surface, page_label);
335 xr_pager_finish_page (struct xr_pager *p)
339 cairo_restore (p->cr);
340 cairo_destroy (p->cr);
346 xr_pager_needs_new_page (struct xr_pager *p)
348 if (p->item && (!p->cr || p->y >= p->fsm_style->size[V]))
350 xr_pager_finish_page (p);
358 add_outline (cairo_t *cr, int parent_id,
359 const char *utf8, const char *link_attribs,
360 cairo_pdf_outline_flags_t flags)
362 cairo_surface_t *surface = cairo_get_target (cr);
363 return (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_PDF
364 ? cairo_pdf_surface_add_outline (surface, parent_id,
365 utf8, link_attribs, flags)
370 xr_pager_run (struct xr_pager *p)
372 if (p->item && p->cr && p->y < p->fsm_style->size[V])
376 if (is_group_open_item (p->item))
378 if (p->n_opens >= p->allocated_opens)
379 p->group_opens = x2nrealloc (p->group_opens,
381 sizeof p->group_opens);
382 p->group_opens[p->n_opens++] = group_open_item_ref (
383 to_group_open_item (p->item));
385 else if (is_group_close_item (p->item))
388 group_open_item_unref (p->group_opens[--p->n_opens]);
389 else if (p->n_group_ids)
393 /* Something wrong! */
397 p->fsm = xr_fsm_create_for_printing (p->item, p->fsm_style, p->cr);
400 output_item_unref (p->item);
409 char *dest_name = NULL;
410 if (p->page_style->include_outline)
412 static int counter = 0;
413 dest_name = xasprintf ("dest%d", counter++);
414 char *attrs = xasprintf ("name='%s'", dest_name);
415 cairo_tag_begin (p->cr, CAIRO_TAG_DEST, attrs);
419 int spacing = p->fsm_style->object_spacing;
420 int chunk = xr_fsm_draw_slice (p->fsm, p->cr,
421 p->fsm_style->size[V] - p->y);
422 p->y += chunk + spacing;
423 cairo_translate (p->cr, 0, xr_to_pt (chunk + spacing));
425 if (p->page_style->include_outline)
427 cairo_tag_end (p->cr, CAIRO_TAG_DEST);
429 if (chunk && p->slice_idx++ == 0)
431 char *attrs = xasprintf ("dest='%s'", dest_name);
433 int parent_group_id = (p->n_group_ids
434 ? p->group_ids[p->n_group_ids - 1]
435 : CAIRO_PDF_OUTLINE_ROOT);
436 for (size_t i = 0; i < p->n_opens; i++)
438 parent_group_id = add_outline (
439 p->cr, parent_group_id,
440 p->group_opens[i]->command_name, attrs,
441 CAIRO_PDF_OUTLINE_FLAG_OPEN);
442 group_open_item_unref (p->group_opens[i]);
444 if (p->n_group_ids >= p->allocated_group_ids)
445 p->group_ids = x2nrealloc (p->group_ids,
446 &p->allocated_group_ids,
447 sizeof *p->group_ids);
448 p->group_ids[p->n_group_ids++] = parent_group_id;
452 add_outline (p->cr, parent_group_id,
453 output_item_get_label (p->item), attrs, 0);
459 if (xr_fsm_is_empty (p->fsm))
461 xr_fsm_destroy (p->fsm);
463 output_item_unref (p->item);