8a0f948cc86ab89a70495b2add3273cc64c2c211
[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 <pango/pango-layout.h>
23 #include <pango/pangocairo.h>
24
25 #include "output/driver-provider.h"
26
27 #include "gl/xalloc.h"
28
29 #include "gettext.h"
30 #define _(msgid) gettext (msgid)
31
32 /* This file uses TABLE_HORZ and TABLE_VERT enough to warrant abbreviating. */
33 #define H TABLE_HORZ
34 #define V TABLE_VERT
35 \f
36 struct xr_page_style *
37 xr_page_style_ref (const struct xr_page_style *ps_)
38 {
39   struct xr_page_style *ps = CONST_CAST (struct xr_page_style *, ps_);
40   assert (ps->ref_cnt > 0);
41   ps->ref_cnt++;
42   return ps;
43 }
44
45 struct xr_page_style *
46 xr_page_style_unshare (struct xr_page_style *old)
47 {
48   assert (old->ref_cnt > 0);
49   if (old->ref_cnt == 1)
50     return old;
51
52   xr_page_style_unref (old);
53
54   struct xr_page_style *new = xmemdup (old, sizeof *old);
55   new->ref_cnt = 1;
56   for (int i = 0; i < 2; i++)
57     page_heading_copy (&new->headings[i], &old->headings[i]);
58   if (old->font)
59     new->font = pango_font_description_copy (old->font);
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           if (ps->font)
75             pango_font_description_free (ps->font);
76           free (ps);
77         }
78     }
79 }
80
81 static bool
82 pfd_equals (const PangoFontDescription *a,
83             const PangoFontDescription *b)
84 {
85   return a && b ? pango_font_description_equal (a, b) : !a && !b;
86 }
87
88 bool
89 xr_page_style_equals (const struct xr_page_style *a,
90                       const struct xr_page_style *b)
91 {
92   for (int i = 0; i < TABLE_N_AXES; i++)
93     {
94       if (a->size[i] != b->size[i])
95         return false;
96
97       for (int j = 0; j < 2; j++)
98         if (a->margins[i][j] != b->margins[i][j])
99           return false;
100     }
101
102   for (int i = 0; i < 2; i++)
103     if (!page_heading_equals (&a->headings[i], &b->headings[i]))
104       return false;
105
106   return (pfd_equals (a->font, b->font)
107           && a->font_scale == b->font_scale
108           && a->initial_page_number == b->initial_page_number
109           && a->object_spacing == b->object_spacing);
110 }
111 \f
112 struct xr_pager
113   {
114     struct xr_page_style *page_style;
115     int page_index;
116     int heading_heights[2];
117
118     /* Current output item. */
119     struct xr_fsm *fsm;
120     struct xr_fsm_style *fsm_style;
121     struct output_item *item;
122
123     /* Current output page. */
124     cairo_t *cr;
125     int y;
126   };
127
128 static void xr_pager_run (struct xr_pager *);
129
130 /* Conversions to and from points. */
131 static double
132 xr_to_pt (int x)
133 {
134   return x / (double) XR_POINT;
135 }
136
137 static int
138 pango_to_xr (int pango)
139 {
140   return (XR_POINT != PANGO_SCALE
141           ? ceil (pango * (1. * XR_POINT / PANGO_SCALE))
142           : pango);
143 }
144
145 static int
146 xr_to_pango (int xr)
147 {
148   return (XR_POINT != PANGO_SCALE
149           ? ceil (xr * (1. / XR_POINT * PANGO_SCALE))
150           : xr);
151 }
152
153 static int
154 get_layout_height (PangoLayout *layout)
155 {
156   int w, h;
157   pango_layout_get_size (layout, &w, &h);
158   return h;
159 }
160
161 static int
162 xr_render_page_heading (cairo_t *cairo, const PangoFontDescription *font,
163                         const struct page_heading *ph, int page_number,
164                         int width, bool draw, int base_y)
165 {
166   PangoLayout *layout = pango_cairo_create_layout (cairo);
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       if (draw)
187         {
188           cairo_save (cairo);
189           cairo_translate (cairo, 0, xr_to_pt (y + base_y));
190           pango_cairo_show_layout (cairo, layout);
191           cairo_restore (cairo);
192         }
193
194       y += pango_to_xr (get_layout_height (layout));
195     }
196
197   g_object_unref (G_OBJECT (layout));
198
199   return y;
200 }
201
202 static void
203 xr_measure_headings (const struct xr_page_style *ps, 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, ps->font, &ps->headings[i], -1,
212                                    ps->size[H], false, 0);
213       if (*h)
214         *h += ps->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 {
223   struct xr_page_style *ps = xr_page_style_ref (ps_);
224
225   int heading_heights[2];
226   xr_measure_headings (ps, heading_heights);
227   int total = heading_heights[0] + heading_heights[1];
228   if (total > 0 && total < ps->size[V])
229     {
230       ps = xr_page_style_unshare (ps);
231
232       for (int i = 0; i < 2; i++)
233         ps->margins[V][i] += heading_heights[i];
234       ps->size[V] -= total;
235     }
236
237   struct xr_pager *p = xmalloc (sizeof *p);
238   *p = (struct xr_pager) { .page_style = ps };
239   return p;
240 }
241
242 void
243 xr_pager_destroy (struct xr_pager *p)
244 {
245   if (p)
246     {
247       xr_fsm_destroy (p->fsm);
248       xr_fsm_style_unref (p->fsm_style);
249       output_item_unref (p->item);
250
251       xr_page_style_unref (p->page_style);
252       if (p->cr)
253         cairo_destroy (p->cr);
254       free (p);
255     }
256 }
257
258 bool
259 xr_pager_has_item (const struct xr_pager *p)
260 {
261   return p->item != NULL;
262 }
263
264 void
265 xr_pager_add_item (struct xr_pager *p, const struct xr_fsm_style *fsm_style,
266                    const struct output_item *item)
267 {
268   assert (!p->item);
269   p->item = output_item_ref (item);
270   p->fsm_style = xr_fsm_style_ref (fsm_style);
271   xr_pager_run (p);
272 }
273
274 bool
275 xr_pager_has_page (const struct xr_pager *p)
276 {
277   return p->cr;
278 }
279
280 void
281 xr_pager_add_page (struct xr_pager *p, cairo_t *cr)
282 {
283   assert (!p->cr);
284   p->cr = cr;
285   p->y = 0;
286
287   const struct xr_page_style *ps = p->page_style;
288   const struct cell_color *bg = &ps->bg;
289   if (bg->alpha)
290     {
291       cairo_save (cr);
292       cairo_set_source_rgba (cr, bg->r / 255.0, bg->g / 255.0,
293                           bg->b / 255.0, bg->alpha / 255.0);
294       cairo_rectangle (cr, 0, 0, ps->size[H], ps->size[V]);
295       cairo_fill (cr);
296       cairo_restore (cr);
297     }
298   cairo_translate (cr,
299                    xr_to_pt (ps->margins[H][0]),
300                    xr_to_pt (ps->margins[V][0]));
301
302   int page_number = p->page_index++ + ps->initial_page_number;
303   if (p->heading_heights[0])
304     xr_render_page_heading (cr, ps->font, &ps->headings[0], page_number,
305                             ps->size[H], true, -p->heading_heights[0]);
306
307   if (p->heading_heights[1])
308     xr_render_page_heading (cr, ps->font, &ps->headings[1], page_number,
309                             ps->size[H], true,
310                             ps->size[V] + ps->object_spacing);
311
312   xr_pager_run (p);
313 }
314
315 bool
316 xr_pager_needs_new_page (struct xr_pager *p)
317 {
318   if (p->item && p->y >= p->page_style->size[V])
319     {
320       cairo_destroy (p->cr);
321       p->cr = NULL;
322       return true;
323     }
324   else
325     return false;
326 }
327
328 static void
329 xr_pager_run (struct xr_pager *p)
330 {
331   if (p->item && p->cr && p->y < p->page_style->size[V])
332     {
333       if (!p->fsm)
334         {
335           /* XXX fsm_style needs to account for heading_heights */
336           p->fsm = xr_fsm_create (p->item, p->fsm_style, p->cr);
337           if (!p->fsm)
338             {
339               xr_fsm_style_unref (p->fsm_style);
340               p->fsm_style = NULL;
341               output_item_unref (p->item);
342               p->item = NULL;
343
344               return;
345             }
346         }
347
348       for (;;)
349         {
350           int spacing = p->page_style->object_spacing;
351           int chunk = xr_fsm_draw_slice (p->fsm, p->cr,
352                                          p->page_style->size[V] - p->y);
353           p->y += chunk + spacing;
354           cairo_translate (p->cr, 0, xr_to_pt (chunk + spacing));
355
356           if (xr_fsm_is_empty (p->fsm))
357             {
358               xr_fsm_destroy (p->fsm);
359               p->fsm = NULL;
360               xr_fsm_style_unref (p->fsm_style);
361               p->fsm_style = NULL;
362               output_item_unref (p->item);
363               p->item = NULL;
364               return;
365             }
366           else if (!chunk)
367             {
368               assert (p->y > 0);
369               p->y = INT_MAX;
370               return;
371             }
372         }
373     }
374 }