work on docs
[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_iterator iter;
105     struct output_item *root_item;
106     int slice_idx;
107     const char *label;
108
109     /* Grouping, for constructing the outline for PDFs. */
110     struct outline_node
111       {
112         const struct output_item *item;
113         int group_id;
114       }
115     *nodes;
116     size_t n_nodes, allocated_nodes;
117
118     /* Current output page. */
119     cairo_t *cr;
120     int y;
121   };
122
123 static void xr_pager_run (struct xr_pager *);
124
125 /* Conversions to and from points. */
126 static double
127 xr_to_pt (int x)
128 {
129   return x / (double) XR_POINT;
130 }
131
132 static int
133 pango_to_xr (int pango)
134 {
135   return (XR_POINT != PANGO_SCALE
136           ? ceil (pango * (1. * XR_POINT / PANGO_SCALE))
137           : pango);
138 }
139
140 static int
141 xr_to_pango (int xr)
142 {
143   return (XR_POINT != PANGO_SCALE
144           ? ceil (xr * (1. / XR_POINT * PANGO_SCALE))
145           : xr);
146 }
147
148 static int
149 get_layout_height (PangoLayout *layout)
150 {
151   int w, h;
152   pango_layout_get_size (layout, &w, &h);
153   return h;
154 }
155
156 static int
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)
160 {
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);
165
166   pango_layout_set_font_description (layout, font);
167
168   int y = 0;
169   for (size_t i = 0; i < ph->n; i++)
170     {
171       const struct page_paragraph *pp = &ph->paragraphs[i];
172
173       char *markup = output_driver_substitute_heading_vars (pp->markup,
174                                                             page_number);
175       pango_layout_set_markup (layout, markup, -1);
176       free (markup);
177
178       pango_layout_set_alignment (
179         layout,
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));
185
186       cairo_save (cairo);
187       cairo_translate (cairo, 0, xr_to_pt (y + base_y));
188       pango_cairo_show_layout (cairo, layout);
189       cairo_restore (cairo);
190
191       y += pango_to_xr (get_layout_height (layout));
192     }
193
194   g_object_unref (G_OBJECT (layout));
195
196   return y;
197 }
198
199 static void
200 xr_measure_headings (const struct xr_page_style *ps,
201                      const struct xr_fsm_style *fs,
202                      int heading_heights[2])
203 {
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++)
208     {
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);
213       if (*h)
214         *h += fs->object_spacing;
215     }
216   cairo_destroy (cairo);
217   cairo_surface_destroy (surface);
218 }
219
220 struct xr_pager *
221 xr_pager_create (const struct xr_page_style *ps_,
222                  const struct xr_fsm_style *fs_)
223 {
224   struct xr_page_style *ps = xr_page_style_ref (ps_);
225   struct xr_fsm_style *fs = xr_fsm_style_ref (fs_);
226
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])
231     {
232       fs = xr_fsm_style_unshare (fs);
233       ps = xr_page_style_unshare (ps);
234
235       for (int i = 0; i < 2; i++)
236         ps->margins[V][i] += heading_heights[i];
237       fs->size[V] -= total;
238     }
239
240   struct xr_pager *p = xmalloc (sizeof *p);
241   *p = (struct xr_pager) { .page_style = ps, .fsm_style = fs };
242   return p;
243 }
244
245 void
246 xr_pager_destroy (struct xr_pager *p)
247 {
248   if (p)
249     {
250       free (p->nodes);
251
252       xr_page_style_unref (p->page_style);
253       xr_fsm_style_unref (p->fsm_style);
254
255       xr_fsm_destroy (p->fsm);
256       output_iterator_destroy (&p->iter);
257       output_item_unref (p->root_item);
258
259       if (p->cr)
260         {
261           cairo_restore (p->cr);
262           cairo_destroy (p->cr);
263         }
264       free (p);
265     }
266 }
267
268 bool
269 xr_pager_has_item (const struct xr_pager *p)
270 {
271   return p->root_item != NULL;
272 }
273
274 void
275 xr_pager_add_item (struct xr_pager *p, const struct output_item *item)
276 {
277   assert (!p->root_item);
278   p->root_item = output_item_ref (item);
279   output_iterator_init (&p->iter, item);
280   xr_pager_run (p);
281 }
282
283 bool
284 xr_pager_has_page (const struct xr_pager *p)
285 {
286   return p->cr;
287 }
288
289 void
290 xr_pager_add_page (struct xr_pager *p, cairo_t *cr)
291 {
292   assert (!p->cr);
293   cairo_save (cr);
294   p->cr = cr;
295   p->y = 0;
296
297   const struct xr_fsm_style *fs = p->fsm_style;
298   const struct xr_page_style *ps = p->page_style;
299   cairo_translate (cr,
300                    xr_to_pt (ps->margins[H][0]),
301                    xr_to_pt (ps->margins[V][0]));
302
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);
308
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);
313
314   cairo_surface_t *surface = cairo_get_target (cr);
315   if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_PDF)
316     {
317       char *page_label = xasprintf ("%d", page_number);
318       cairo_pdf_surface_set_page_label (surface, page_label);
319       free (page_label);
320     }
321
322   xr_pager_run (p);
323 }
324
325 void
326 xr_pager_finish_page (struct xr_pager *p)
327 {
328   if (p->cr)
329     {
330       cairo_restore (p->cr);
331       cairo_destroy (p->cr);
332       p->cr = NULL;
333     }
334 }
335
336 bool
337 xr_pager_needs_new_page (struct xr_pager *p)
338 {
339   if (p->root_item && (!p->cr || p->y >= p->fsm_style->size[V]))
340     {
341       xr_pager_finish_page (p);
342       return true;
343     }
344   else
345     return false;
346 }
347
348 static int
349 add_outline (cairo_t *cr, int parent_id,
350              const char *utf8, const char *link_attribs,
351              cairo_pdf_outline_flags_t flags)
352 {
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)
357           : 0);
358 }
359
360 static void
361 xr_pager_run (struct xr_pager *p)
362 {
363   if (!p->root_item || !p->cr || p->y >= p->fsm_style->size[V])
364     return;
365
366   for (;;)
367     {
368       /* Make sure we've got an object to render. */
369       while (!p->fsm)
370         {
371           /* If there are no remaining objects to render, then we're done. */
372           if (!p->iter.cur)
373             {
374               output_item_unref (p->root_item);
375               p->root_item = NULL;
376               return;
377             }
378
379           /* Prepare to render the current object. */
380           p->fsm = xr_fsm_create_for_printing (p->iter.cur, p->fsm_style,
381                                                p->cr);
382           p->label = output_item_get_label (p->iter.cur);
383           p->slice_idx = 0;
384           while (p->n_nodes > p->iter.n)
385             p->n_nodes--;
386           while (p->n_nodes)
387             {
388               size_t i = p->n_nodes - 1;
389               if (p->nodes[i].item == p->iter.nodes[i].group)
390                 break;
391
392               p->nodes--;
393             }
394           while (p->n_nodes < p->iter.n)
395             {
396               if (p->n_nodes >= p->allocated_nodes)
397                 p->nodes = x2nrealloc (p->nodes, &p->allocated_nodes,
398                                        sizeof *p->nodes);
399               size_t i = p->n_nodes++;
400               p->nodes[i] = (struct outline_node) {
401                 .item = p->iter.nodes[i].group,
402               };
403             }
404           output_iterator_next (&p->iter);
405         }
406
407       char *dest_name = NULL;
408       if (p->page_style->include_outline)
409         {
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);
414           free (attrs);
415         }
416
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));
422
423       if (p->page_style->include_outline)
424         {
425           cairo_tag_end (p->cr, CAIRO_TAG_DEST);
426
427           if (chunk && p->slice_idx++ == 0)
428             {
429               char *attrs = xasprintf ("dest='%s'", dest_name);
430
431               int parent_group_id = CAIRO_PDF_OUTLINE_ROOT;
432               for (size_t i = 0; i < p->n_nodes; i++)
433                 {
434                   struct outline_node *node = &p->nodes[i];
435                   if (!node->group_id)
436                     {
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);
441                     }
442                   parent_group_id = node->group_id;
443                 }
444
445               add_outline (p->cr, parent_group_id, p->label, attrs, 0);
446               free (attrs);
447             }
448           free (dest_name);
449         }
450
451       if (xr_fsm_is_empty (p->fsm))
452         {
453           xr_fsm_destroy (p->fsm);
454           p->fsm = NULL;
455         }
456       else if (!chunk)
457         {
458           assert (p->y > 0);
459           p->y = INT_MAX;
460           return;
461         }
462     }
463 }