cairo-pager: Add outline to PDF output.
[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           && a->object_spacing == b->object_spacing);
100 }
101 \f
102 struct xr_pager
103   {
104     struct xr_page_style *page_style;
105     struct xr_fsm_style *fsm_style;
106     int page_index;
107     int heading_heights[2];
108
109     /* Current output item. */
110     struct xr_fsm *fsm;
111     struct output_item *item;
112     int slice_idx;
113
114     /* Grouping, for constructing the outline for PDFs.
115
116        The 'group_ids' were returned by cairo_pdf_surface_add_outline() and
117        represent the groups within which upcoming output is nested.  The
118        'group_opens' will be passed to cairo_pdf_surface_add_outline() when the
119        next item is rendered (we defer it so that the location associated with
120        the outline item can be the first object actually output in it). */
121     int *group_ids;
122     size_t n_group_ids, allocated_group_ids;
123     struct group_open_item **group_opens;
124     size_t n_opens, allocated_opens;
125
126     /* Current output page. */
127     cairo_t *cr;
128     int y;
129   };
130
131 static void xr_pager_run (struct xr_pager *);
132
133 /* Conversions to and from points. */
134 static double
135 xr_to_pt (int x)
136 {
137   return x / (double) XR_POINT;
138 }
139
140 static int
141 pango_to_xr (int pango)
142 {
143   return (XR_POINT != PANGO_SCALE
144           ? ceil (pango * (1. * XR_POINT / PANGO_SCALE))
145           : pango);
146 }
147
148 static int
149 xr_to_pango (int xr)
150 {
151   return (XR_POINT != PANGO_SCALE
152           ? ceil (xr * (1. / XR_POINT * PANGO_SCALE))
153           : xr);
154 }
155
156 static int
157 get_layout_height (PangoLayout *layout)
158 {
159   int w, h;
160   pango_layout_get_size (layout, &w, &h);
161   return h;
162 }
163
164 static int
165 xr_render_page_heading (cairo_t *cairo, const PangoFontDescription *font,
166                         const struct page_heading *ph, int page_number,
167                         int width, int base_y, double font_resolution)
168 {
169   PangoContext *context = pango_cairo_create_context (cairo);
170   pango_cairo_context_set_resolution (context, font_resolution);
171   PangoLayout *layout = pango_layout_new (context);
172   g_object_unref (context);
173
174   pango_layout_set_font_description (layout, font);
175
176   int y = 0;
177   for (size_t i = 0; i < ph->n; i++)
178     {
179       const struct page_paragraph *pp = &ph->paragraphs[i];
180
181       char *markup = output_driver_substitute_heading_vars (pp->markup,
182                                                             page_number);
183       pango_layout_set_markup (layout, markup, -1);
184       free (markup);
185
186       pango_layout_set_alignment (
187         layout,
188         (pp->halign == TABLE_HALIGN_LEFT ? PANGO_ALIGN_LEFT
189          : pp->halign == TABLE_HALIGN_CENTER ? PANGO_ALIGN_CENTER
190          : pp->halign == TABLE_HALIGN_MIXED ? PANGO_ALIGN_LEFT
191          : PANGO_ALIGN_RIGHT));
192       pango_layout_set_width (layout, xr_to_pango (width));
193
194       cairo_save (cairo);
195       cairo_translate (cairo, 0, xr_to_pt (y + base_y));
196       pango_cairo_show_layout (cairo, layout);
197       cairo_restore (cairo);
198
199       y += pango_to_xr (get_layout_height (layout));
200     }
201
202   g_object_unref (G_OBJECT (layout));
203
204   return y;
205 }
206
207 static void
208 xr_measure_headings (const struct xr_page_style *ps,
209                      const struct xr_fsm_style *fs,
210                      int heading_heights[2])
211 {
212   cairo_surface_t *surface = cairo_recording_surface_create (
213     CAIRO_CONTENT_COLOR, NULL);
214   cairo_t *cairo = cairo_create (surface);
215   for (int i = 0; i < 2; i++)
216     {
217       int *h = &heading_heights[i];
218       *h = xr_render_page_heading (cairo, fs->fonts[XR_FONT_PROPORTIONAL],
219                                    &ps->headings[i], -1, fs->size[H], 0,
220                                    fs->font_resolution);
221       if (*h)
222         *h += ps->object_spacing;
223     }
224   cairo_destroy (cairo);
225   cairo_surface_destroy (surface);
226 }
227
228 struct xr_pager *
229 xr_pager_create (const struct xr_page_style *ps_,
230                  const struct xr_fsm_style *fs_)
231 {
232   struct xr_page_style *ps = xr_page_style_ref (ps_);
233   struct xr_fsm_style *fs = xr_fsm_style_ref (fs_);
234
235   int heading_heights[2];
236   xr_measure_headings (ps, fs, heading_heights);
237   int total = heading_heights[0] + heading_heights[1];
238   if (total > 0 && total < fs->size[V])
239     {
240       fs = xr_fsm_style_unshare (fs);
241       ps = xr_page_style_unshare (ps);
242
243       for (int i = 0; i < 2; i++)
244         ps->margins[V][i] += heading_heights[i];
245       fs->size[V] -= total;
246     }
247
248   struct xr_pager *p = xmalloc (sizeof *p);
249   *p = (struct xr_pager) { .page_style = ps, .fsm_style = fs };
250   return p;
251 }
252
253 void
254 xr_pager_destroy (struct xr_pager *p)
255 {
256   if (p)
257     {
258       free (p->group_ids);
259       for (size_t i = 0; i < p->n_opens; i++)
260         group_open_item_unref (p->group_opens[i]);
261       free (p->group_opens);
262
263       xr_page_style_unref (p->page_style);
264       xr_fsm_style_unref (p->fsm_style);
265
266       xr_fsm_destroy (p->fsm);
267       output_item_unref (p->item);
268
269       if (p->cr)
270         {
271           cairo_restore (p->cr);
272           cairo_destroy (p->cr);
273         }
274       free (p);
275     }
276 }
277
278 bool
279 xr_pager_has_item (const struct xr_pager *p)
280 {
281   return p->item != NULL;
282 }
283
284 void
285 xr_pager_add_item (struct xr_pager *p, const struct output_item *item)
286 {
287   assert (!p->item);
288   p->item = output_item_ref (item);
289   p->slice_idx = 0;
290   xr_pager_run (p);
291 }
292
293 bool
294 xr_pager_has_page (const struct xr_pager *p)
295 {
296   return p->cr;
297 }
298
299 void
300 xr_pager_add_page (struct xr_pager *p, cairo_t *cr)
301 {
302   assert (!p->cr);
303   cairo_save (cr);
304   p->cr = cr;
305   p->y = 0;
306
307   const struct xr_fsm_style *fs = p->fsm_style;
308   const struct xr_page_style *ps = p->page_style;
309   cairo_translate (cr,
310                    xr_to_pt (ps->margins[H][0]),
311                    xr_to_pt (ps->margins[V][0]));
312
313   const PangoFontDescription *font = fs->fonts[XR_FONT_PROPORTIONAL];
314   int page_number = p->page_index++ + ps->initial_page_number;
315   if (p->heading_heights[0])
316     xr_render_page_heading (cr, font, &ps->headings[0], page_number,
317                             fs->size[H], -p->heading_heights[0],
318                             fs->font_resolution);
319
320   if (p->heading_heights[1])
321     xr_render_page_heading (cr, font, &ps->headings[1], page_number,
322                             fs->size[H], fs->size[V] + ps->object_spacing,
323                             fs->font_resolution);
324
325   xr_pager_run (p);
326 }
327
328 void
329 xr_pager_finish_page (struct xr_pager *p)
330 {
331   if (p->cr)
332     {
333       cairo_restore (p->cr);
334       cairo_destroy (p->cr);
335       p->cr = NULL;
336     }
337 }
338
339 bool
340 xr_pager_needs_new_page (struct xr_pager *p)
341 {
342   if (p->item && (!p->cr || p->y >= p->fsm_style->size[V]))
343     {
344       xr_pager_finish_page (p);
345       return true;
346     }
347   else
348     return false;
349 }
350
351 static int
352 add_outline (cairo_t *cr, int parent_id,
353              const char *utf8, const char *link_attribs,
354              cairo_pdf_outline_flags_t flags)
355 {
356   cairo_surface_t *surface = cairo_get_target (cr);
357   return (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_PDF
358           ? cairo_pdf_surface_add_outline (surface, parent_id,
359                                            utf8, link_attribs, flags)
360           : 0);
361 }
362
363 static void
364 xr_pager_run (struct xr_pager *p)
365 {
366   if (p->item && p->cr && p->y < p->fsm_style->size[V])
367     {
368       if (!p->fsm)
369         {
370           if (is_group_open_item (p->item))
371             {
372               if (p->n_opens >= p->allocated_opens)
373                 p->group_opens = x2nrealloc (p->group_opens,
374                                              &p->allocated_opens,
375                                              sizeof p->group_opens);
376               p->group_opens[p->n_opens++] = group_open_item_ref (
377                 to_group_open_item (p->item));
378             }
379           else if (is_group_close_item (p->item))
380             {
381               if (p->n_opens)
382                 group_open_item_unref (p->group_opens[--p->n_opens]);
383               else if (p->n_group_ids)
384                 p->n_group_ids--;
385               else
386                 {
387                   /* Something wrong! */
388                 }
389             }
390
391           p->fsm = xr_fsm_create (p->item, p->fsm_style, p->cr);
392           if (!p->fsm)
393             {
394               output_item_unref (p->item);
395               p->item = NULL;
396
397               return;
398             }
399         }
400
401       for (;;)
402         {
403           char *dest_name = NULL;
404           if (p->page_style->include_outline)
405             {
406               static int counter = 0;
407               dest_name = xasprintf ("dest%d", counter++);
408               char *attrs = xasprintf ("name='%s'", dest_name);
409               cairo_tag_begin (p->cr, CAIRO_TAG_DEST, attrs);
410               free (attrs);
411             }
412
413           int spacing = p->page_style->object_spacing;
414           int chunk = xr_fsm_draw_slice (p->fsm, p->cr,
415                                          p->fsm_style->size[V] - p->y);
416           p->y += chunk + spacing;
417           cairo_translate (p->cr, 0, xr_to_pt (chunk + spacing));
418
419           if (p->page_style->include_outline)
420             {
421               cairo_tag_end (p->cr, CAIRO_TAG_DEST);
422
423               if (chunk && p->slice_idx++ == 0)
424                 {
425                   char *attrs = xasprintf ("dest='%s'", dest_name);
426
427                   int parent_group_id = (p->n_group_ids
428                                          ? p->group_ids[p->n_group_ids - 1]
429                                          : CAIRO_PDF_OUTLINE_ROOT);
430                   for (size_t i = 0; i < p->n_opens; i++)
431                     {
432                       parent_group_id = add_outline (
433                         p->cr, parent_group_id,
434                         p->group_opens[i]->command_name, attrs,
435                         CAIRO_PDF_OUTLINE_FLAG_OPEN);
436                       group_open_item_unref (p->group_opens[i]);
437
438                       if (p->n_group_ids >= p->allocated_group_ids)
439                         p->group_ids = x2nrealloc (p->group_ids,
440                                                    &p->allocated_group_ids,
441                                                    sizeof *p->group_ids);
442                       p->group_ids[p->n_group_ids++] = parent_group_id;
443                     }
444                   p->n_opens = 0;
445
446                   const char *text;
447                   if (is_table_item (p->item))
448                     {
449                       const struct table_item_text *title
450                         = table_item_get_title (to_table_item (p->item));
451                       text = title ? title->content : "Table";
452                     }
453                   else if (is_chart_item (p->item))
454                     {
455                       const char *title
456                         = chart_item_get_title (to_chart_item (p->item));
457                       text = title ? title : "Chart";
458                     }
459                   else
460                     text = (is_page_eject_item (p->item) ? "Page Break"
461                             : is_page_setup_item (p->item) ? "Page Setup"
462                             : is_message_item (p->item) ? "Message"
463                             : is_text_item (p->item) ? "Text"
464                             : NULL);
465                   if (text)
466                     add_outline (p->cr, parent_group_id, text, attrs, 0);
467                   free (attrs);
468                 }
469               free (dest_name);
470             }
471
472           if (xr_fsm_is_empty (p->fsm))
473             {
474               xr_fsm_destroy (p->fsm);
475               p->fsm = NULL;
476               output_item_unref (p->item);
477               p->item = NULL;
478               return;
479             }
480           else if (!chunk)
481             {
482               assert (p->y > 0);
483               p->y = INT_MAX;
484               return;
485             }
486         }
487     }
488 }