311a3b546afb7a82b188e24d99d96a55b8a31929
[pspp] / src / output / cairo-pager.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2010, 2014, 2020 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include "output/cairo-pager.h"
20
21 #include <math.h>
22 #include <cairo/cairo-pdf.h>
23 #include <pango/pango-layout.h>
24 #include <pango/pangocairo.h>
25
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"
34
35 #include "gl/xalloc.h"
36
37 #include "gettext.h"
38 #define _(msgid) gettext (msgid)
39
40 /* This file uses TABLE_HORZ and TABLE_VERT enough to warrant abbreviating. */
41 #define H TABLE_HORZ
42 #define V TABLE_VERT
43 \f
44 struct xr_page_style *
45 xr_page_style_ref (const struct xr_page_style *ps_)
46 {
47   struct xr_page_style *ps = CONST_CAST (struct xr_page_style *, ps_);
48   assert (ps->ref_cnt > 0);
49   ps->ref_cnt++;
50   return ps;
51 }
52
53 struct xr_page_style *
54 xr_page_style_unshare (struct xr_page_style *old)
55 {
56   assert (old->ref_cnt > 0);
57   if (old->ref_cnt == 1)
58     return old;
59
60   xr_page_style_unref (old);
61
62   struct xr_page_style *new = xmemdup (old, sizeof *old);
63   new->ref_cnt = 1;
64   for (int i = 0; i < 2; i++)
65     page_heading_copy (&new->headings[i], &old->headings[i]);
66
67   return new;
68 }
69
70 void
71 xr_page_style_unref (struct xr_page_style *ps)
72 {
73   if (ps)
74     {
75       assert (ps->ref_cnt > 0);
76       if (!--ps->ref_cnt)
77         {
78           for (int i = 0; i < 2; i++)
79             page_heading_uninit (&ps->headings[i]);
80           free (ps);
81         }
82     }
83 }
84
85 bool
86 xr_page_style_equals (const struct xr_page_style *a,
87                       const struct xr_page_style *b)
88 {
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])
92         return false;
93
94   for (int i = 0; i < 2; i++)
95     if (!page_heading_equals (&a->headings[i], &b->headings[i]))
96       return false;
97
98   return a->initial_page_number == b->initial_page_number;
99 }
100 \f
101 struct xr_pager
102   {
103     struct xr_page_style *page_style;
104     struct xr_fsm_style *fsm_style;
105     int page_index;
106     int heading_heights[2];
107
108     /* Current output item. */
109     struct xr_fsm *fsm;
110     struct output_item *item;
111     int slice_idx;
112
113     /* Grouping, for constructing the outline for PDFs.
114
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). */
120     int *group_ids;
121     size_t n_group_ids, allocated_group_ids;
122     struct group_open_item **group_opens;
123     size_t n_opens, allocated_opens;
124
125     /* Current output page. */
126     cairo_t *cr;
127     int y;
128   };
129
130 static void xr_pager_run (struct xr_pager *);
131
132 /* Conversions to and from points. */
133 static double
134 xr_to_pt (int x)
135 {
136   return x / (double) XR_POINT;
137 }
138
139 static int
140 pango_to_xr (int pango)
141 {
142   return (XR_POINT != PANGO_SCALE
143           ? ceil (pango * (1. * XR_POINT / PANGO_SCALE))
144           : pango);
145 }
146
147 static int
148 xr_to_pango (int xr)
149 {
150   return (XR_POINT != PANGO_SCALE
151           ? ceil (xr * (1. / XR_POINT * PANGO_SCALE))
152           : xr);
153 }
154
155 static int
156 get_layout_height (PangoLayout *layout)
157 {
158   int w, h;
159   pango_layout_get_size (layout, &w, &h);
160   return h;
161 }
162
163 static int
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)
167 {
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);
172
173   pango_layout_set_font_description (layout, font);
174
175   int y = 0;
176   for (size_t i = 0; i < ph->n; i++)
177     {
178       const struct page_paragraph *pp = &ph->paragraphs[i];
179
180       char *markup = output_driver_substitute_heading_vars (pp->markup,
181                                                             page_number);
182       pango_layout_set_markup (layout, markup, -1);
183       free (markup);
184
185       pango_layout_set_alignment (
186         layout,
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));
192
193       cairo_save (cairo);
194       cairo_translate (cairo, 0, xr_to_pt (y + base_y));
195       pango_cairo_show_layout (cairo, layout);
196       cairo_restore (cairo);
197
198       y += pango_to_xr (get_layout_height (layout));
199     }
200
201   g_object_unref (G_OBJECT (layout));
202
203   return y;
204 }
205
206 static void
207 xr_measure_headings (const struct xr_page_style *ps,
208                      const struct xr_fsm_style *fs,
209                      int heading_heights[2])
210 {
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++)
215     {
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);
220       if (*h)
221         *h += fs->object_spacing;
222     }
223   cairo_destroy (cairo);
224   cairo_surface_destroy (surface);
225 }
226
227 struct xr_pager *
228 xr_pager_create (const struct xr_page_style *ps_,
229                  const struct xr_fsm_style *fs_)
230 {
231   struct xr_page_style *ps = xr_page_style_ref (ps_);
232   struct xr_fsm_style *fs = xr_fsm_style_ref (fs_);
233
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])
238     {
239       fs = xr_fsm_style_unshare (fs);
240       ps = xr_page_style_unshare (ps);
241
242       for (int i = 0; i < 2; i++)
243         ps->margins[V][i] += heading_heights[i];
244       fs->size[V] -= total;
245     }
246
247   struct xr_pager *p = xmalloc (sizeof *p);
248   *p = (struct xr_pager) { .page_style = ps, .fsm_style = fs };
249   return p;
250 }
251
252 void
253 xr_pager_destroy (struct xr_pager *p)
254 {
255   if (p)
256     {
257       free (p->group_ids);
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);
261
262       xr_page_style_unref (p->page_style);
263       xr_fsm_style_unref (p->fsm_style);
264
265       xr_fsm_destroy (p->fsm);
266       output_item_unref (p->item);
267
268       if (p->cr)
269         {
270           cairo_restore (p->cr);
271           cairo_destroy (p->cr);
272         }
273       free (p);
274     }
275 }
276
277 bool
278 xr_pager_has_item (const struct xr_pager *p)
279 {
280   return p->item != NULL;
281 }
282
283 void
284 xr_pager_add_item (struct xr_pager *p, const struct output_item *item)
285 {
286   assert (!p->item);
287   p->item = output_item_ref (item);
288   p->slice_idx = 0;
289   xr_pager_run (p);
290 }
291
292 bool
293 xr_pager_has_page (const struct xr_pager *p)
294 {
295   return p->cr;
296 }
297
298 void
299 xr_pager_add_page (struct xr_pager *p, cairo_t *cr)
300 {
301   assert (!p->cr);
302   cairo_save (cr);
303   p->cr = cr;
304   p->y = 0;
305
306   const struct xr_fsm_style *fs = p->fsm_style;
307   const struct xr_page_style *ps = p->page_style;
308   cairo_translate (cr,
309                    xr_to_pt (ps->margins[H][0]),
310                    xr_to_pt (ps->margins[V][0]));
311
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);
317
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);
322
323   cairo_surface_t *surface = cairo_get_target (cr);
324   if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_PDF)
325     {
326       char *page_label = xasprintf ("%d", page_number);
327       cairo_pdf_surface_set_page_label (surface, page_label);
328       free (page_label);
329     }
330
331   xr_pager_run (p);
332 }
333
334 void
335 xr_pager_finish_page (struct xr_pager *p)
336 {
337   if (p->cr)
338     {
339       cairo_restore (p->cr);
340       cairo_destroy (p->cr);
341       p->cr = NULL;
342     }
343 }
344
345 bool
346 xr_pager_needs_new_page (struct xr_pager *p)
347 {
348   if (p->item && (!p->cr || p->y >= p->fsm_style->size[V]))
349     {
350       xr_pager_finish_page (p);
351       return true;
352     }
353   else
354     return false;
355 }
356
357 static int
358 add_outline (cairo_t *cr, int parent_id,
359              const char *utf8, const char *link_attribs,
360              cairo_pdf_outline_flags_t flags)
361 {
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)
366           : 0);
367 }
368
369 static void
370 xr_pager_run (struct xr_pager *p)
371 {
372   if (p->item && p->cr && p->y < p->fsm_style->size[V])
373     {
374       if (!p->fsm)
375         {
376           if (is_group_open_item (p->item))
377             {
378               if (p->n_opens >= p->allocated_opens)
379                 p->group_opens = x2nrealloc (p->group_opens,
380                                              &p->allocated_opens,
381                                              sizeof p->group_opens);
382               p->group_opens[p->n_opens++] = group_open_item_ref (
383                 to_group_open_item (p->item));
384             }
385           else if (is_group_close_item (p->item))
386             {
387               if (p->n_opens)
388                 group_open_item_unref (p->group_opens[--p->n_opens]);
389               else if (p->n_group_ids)
390                 p->n_group_ids--;
391               else
392                 {
393                   /* Something wrong! */
394                 }
395             }
396
397           p->fsm = xr_fsm_create_for_printing (p->item, p->fsm_style, p->cr);
398           if (!p->fsm)
399             {
400               output_item_unref (p->item);
401               p->item = NULL;
402
403               return;
404             }
405         }
406
407       for (;;)
408         {
409           char *dest_name = NULL;
410           if (p->page_style->include_outline)
411             {
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);
416               free (attrs);
417             }
418
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));
424
425           if (p->page_style->include_outline)
426             {
427               cairo_tag_end (p->cr, CAIRO_TAG_DEST);
428
429               if (chunk && p->slice_idx++ == 0)
430                 {
431                   char *attrs = xasprintf ("dest='%s'", dest_name);
432
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++)
437                     {
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]);
443
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;
449                     }
450                   p->n_opens = 0;
451
452                   add_outline (p->cr, parent_group_id,
453                                output_item_get_label (p->item), attrs, 0);
454                   free (attrs);
455                 }
456               free (dest_name);
457             }
458
459           if (xr_fsm_is_empty (p->fsm))
460             {
461               xr_fsm_destroy (p->fsm);
462               p->fsm = NULL;
463               output_item_unref (p->item);
464               p->item = NULL;
465               return;
466             }
467           else if (!chunk)
468             {
469               assert (p->y > 0);
470               p->y = INT_MAX;
471               return;
472             }
473         }
474     }
475 }