output-item: Collapse the inheritance hierarchy into a single struct.
[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/driver-provider.h"
27 #include "output/output-item.h"
28
29 #include "gl/xalloc.h"
30
31 #include "gettext.h"
32 #define _(msgid) gettext (msgid)
33
34 /* This file uses TABLE_HORZ and TABLE_VERT enough to warrant abbreviating. */
35 #define H TABLE_HORZ
36 #define V TABLE_VERT
37 \f
38 struct xr_page_style *
39 xr_page_style_ref (const struct xr_page_style *ps_)
40 {
41   struct xr_page_style *ps = CONST_CAST (struct xr_page_style *, ps_);
42   assert (ps->ref_cnt > 0);
43   ps->ref_cnt++;
44   return ps;
45 }
46
47 struct xr_page_style *
48 xr_page_style_unshare (struct xr_page_style *old)
49 {
50   assert (old->ref_cnt > 0);
51   if (old->ref_cnt == 1)
52     return old;
53
54   xr_page_style_unref (old);
55
56   struct xr_page_style *new = xmemdup (old, sizeof *old);
57   new->ref_cnt = 1;
58   for (int i = 0; i < 2; i++)
59     page_heading_copy (&new->headings[i], &old->headings[i]);
60
61   return new;
62 }
63
64 void
65 xr_page_style_unref (struct xr_page_style *ps)
66 {
67   if (ps)
68     {
69       assert (ps->ref_cnt > 0);
70       if (!--ps->ref_cnt)
71         {
72           for (int i = 0; i < 2; i++)
73             page_heading_uninit (&ps->headings[i]);
74           free (ps);
75         }
76     }
77 }
78
79 bool
80 xr_page_style_equals (const struct xr_page_style *a,
81                       const struct xr_page_style *b)
82 {
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])
86         return false;
87
88   for (int i = 0; i < 2; i++)
89     if (!page_heading_equals (&a->headings[i], &b->headings[i]))
90       return false;
91
92   return a->initial_page_number == b->initial_page_number;
93 }
94 \f
95 struct xr_pager
96   {
97     struct xr_page_style *page_style;
98     struct xr_fsm_style *fsm_style;
99     int page_index;
100     int heading_heights[2];
101
102     /* Current output item. */
103     struct xr_fsm *fsm;
104     struct output_item *item;
105     int slice_idx;
106
107     /* Grouping, for constructing the outline for PDFs.
108
109        The 'group_ids' were returned by cairo_pdf_surface_add_outline() and
110        represent the groups within which upcoming output is nested.  The
111        'group_opens' will be passed to cairo_pdf_surface_add_outline() when the
112        next item is rendered (we defer it so that the location associated with
113        the outline item can be the first object actually output in it). */
114     int *group_ids;
115     size_t n_group_ids, allocated_group_ids;
116     struct output_item **group_opens;
117     size_t n_opens, allocated_opens;
118
119     /* Current output page. */
120     cairo_t *cr;
121     int y;
122   };
123
124 static void xr_pager_run (struct xr_pager *);
125
126 /* Conversions to and from points. */
127 static double
128 xr_to_pt (int x)
129 {
130   return x / (double) XR_POINT;
131 }
132
133 static int
134 pango_to_xr (int pango)
135 {
136   return (XR_POINT != PANGO_SCALE
137           ? ceil (pango * (1. * XR_POINT / PANGO_SCALE))
138           : pango);
139 }
140
141 static int
142 xr_to_pango (int xr)
143 {
144   return (XR_POINT != PANGO_SCALE
145           ? ceil (xr * (1. / XR_POINT * PANGO_SCALE))
146           : xr);
147 }
148
149 static int
150 get_layout_height (PangoLayout *layout)
151 {
152   int w, h;
153   pango_layout_get_size (layout, &w, &h);
154   return h;
155 }
156
157 static int
158 xr_render_page_heading (cairo_t *cairo, const PangoFontDescription *font,
159                         const struct page_heading *ph, int page_number,
160                         int width, int base_y, double font_resolution)
161 {
162   PangoContext *context = pango_cairo_create_context (cairo);
163   pango_cairo_context_set_resolution (context, font_resolution);
164   PangoLayout *layout = pango_layout_new (context);
165   g_object_unref (context);
166
167   pango_layout_set_font_description (layout, font);
168
169   int y = 0;
170   for (size_t i = 0; i < ph->n; i++)
171     {
172       const struct page_paragraph *pp = &ph->paragraphs[i];
173
174       char *markup = output_driver_substitute_heading_vars (pp->markup,
175                                                             page_number);
176       pango_layout_set_markup (layout, markup, -1);
177       free (markup);
178
179       pango_layout_set_alignment (
180         layout,
181         (pp->halign == TABLE_HALIGN_LEFT ? PANGO_ALIGN_LEFT
182          : pp->halign == TABLE_HALIGN_CENTER ? PANGO_ALIGN_CENTER
183          : pp->halign == TABLE_HALIGN_MIXED ? PANGO_ALIGN_LEFT
184          : PANGO_ALIGN_RIGHT));
185       pango_layout_set_width (layout, xr_to_pango (width));
186
187       cairo_save (cairo);
188       cairo_translate (cairo, 0, xr_to_pt (y + base_y));
189       pango_cairo_show_layout (cairo, layout);
190       cairo_restore (cairo);
191
192       y += pango_to_xr (get_layout_height (layout));
193     }
194
195   g_object_unref (G_OBJECT (layout));
196
197   return y;
198 }
199
200 static void
201 xr_measure_headings (const struct xr_page_style *ps,
202                      const struct xr_fsm_style *fs,
203                      int heading_heights[2])
204 {
205   cairo_surface_t *surface = cairo_recording_surface_create (
206     CAIRO_CONTENT_COLOR, NULL);
207   cairo_t *cairo = cairo_create (surface);
208   for (int i = 0; i < 2; i++)
209     {
210       int *h = &heading_heights[i];
211       *h = xr_render_page_heading (cairo, fs->font,
212                                    &ps->headings[i], -1, fs->size[H], 0,
213                                    fs->font_resolution);
214       if (*h)
215         *h += fs->object_spacing;
216     }
217   cairo_destroy (cairo);
218   cairo_surface_destroy (surface);
219 }
220
221 struct xr_pager *
222 xr_pager_create (const struct xr_page_style *ps_,
223                  const struct xr_fsm_style *fs_)
224 {
225   struct xr_page_style *ps = xr_page_style_ref (ps_);
226   struct xr_fsm_style *fs = xr_fsm_style_ref (fs_);
227
228   int heading_heights[2];
229   xr_measure_headings (ps, fs, heading_heights);
230   int total = heading_heights[0] + heading_heights[1];
231   if (total > 0 && total < fs->size[V])
232     {
233       fs = xr_fsm_style_unshare (fs);
234       ps = xr_page_style_unshare (ps);
235
236       for (int i = 0; i < 2; i++)
237         ps->margins[V][i] += heading_heights[i];
238       fs->size[V] -= total;
239     }
240
241   struct xr_pager *p = xmalloc (sizeof *p);
242   *p = (struct xr_pager) { .page_style = ps, .fsm_style = fs };
243   return p;
244 }
245
246 void
247 xr_pager_destroy (struct xr_pager *p)
248 {
249   if (p)
250     {
251       free (p->group_ids);
252       for (size_t i = 0; i < p->n_opens; i++)
253         output_item_unref (p->group_opens[i]);
254       free (p->group_opens);
255
256       xr_page_style_unref (p->page_style);
257       xr_fsm_style_unref (p->fsm_style);
258
259       xr_fsm_destroy (p->fsm);
260       output_item_unref (p->item);
261
262       if (p->cr)
263         {
264           cairo_restore (p->cr);
265           cairo_destroy (p->cr);
266         }
267       free (p);
268     }
269 }
270
271 bool
272 xr_pager_has_item (const struct xr_pager *p)
273 {
274   return p->item != NULL;
275 }
276
277 void
278 xr_pager_add_item (struct xr_pager *p, const struct output_item *item)
279 {
280   assert (!p->item);
281   p->item = output_item_ref (item);
282   p->slice_idx = 0;
283   xr_pager_run (p);
284 }
285
286 bool
287 xr_pager_has_page (const struct xr_pager *p)
288 {
289   return p->cr;
290 }
291
292 void
293 xr_pager_add_page (struct xr_pager *p, cairo_t *cr)
294 {
295   assert (!p->cr);
296   cairo_save (cr);
297   p->cr = cr;
298   p->y = 0;
299
300   const struct xr_fsm_style *fs = p->fsm_style;
301   const struct xr_page_style *ps = p->page_style;
302   cairo_translate (cr,
303                    xr_to_pt (ps->margins[H][0]),
304                    xr_to_pt (ps->margins[V][0]));
305
306   int page_number = p->page_index++ + ps->initial_page_number;
307   if (p->heading_heights[0])
308     xr_render_page_heading (cr, fs->font, &ps->headings[0], page_number,
309                             fs->size[H], -p->heading_heights[0],
310                             fs->font_resolution);
311
312   if (p->heading_heights[1])
313     xr_render_page_heading (cr, fs->font, &ps->headings[1], page_number,
314                             fs->size[H], fs->size[V] + fs->object_spacing,
315                             fs->font_resolution);
316
317   cairo_surface_t *surface = cairo_get_target (cr);
318   if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_PDF)
319     {
320       char *page_label = xasprintf ("%d", page_number);
321       cairo_pdf_surface_set_page_label (surface, page_label);
322       free (page_label);
323     }
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 (p->item->type == OUTPUT_ITEM_GROUP_OPEN)
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++] = output_item_ref (p->item);
377             }
378           else if (p->item->type == OUTPUT_ITEM_GROUP_CLOSE)
379             {
380               if (p->n_opens)
381                 output_item_unref (p->group_opens[--p->n_opens]);
382               else if (p->n_group_ids)
383                 p->n_group_ids--;
384               else
385                 {
386                   /* Something wrong! */
387                 }
388             }
389
390           p->fsm = xr_fsm_create_for_printing (p->item, p->fsm_style, p->cr);
391           if (!p->fsm)
392             {
393               output_item_unref (p->item);
394               p->item = NULL;
395
396               return;
397             }
398         }
399
400       for (;;)
401         {
402           char *dest_name = NULL;
403           if (p->page_style->include_outline)
404             {
405               static int counter = 0;
406               dest_name = xasprintf ("dest%d", counter++);
407               char *attrs = xasprintf ("name='%s'", dest_name);
408               cairo_tag_begin (p->cr, CAIRO_TAG_DEST, attrs);
409               free (attrs);
410             }
411
412           int spacing = p->fsm_style->object_spacing;
413           int chunk = xr_fsm_draw_slice (p->fsm, p->cr,
414                                          p->fsm_style->size[V] - p->y);
415           p->y += chunk + spacing;
416           cairo_translate (p->cr, 0, xr_to_pt (chunk + spacing));
417
418           if (p->page_style->include_outline)
419             {
420               cairo_tag_end (p->cr, CAIRO_TAG_DEST);
421
422               if (chunk && p->slice_idx++ == 0)
423                 {
424                   char *attrs = xasprintf ("dest='%s'", dest_name);
425
426                   int parent_group_id = (p->n_group_ids
427                                          ? p->group_ids[p->n_group_ids - 1]
428                                          : CAIRO_PDF_OUTLINE_ROOT);
429                   for (size_t i = 0; i < p->n_opens; i++)
430                     {
431                       parent_group_id = add_outline (
432                         p->cr, parent_group_id,
433                         output_item_get_label (p->group_opens[i]),
434                         attrs, CAIRO_PDF_OUTLINE_FLAG_OPEN);
435                       output_item_unref (p->group_opens[i]);
436
437                       if (p->n_group_ids >= p->allocated_group_ids)
438                         p->group_ids = x2nrealloc (p->group_ids,
439                                                    &p->allocated_group_ids,
440                                                    sizeof *p->group_ids);
441                       p->group_ids[p->n_group_ids++] = parent_group_id;
442                     }
443                   p->n_opens = 0;
444
445                   add_outline (p->cr, parent_group_id,
446                                output_item_get_label (p->item), attrs, 0);
447                   free (attrs);
448                 }
449               free (dest_name);
450             }
451
452           if (xr_fsm_is_empty (p->fsm))
453             {
454               xr_fsm_destroy (p->fsm);
455               p->fsm = NULL;
456               output_item_unref (p->item);
457               p->item = NULL;
458               return;
459             }
460           else if (!chunk)
461             {
462               assert (p->y > 0);
463               p->y = INT_MAX;
464               return;
465             }
466         }
467     }
468 }