cairo output works again
[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 \f
32 struct xr_page_style *
33 xr_page_style_ref (const struct xr_page_style *ps_)
34 {
35   struct xr_page_style *ps = CONST_CAST (struct xr_page_style *, ps_);
36   assert (ps->ref_cnt > 0);
37   ps->ref_cnt++;
38   return ps;
39 }
40
41 void
42 xr_page_style_unref (struct xr_page_style *ps)
43 {
44   if (ps)
45     {
46       assert (ps->ref_cnt > 0);
47       if (!--ps->ref_cnt)
48         {
49           for (int i = 0; i < 2; i++)
50             page_heading_uninit (&ps->headings[i]);
51           if (ps->font)
52             pango_font_description_free (ps->font);
53           free (ps);
54         }
55     }
56 }
57
58 static bool
59 pfd_equals (const PangoFontDescription *a,
60             const PangoFontDescription *b)
61 {
62   return a && b ? pango_font_description_equal (a, b) : !a && !b;
63 }
64
65 bool
66 xr_page_style_equals (const struct xr_page_style *a,
67                       const struct xr_page_style *b)
68 {
69   for (int i = 0; i < TABLE_N_AXES; i++)
70     {
71       if (a->size[i] != b->size[i])
72         return false;
73
74       for (int j = 0; j < 2; j++)
75         if (a->margins[i][j] != b->margins[i][j])
76           return false;
77     }
78
79   for (int i = 0; i < 2; i++)
80     if (!page_heading_equals (&a->headings[i], &b->headings[i]))
81       return false;
82
83   return (pfd_equals (a->font, b->font)
84           && a->font_scale == b->font_scale
85           && a->initial_page_number == b->initial_page_number
86           && a->object_spacing == b->object_spacing);
87 }
88 \f
89 struct xr_pager
90   {
91     struct xr_page_style *page_style;
92     int page_index;
93     int heading_heights[2];
94
95     /* Current output item. */
96     struct xr_fsm *fsm;
97     struct xr_fsm_style *fsm_style;
98     struct output_item *item;
99
100     /* Current output page. */
101     cairo_t *cr;
102     int y;
103   };
104
105 static void xr_pager_run (struct xr_pager *);
106
107 /* Conversions to and from points. */
108 static double
109 xr_to_pt (int x)
110 {
111   return x / (double) XR_POINT;
112 }
113
114 static int
115 pango_to_xr (int pango)
116 {
117   return (XR_POINT != PANGO_SCALE
118           ? ceil (pango * (1. * XR_POINT / PANGO_SCALE))
119           : pango);
120 }
121
122 static int
123 xr_to_pango (int xr)
124 {
125   return (XR_POINT != PANGO_SCALE
126           ? ceil (xr * (1. / XR_POINT * PANGO_SCALE))
127           : xr);
128 }
129
130 static int
131 get_layout_height (PangoLayout *layout)
132 {
133   int w, h;
134   pango_layout_get_size (layout, &w, &h);
135   return h;
136 }
137
138 static int
139 xr_render_page_heading (cairo_t *cairo, const PangoFontDescription *font,
140                         const struct page_heading *ph, int page_number,
141                         int width, bool draw, int base_y)
142 {
143   PangoLayout *layout = pango_cairo_create_layout (cairo);
144   pango_layout_set_font_description (layout, font);
145
146   int y = 0;
147   for (size_t i = 0; i < ph->n; i++)
148     {
149       const struct page_paragraph *pp = &ph->paragraphs[i];
150
151       char *markup = output_driver_substitute_heading_vars (pp->markup,
152                                                             page_number);
153       pango_layout_set_markup (layout, markup, -1);
154       free (markup);
155
156       pango_layout_set_alignment (
157         layout,
158         (pp->halign == TABLE_HALIGN_LEFT ? PANGO_ALIGN_LEFT
159          : pp->halign == TABLE_HALIGN_CENTER ? PANGO_ALIGN_CENTER
160          : pp->halign == TABLE_HALIGN_MIXED ? PANGO_ALIGN_LEFT
161          : PANGO_ALIGN_RIGHT));
162       pango_layout_set_width (layout, xr_to_pango (width));
163       if (draw)
164         {
165           cairo_save (cairo);
166           cairo_translate (cairo, 0, xr_to_pt (y + base_y));
167           pango_cairo_show_layout (cairo, layout);
168           cairo_restore (cairo);
169         }
170
171       y += pango_to_xr (get_layout_height (layout));
172     }
173
174   g_object_unref (G_OBJECT (layout));
175
176   return y;
177 }
178
179 static void
180 xr_measure_headings (struct xr_pager *p)
181 {
182   const struct xr_page_style *ps = p->page_style;
183
184   cairo_surface_t *surface = cairo_recording_surface_create (
185     CAIRO_CONTENT_COLOR, NULL);
186   cairo_t *cairo = cairo_create (surface);
187   for (int i = 0; i < 2; i++)
188     {
189       int h = xr_render_page_heading (cairo, ps->font, &ps->headings[i], -1,
190                                       ps->size[TABLE_HORZ], false, 0);
191
192       /* If the heading is nonempty, add a margin above or below it. */
193       if (h)
194         h += ps->object_spacing;
195
196       p->heading_heights[i] = h;
197     }
198   cairo_destroy (cairo);
199   cairo_surface_destroy (surface);
200
201   int total = p->heading_heights[0] + p->heading_heights[1];
202   if (total >= ps->size[TABLE_VERT])
203     p->heading_heights[0] = p->heading_heights[1] = 0;
204 }
205
206 struct xr_pager *
207 xr_pager_create (const struct xr_page_style *ps)
208 {
209   struct xr_pager *p = xmalloc (sizeof *p);
210   *p = (struct xr_pager) { .page_style = xr_page_style_ref (ps) };
211
212   xr_measure_headings (p);
213
214   return p;
215 }
216
217 void
218 xr_pager_destroy (struct xr_pager *p)
219 {
220   if (p)
221     {
222       xr_fsm_destroy (p->fsm);
223       xr_fsm_style_unref (p->fsm_style);
224       output_item_unref (p->item);
225
226       xr_page_style_unref (p->page_style);
227       if (p->cr)
228         cairo_destroy (p->cr);
229       free (p);
230     }
231 }
232
233 bool
234 xr_pager_has_item (const struct xr_pager *p)
235 {
236   return p->item != NULL;
237 }
238
239 void
240 xr_pager_add_item (struct xr_pager *p, const struct xr_fsm_style *fsm_style,
241                    const struct output_item *item)
242 {
243   assert (!p->item);
244   p->item = output_item_ref (item);
245   p->fsm_style = xr_fsm_style_ref (fsm_style);
246   xr_pager_run (p);
247 }
248
249 bool
250 xr_pager_has_page (const struct xr_pager *p)
251 {
252   return p->cr;
253 }
254
255 void
256 xr_pager_add_page (struct xr_pager *p, cairo_t *cr)
257 {
258   assert (!p->cr);
259   p->cr = cr;
260   p->y = 0;
261
262   const struct xr_page_style *ps = p->page_style;
263   const struct cell_color *bg = &ps->bg;
264   if (bg->alpha)
265     {
266       cairo_save (cr);
267       cairo_set_source_rgba (cr, bg->r / 255.0, bg->g / 255.0,
268                           bg->b / 255.0, bg->alpha / 255.0);
269       cairo_rectangle (cr, 0, 0, ps->size[TABLE_HORZ], ps->size[TABLE_VERT]);
270       cairo_fill (cr);
271       cairo_restore (cr);
272     }
273   cairo_translate (cr,
274                    xr_to_pt (ps->margins[TABLE_HORZ][0]),
275                    xr_to_pt (ps->margins[TABLE_VERT][0]));
276
277   int page_number = p->page_index++ + ps->initial_page_number;
278   if (p->heading_heights[0])
279     xr_render_page_heading (cr, ps->font, &ps->headings[0], page_number,
280                             ps->size[TABLE_HORZ], true, 0);
281
282   if (p->heading_heights[1])
283     xr_render_page_heading (
284       cr, ps->font, &ps->headings[1], page_number,
285       ps->size[TABLE_HORZ], true,
286       ps->size[TABLE_VERT] - p->heading_heights[1] + ps->object_spacing);
287
288   cairo_translate (cr, 0, xr_to_pt (p->heading_heights[0]));
289
290   xr_pager_run (p);
291 }
292
293 bool
294 xr_pager_needs_new_page (struct xr_pager *p)
295 {
296   int vsize = (p->page_style->size[TABLE_VERT]
297                - p->heading_heights[0] - p->heading_heights[1]);
298   if (p->item && p->y >= vsize)
299     {
300       cairo_destroy (p->cr);
301       p->cr = NULL;
302       return true;
303     }
304   else
305     return false;
306 }
307
308 static void
309 xr_pager_run (struct xr_pager *p)
310 {
311   int vsize = (p->page_style->size[TABLE_VERT]
312                - p->heading_heights[0] - p->heading_heights[1]);
313   if (p->item && p->cr && p->y < vsize)
314     {
315       if (!p->fsm)
316         {
317           /* XXX fsm_style needs to account for heading_heights */
318           p->fsm = xr_fsm_create (p->item, p->fsm_style, p->cr);
319           if (!p->fsm)
320             {
321               xr_fsm_style_unref (p->fsm_style);
322               p->fsm_style = NULL;
323               output_item_unref (p->item);
324               p->item = NULL;
325
326               return;
327             }
328         }
329
330       for (;;)
331         {
332           int spacing = p->page_style->object_spacing;
333           int chunk = xr_fsm_draw_slice (p->fsm, p->cr, vsize - p->y);
334           p->y += chunk + spacing;
335           cairo_translate (p->cr, 0, xr_to_pt (chunk + spacing));
336
337           if (xr_fsm_is_empty (p->fsm))
338             {
339               xr_fsm_destroy (p->fsm);
340               p->fsm = NULL;
341               xr_fsm_style_unref (p->fsm_style);
342               p->fsm_style = NULL;
343               output_item_unref (p->item);
344               p->item = NULL;
345               return;
346             }
347           else if (!chunk)
348             {
349               assert (p->y > 0);
350               p->y = INT_MAX;
351               return;
352             }
353         }
354     }
355 }