3e5f962bd3b08be527cbe6f1f029bf1410fe9a57
[pspp] / src / output / spv / spv-table-look.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2017, 2018, 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/spv/spv-table-look.h"
20
21 #include <errno.h>
22 #include <inttypes.h>
23 #include <libxml/xmlreader.h>
24 #include <libxml/xmlwriter.h>
25 #include <string.h>
26
27 #include "libpspp/i18n.h"
28 #include "output/spv/structure-xml-parser.h"
29 #include "output/spv/tlo-parser.h"
30 #include "output/pivot-table.h"
31 #include "output/table.h"
32
33 #include "gl/read-file.h"
34 #include "gl/xalloc.h"
35 #include "gl/xmemdup0.h"
36
37 #include "gettext.h"
38 #define _(msgid) gettext (msgid)
39
40 static struct cell_color
41 optional_color (int color, struct cell_color default_color)
42 {
43   return (color >= 0
44           ? (struct cell_color) CELL_COLOR (color >> 16, color >> 8, color)
45           : default_color);
46 }
47
48 static int
49 optional_length (const char *s, int default_length)
50 {
51   /* There is usually a "pt" suffix.  We ignore it. */
52   int length;
53   return s && sscanf (s, "%d", &length) == 1 ? length : default_length;
54 }
55
56 static int
57 optional_px (double inches, int default_px)
58 {
59   return inches != DBL_MAX ? inches * 96.0 : default_px;
60 }
61
62 static int
63 optional_int (int x, int default_value)
64 {
65   return x != INT_MIN ? x : default_value;
66 }
67
68 static int
69 optional_pt (double inches, int default_pt)
70 {
71   return inches != DBL_MAX ? inches * 72.0 + .5 : default_pt;
72 }
73
74 static const char *pivot_area_names[PIVOT_N_AREAS] = {
75   [PIVOT_AREA_TITLE] = "title",
76   [PIVOT_AREA_CAPTION] = "caption",
77   [PIVOT_AREA_FOOTER] = "footnotes",
78   [PIVOT_AREA_CORNER] = "cornerLabels",
79   [PIVOT_AREA_COLUMN_LABELS] = "columnLabels",
80   [PIVOT_AREA_ROW_LABELS] = "rowLabels",
81   [PIVOT_AREA_DATA] = "data",
82   [PIVOT_AREA_LAYERS] = "layers",
83 };
84
85 static enum pivot_area
86 pivot_area_from_name (const char *name)
87 {
88   enum pivot_area area;
89   for (area = 0; area < PIVOT_N_AREAS; area++)
90     if (!strcmp (name, pivot_area_names[area]))
91       break;
92   return area;
93 }
94
95 static const char *pivot_border_names[PIVOT_N_BORDERS] = {
96   [PIVOT_BORDER_TITLE] = "titleLayerSeparator",
97   [PIVOT_BORDER_OUTER_LEFT] = "leftOuterFrame",
98   [PIVOT_BORDER_OUTER_TOP] = "topOuterFrame",
99   [PIVOT_BORDER_OUTER_RIGHT] = "rightOuterFrame",
100   [PIVOT_BORDER_OUTER_BOTTOM] = "bottomOuterFrame",
101   [PIVOT_BORDER_INNER_LEFT] = "leftInnerFrame",
102   [PIVOT_BORDER_INNER_TOP] = "topInnerFrame",
103   [PIVOT_BORDER_INNER_RIGHT] = "rightInnerFrame",
104   [PIVOT_BORDER_INNER_BOTTOM] = "bottomInnerFrame",
105   [PIVOT_BORDER_DATA_LEFT] = "dataAreaLeft",
106   [PIVOT_BORDER_DATA_TOP] = "dataAreaTop",
107   [PIVOT_BORDER_DIM_ROW_HORZ] = "horizontalDimensionBorderRows",
108   [PIVOT_BORDER_DIM_ROW_VERT] = "verticalDimensionBorderRows",
109   [PIVOT_BORDER_DIM_COL_HORZ] = "horizontalDimensionBorderColumns",
110   [PIVOT_BORDER_DIM_COL_VERT] = "verticalDimensionBorderColumns",
111   [PIVOT_BORDER_CAT_ROW_HORZ] = "horizontalCategoryBorderRows",
112   [PIVOT_BORDER_CAT_ROW_VERT] = "verticalCategoryBorderRows",
113   [PIVOT_BORDER_CAT_COL_HORZ] = "horizontalCategoryBorderColumns",
114   [PIVOT_BORDER_CAT_COL_VERT] = "verticalCategoryBorderColumns",
115 };
116
117 static enum pivot_border
118 pivot_border_from_name (const char *name)
119 {
120   enum pivot_border border;
121   for (border = 0; border < PIVOT_N_BORDERS; border++)
122     if (!strcmp (name, pivot_border_names[border]))
123       break;
124   return border;
125 }
126
127 char * WARN_UNUSED_RESULT
128 spv_table_look_decode (const struct spvsx_table_properties *in,
129                        struct pivot_table_look **outp)
130 {
131   struct pivot_table_look *out = pivot_table_look_new_builtin_default ();
132   char *error = NULL;
133
134   out->name = in->name ? xstrdup (in->name) : NULL;
135
136   const struct spvsx_general_properties *g = in->general_properties;
137   out->omit_empty = g->hide_empty_rows != 0;
138   out->width_ranges[TABLE_HORZ][0] = optional_pt (g->minimum_column_width, -1);
139   out->width_ranges[TABLE_HORZ][1] = optional_pt (g->maximum_column_width, -1);
140   out->width_ranges[TABLE_VERT][0] = optional_pt (g->minimum_row_width, -1);
141   out->width_ranges[TABLE_VERT][1] = optional_pt (g->maximum_row_width, -1);
142   out->row_labels_in_corner
143     = g->row_dimension_labels != SPVSX_ROW_DIMENSION_LABELS_NESTED;
144
145   const struct spvsx_footnote_properties *f = in->footnote_properties;
146   out->footnote_marker_superscripts
147     = (f->marker_position != SPVSX_MARKER_POSITION_SUBSCRIPT);
148   out->show_numeric_markers
149     = (f->number_format == SPVSX_NUMBER_FORMAT_NUMERIC);
150
151   const struct spvsx_cell_format_properties *cfp = in->cell_format_properties;
152   for (size_t i = 0; i < cfp->n_cell_style; i++)
153     {
154       const struct spvsx_cell_style *c = cfp->cell_style[i];
155       const char *name = CHAR_CAST (const char *, c->node_.raw->name);
156       enum pivot_area area = pivot_area_from_name (name);
157       if (area == PIVOT_N_AREAS)
158         {
159           error = xasprintf ("unknown area \"%s\" in cellFormatProperties",
160                              name);
161           goto error;
162         }
163
164       struct table_area_style *a = &out->areas[area];
165       const struct spvsx_style *s = c->style;
166       if (s->font_weight)
167         a->font_style.bold = s->font_weight == SPVSX_FONT_WEIGHT_BOLD;
168       if (s->font_style)
169         a->font_style.italic = s->font_style == SPVSX_FONT_STYLE_ITALIC;
170       if (s->font_underline)
171         a->font_style.underline
172           = s->font_underline == SPVSX_FONT_UNDERLINE_UNDERLINE;
173       if (s->color >= 0)
174         a->font_style.fg[0] = optional_color (
175           s->color, (struct cell_color) CELL_COLOR_BLACK);
176       if (c->alternating_text_color >= 0 || s->color >= 0)
177         a->font_style.fg[1] = optional_color (c->alternating_text_color,
178                                               a->font_style.fg[0]);
179       if (s->color2 >= 0)
180         a->font_style.bg[0] = optional_color (
181           s->color2, (struct cell_color) CELL_COLOR_WHITE);
182       if (c->alternating_color >= 0 || s->color2 >= 0)
183         a->font_style.bg[1] = optional_color (c->alternating_color,
184                                               a->font_style.bg[0]);
185       if (s->font_family)
186         {
187           free (a->font_style.typeface);
188           a->font_style.typeface = xstrdup (s->font_family);
189         }
190
191       if (s->font_size)
192         a->font_style.size = optional_length (s->font_size, 0);
193
194       if (s->text_alignment)
195         a->cell_style.halign
196           = (s->text_alignment == SPVSX_TEXT_ALIGNMENT_LEFT
197              ? TABLE_HALIGN_LEFT
198              : s->text_alignment == SPVSX_TEXT_ALIGNMENT_RIGHT
199              ? TABLE_HALIGN_RIGHT
200              : s->text_alignment == SPVSX_TEXT_ALIGNMENT_CENTER
201              ? TABLE_HALIGN_CENTER
202              : s->text_alignment == SPVSX_TEXT_ALIGNMENT_DECIMAL
203              ? TABLE_HALIGN_DECIMAL
204              : TABLE_HALIGN_MIXED);
205       if (s->label_location_vertical)
206         a->cell_style.valign
207           = (s->label_location_vertical == SPVSX_LABEL_LOCATION_VERTICAL_NEGATIVE
208              ? TABLE_VALIGN_BOTTOM
209              : s->label_location_vertical == SPVSX_LABEL_LOCATION_VERTICAL_POSITIVE
210              ? TABLE_VALIGN_TOP
211              : TABLE_VALIGN_CENTER);
212
213       if (s->decimal_offset != DBL_MAX)
214         a->cell_style.decimal_offset = optional_px (s->decimal_offset, 0);
215
216       if (s->margin_left != DBL_MAX)
217         a->cell_style.margin[TABLE_HORZ][0] = optional_px (s->margin_left, 8);
218       if (s->margin_right != DBL_MAX)
219         a->cell_style.margin[TABLE_HORZ][1] = optional_px (s->margin_right,
220                                                            11);
221       if (s->margin_top != DBL_MAX)
222         a->cell_style.margin[TABLE_VERT][0] = optional_px (s->margin_top, 1);
223       if (s->margin_bottom != DBL_MAX)
224         a->cell_style.margin[TABLE_VERT][1] = optional_px (s->margin_bottom,
225                                                            1);
226     }
227
228   const struct spvsx_border_properties *bp = in->border_properties;
229   for (size_t i = 0; i < bp->n_border_style; i++)
230     {
231       const struct spvsx_border_style *bin = bp->border_style[i];
232       const char *name = CHAR_CAST (const char *, bin->node_.raw->name);
233       enum pivot_border border = pivot_border_from_name (name);
234       if (border == PIVOT_N_BORDERS)
235         {
236           error = xasprintf ("unknown border \"%s\" parsing borderProperties",
237                              name);
238           goto error;
239         }
240
241       struct table_border_style *bout = &out->borders[border];
242       bout->stroke
243         = (bin->border_style_type == SPVSX_BORDER_STYLE_TYPE_NONE
244            ? TABLE_STROKE_NONE
245            : bin->border_style_type == SPVSX_BORDER_STYLE_TYPE_DASHED
246            ? TABLE_STROKE_DASHED
247            : bin->border_style_type == SPVSX_BORDER_STYLE_TYPE_THICK
248            ? TABLE_STROKE_THICK
249            : bin->border_style_type == SPVSX_BORDER_STYLE_TYPE_THIN
250            ? TABLE_STROKE_THIN
251            : bin->border_style_type == SPVSX_BORDER_STYLE_TYPE_DOUBLE
252            ? TABLE_STROKE_DOUBLE
253            : TABLE_STROKE_SOLID);
254       bout->color = optional_color (bin->color,
255                                     (struct cell_color) CELL_COLOR_BLACK);
256     }
257
258   const struct spvsx_printing_properties *pp = in->printing_properties;
259   out->print_all_layers = pp->print_all_layers > 0;
260   out->paginate_layers = pp->print_each_layer_on_separate_page > 0;
261   out->shrink_to_fit[TABLE_HORZ] = pp->rescale_wide_table_to_fit_page > 0;
262   out->shrink_to_fit[TABLE_VERT] = pp->rescale_long_table_to_fit_page > 0;
263   out->top_continuation = pp->continuation_text_at_top > 0;
264   out->bottom_continuation = pp->continuation_text_at_bottom > 0;
265   free (out->continuation);
266   out->continuation = xstrdup (pp->continuation_text
267                                ? pp->continuation_text : "(cont.)");
268   out->n_orphan_lines = optional_int (pp->window_orphan_lines, 2);
269
270   *outp = out;
271   return NULL;
272
273 error:
274   pivot_table_look_unref (out);
275   *outp = NULL;
276   return error;
277 }
278 \f
279 static struct cell_color
280 tlo_decode_color (uint32_t c)
281 {
282   return (struct cell_color) CELL_COLOR (c, c >> 8, c >> 16);
283 }
284
285 static void
286 tlo_decode_border (const struct tlo_separator *in,
287                    struct table_border_style *out)
288 {
289   if (in->type == 0)
290     {
291       out->stroke = TABLE_STROKE_NONE;
292       return;
293     }
294
295   out->color = tlo_decode_color (in->type_01.color);
296
297   switch (in->type_01.style)
298     {
299     case 0:
300       out->stroke = (in->type_01.width == 0 ? TABLE_STROKE_THIN
301                      : in->type_01.width == 1 ? TABLE_STROKE_SOLID
302                      : TABLE_STROKE_THICK);
303       break;
304
305     case 1:
306       out->stroke = TABLE_STROKE_DOUBLE;
307       break;
308
309     case 2:
310       out->stroke = TABLE_STROKE_DASHED;
311       break;
312     }
313 }
314
315 static struct cell_color
316 interpolate_colors (struct cell_color c0, struct cell_color c1, int shading)
317 {
318   if (shading <= 0)
319     return c0;
320   else if (shading >= 10)
321     return c1;
322   else
323     {
324       int x0 = 10 - shading;
325       int x1 = shading;
326
327       return (struct cell_color) CELL_COLOR ((c0.r * x0 + c1.r * x1) / 10,
328                                              (c0.g * x0 + c1.g * x1) / 10,
329                                              (c0.b * x0 + c1.b * x1) / 10);
330     }
331 }
332
333 static void
334 tlo_decode_area (const struct tlo_area_color *color,
335                  const struct tlo_area_style *style,
336                  struct table_area_style *out)
337 {
338   out->cell_style.halign = (style->halign == 0 ? TABLE_HALIGN_LEFT
339                             : style->halign == 1 ? TABLE_HALIGN_RIGHT
340                             : style->halign == 2 ? TABLE_HALIGN_CENTER
341                             : style->halign == 4 ? TABLE_HALIGN_DECIMAL
342                             : TABLE_HALIGN_MIXED);
343   out->cell_style.valign = (style->valign == 0 ? TABLE_VALIGN_TOP
344                             : style->valign == 1 ? TABLE_VALIGN_BOTTOM
345                             : TABLE_VALIGN_CENTER);
346   out->cell_style.decimal_offset = style->decimal_offset / 20;
347   out->cell_style.decimal_char = '.';                  /* XXX */
348   out->cell_style.margin[TABLE_HORZ][0] = style->left_margin / 20;
349   out->cell_style.margin[TABLE_HORZ][1] = style->right_margin / 20;
350   out->cell_style.margin[TABLE_VERT][0] = style->top_margin / 20;
351   out->cell_style.margin[TABLE_VERT][1] = style->bottom_margin / 20;
352
353   out->font_style.bold = style->weight > 400;
354   out->font_style.italic = style->italic;
355   out->font_style.underline = style->underline;
356   out->font_style.markup = false;
357
358   out->font_style.fg[0] = out->font_style.fg[1]
359     = tlo_decode_color (style->text_color);
360
361   struct cell_color c0 = tlo_decode_color (color->color0);
362   struct cell_color c10 = tlo_decode_color (color->color10);
363   struct cell_color bg = interpolate_colors (c0, c10, color->shading);
364   out->font_style.bg[0] = out->font_style.bg[1] = bg;
365
366   free (out->font_style.typeface);
367   out->font_style.typeface = recode_string (
368     "UTF-8", "ISO-8859-1",
369     CHAR_CAST (char *, style->font_name), style->font_name_len);
370   out->font_style.size = -style->font_size * 3 / 4;
371 }
372
373 static struct pivot_table_look *
374 tlo_decode (const struct tlo_table_look *in)
375 {
376   struct pivot_table_look *out = pivot_table_look_new_builtin_default ();
377
378   const uint16_t flags = in->tl->flags;
379
380   out->omit_empty = (flags & 0x02) != 0;
381   out->row_labels_in_corner = !in->tl->nested_row_labels;
382   if (in->v2_styles)
383     {
384       out->width_ranges[TABLE_HORZ][0] = in->v2_styles->min_col_width;
385       out->width_ranges[TABLE_HORZ][1] = in->v2_styles->max_col_width;
386       out->width_ranges[TABLE_VERT][0] = in->v2_styles->min_row_height;
387       out->width_ranges[TABLE_VERT][1] = in->v2_styles->max_row_height;
388     }
389   else
390     {
391       out->width_ranges[TABLE_HORZ][0] = 36;
392       out->width_ranges[TABLE_HORZ][1] = 72;
393       out->width_ranges[TABLE_VERT][0] = 36;
394       out->width_ranges[TABLE_VERT][1] = 120;
395     }
396
397   out->show_numeric_markers = flags & 0x04;
398   out->footnote_marker_superscripts = !in->tl->footnote_marker_subscripts;
399
400   for (int i = 0; i < 4; i++)
401     {
402       static const enum pivot_border map[4] =
403         {
404           PIVOT_BORDER_DIM_ROW_HORZ,
405           PIVOT_BORDER_DIM_ROW_VERT,
406           PIVOT_BORDER_CAT_ROW_HORZ,
407           PIVOT_BORDER_CAT_ROW_VERT,
408         };
409       tlo_decode_border (in->ss->sep1[i], &out->borders[map[i]]);
410     }
411
412   for (int i = 0; i < 4; i++)
413     {
414       static const enum pivot_border map[4] =
415         {
416           PIVOT_BORDER_DIM_COL_HORZ,
417           PIVOT_BORDER_DIM_COL_VERT,
418           PIVOT_BORDER_CAT_COL_HORZ,
419           PIVOT_BORDER_CAT_COL_VERT,
420         };
421       tlo_decode_border (in->ss->sep2[i], &out->borders[map[i]]);
422     }
423
424   if (in->v2_styles)
425     for (int i = 0; i < 11; i++)
426       {
427         static const enum pivot_border map[11] =
428           {
429             PIVOT_BORDER_TITLE,
430             PIVOT_BORDER_INNER_LEFT,
431             PIVOT_BORDER_INNER_RIGHT,
432             PIVOT_BORDER_INNER_TOP,
433             PIVOT_BORDER_INNER_BOTTOM,
434             PIVOT_BORDER_OUTER_LEFT,
435             PIVOT_BORDER_OUTER_RIGHT,
436             PIVOT_BORDER_OUTER_TOP,
437             PIVOT_BORDER_OUTER_BOTTOM,
438             PIVOT_BORDER_DATA_LEFT,
439             PIVOT_BORDER_DATA_TOP,
440           };
441         tlo_decode_border (in->v2_styles->sep3[i], &out->borders[map[i]]);
442       }
443   else
444     {
445       out->borders[PIVOT_BORDER_TITLE].stroke = TABLE_STROKE_NONE;
446       out->borders[PIVOT_BORDER_INNER_LEFT].stroke = TABLE_STROKE_SOLID;
447       out->borders[PIVOT_BORDER_INNER_TOP].stroke = TABLE_STROKE_SOLID;
448       out->borders[PIVOT_BORDER_INNER_RIGHT].stroke = TABLE_STROKE_SOLID;
449       out->borders[PIVOT_BORDER_INNER_BOTTOM].stroke = TABLE_STROKE_SOLID;
450       out->borders[PIVOT_BORDER_OUTER_LEFT].stroke = TABLE_STROKE_NONE;
451       out->borders[PIVOT_BORDER_OUTER_TOP].stroke = TABLE_STROKE_NONE;
452       out->borders[PIVOT_BORDER_OUTER_RIGHT].stroke = TABLE_STROKE_NONE;
453       out->borders[PIVOT_BORDER_OUTER_BOTTOM].stroke = TABLE_STROKE_NONE;
454       out->borders[PIVOT_BORDER_DATA_LEFT].stroke = TABLE_STROKE_NONE;
455       out->borders[PIVOT_BORDER_DATA_TOP].stroke = TABLE_STROKE_NONE;
456     }
457
458   tlo_decode_area (in->cs->title_color, in->ts->title_style,
459                    &out->areas[PIVOT_AREA_TITLE]);
460   for (int i = 0; i < 7; i++)
461     {
462       static const enum pivot_area map[7] = {
463         PIVOT_AREA_LAYERS,
464         PIVOT_AREA_CORNER,
465         PIVOT_AREA_ROW_LABELS,
466         PIVOT_AREA_COLUMN_LABELS,
467         PIVOT_AREA_DATA,
468         PIVOT_AREA_CAPTION,
469         PIVOT_AREA_FOOTER
470       };
471       tlo_decode_area (in->ts->most_areas[i]->color,
472                        in->ts->most_areas[i]->style,
473                        &out->areas[map[i]]);
474     }
475
476   out->print_all_layers = flags & 0x08;
477   out->paginate_layers = flags & 0x40;
478   out->shrink_to_fit[TABLE_HORZ] = flags & 0x10;
479   out->shrink_to_fit[TABLE_VERT] = flags & 0x20;
480   out->top_continuation = flags & 0x80;
481   out->bottom_continuation = flags & 0x100;
482   if (in->v2_styles)
483     {
484       free (out->continuation);
485       out->continuation = xmemdup0 (in->v2_styles->continuation,
486                                     in->v2_styles->continuation_len);
487     }
488   /* n_orphan_lines isn't in .tlo files AFAICT. */
489
490   return out;
491 }
492 \f
493 char * WARN_UNUSED_RESULT
494 spv_table_look_read (const char *filename, struct pivot_table_look **outp)
495 {
496   *outp = NULL;
497
498   size_t length;
499   char *file = read_file (filename, 0, &length);
500   if (!file)
501     return xasprintf ("%s: failed to read file (%s)",
502                       filename, strerror (errno));
503
504   if ((uint8_t) file[0] == 0xff)
505     {
506       struct spvbin_input input;
507       spvbin_input_init (&input, file, length);
508
509       struct tlo_table_look *look;
510       char *error = NULL;
511       if (!tlo_parse_table_look (&input, &look))
512         error = spvbin_input_to_error (&input, NULL);
513       else
514         {
515           *outp = tlo_decode (look);
516           tlo_free_table_look (look);
517         }
518       return error;
519     }
520   else
521     {
522       xmlDoc *doc = xmlReadMemory (file, length, NULL, NULL, XML_PARSE_NOBLANKS);
523       free (file);
524       if (!doc)
525         return xasprintf ("%s: failed to parse XML", filename);
526
527       struct spvxml_context ctx = SPVXML_CONTEXT_INIT (ctx);
528       struct spvsx_table_properties *tp;
529       spvsx_parse_table_properties (&ctx, xmlDocGetRootElement (doc), &tp);
530       char *error = spvxml_context_finish (&ctx, &tp->node_);
531
532       if (!error)
533         error = spv_table_look_decode (tp, outp);
534
535       spvsx_free_table_properties (tp);
536       xmlFreeDoc (doc);
537
538       return error;
539     }
540 }
541
542 static void
543 write_attr (xmlTextWriter *xml, const char *name, const char *value)
544 {
545   xmlTextWriterWriteAttribute (xml,
546                                CHAR_CAST (xmlChar *, name),
547                                CHAR_CAST (xmlChar *, value));
548 }
549
550 static void PRINTF_FORMAT (3, 4)
551 write_attr_format (xmlTextWriter *xml, const char *name,
552                    const char *format, ...)
553 {
554   va_list args;
555   va_start (args, format);
556   char *value = xvasprintf (format, args);
557   va_end (args);
558
559   write_attr (xml, name, value);
560   free (value);
561 }
562
563 static void
564 write_attr_color (xmlTextWriter *xml, const char *name,
565                   const struct cell_color *color)
566 {
567   write_attr_format (xml, name, "#%02"PRIx8"%02"PRIx8"%02"PRIx8,
568                      color->r, color->g, color->b);
569 }
570
571 static void
572 write_attr_dimension (xmlTextWriter *xml, const char *name, int px)
573 {
574   int pt = px / 96.0 * 72.0;
575   write_attr_format (xml, name, "%dpt", pt);
576 }
577
578 static void
579 write_attr_bool (xmlTextWriter *xml, const char *name, bool b)
580 {
581   write_attr (xml, name, b ? "true" : "false");
582 }
583
584 static void
585 start_elem (xmlTextWriter *xml, const char *name)
586 {
587   xmlTextWriterStartElement (xml, CHAR_CAST (xmlChar *, name));
588 }
589
590 static void
591 end_elem (xmlTextWriter *xml)
592 {
593   xmlTextWriterEndElement (xml);
594 }
595
596 char * WARN_UNUSED_RESULT
597 spv_table_look_write (const char *filename, const struct pivot_table_look *look)
598 {
599   FILE *file = fopen (filename, "w");
600   if (!file)
601     return xasprintf (_("%s: create failed (%s)"), filename, strerror (errno));
602
603   xmlTextWriter *xml = xmlNewTextWriter (xmlOutputBufferCreateFile (
604                                            file, NULL));
605   if (!xml)
606     {
607       fclose (file);
608       return xasprintf (_("%s: failed to start writing XML"), filename);
609     }
610
611   xmlTextWriterSetIndent (xml, 1);
612   xmlTextWriterSetIndentString (xml, CHAR_CAST (xmlChar *, "    "));
613
614   xmlTextWriterStartDocument (xml, NULL, "UTF-8", NULL);
615   start_elem (xml, "tableProperties");
616   if (look->name)
617     write_attr (xml, "name", look->name);
618   write_attr (xml, "xmlns", "http://www.ibm.com/software/analytics/spss/xml/table-looks");
619   write_attr (xml, "xmlns:vizml", "http://www.ibm.com/software/analytics/spss/xml/visualization");
620   write_attr (xml, "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
621   write_attr (xml, "xsi:schemaLocation", "http://www.ibm.com/software/analytics/spss/xml/table-looks http://www.ibm.com/software/analytics/spss/xml/table-looks/table-looks-1.4.xsd");
622
623   start_elem (xml, "generalProperties");
624   write_attr_bool (xml, "hideEmptyRows", look->omit_empty);
625   const int (*wr)[2] = look->width_ranges;
626   write_attr_format (xml, "maximumColumnWidth", "%d", wr[TABLE_HORZ][1]);
627   write_attr_format (xml, "maximumRowWidth", "%d", wr[TABLE_VERT][1]);
628   write_attr_format (xml, "minimumColumnWidth", "%d", wr[TABLE_HORZ][0]);
629   write_attr_format (xml, "minimumRowWidth", "%d", wr[TABLE_VERT][0]);
630   write_attr (xml, "rowDimensionLabels",
631               look->row_labels_in_corner ? "inCorner" : "nested");
632   end_elem (xml);
633
634   start_elem (xml, "footnoteProperties");
635   write_attr (xml, "markerPosition",
636               look->footnote_marker_superscripts ? "superscript" : "subscript");
637   write_attr (xml, "numberFormat",
638               look->show_numeric_markers ? "numeric" : "alphabetic");
639   end_elem (xml);
640
641   start_elem (xml, "cellFormatProperties");
642   for (enum pivot_area a = 0; a < PIVOT_N_AREAS; a++)
643     {
644       const struct table_area_style *area = &look->areas[a];
645       const struct font_style *font = &area->font_style;
646       const struct cell_style *cell = &area->cell_style;
647
648       start_elem (xml, pivot_area_names[a]);
649       if (a == PIVOT_AREA_DATA
650           && (!cell_color_equal (&font->fg[0], &font->fg[1])
651               || !cell_color_equal (&font->bg[0], &font->bg[1])))
652         {
653           write_attr_color (xml, "alternatingColor", &font->bg[1]);
654           write_attr_color (xml, "alternatingTextColor", &font->fg[1]);
655         }
656
657       start_elem (xml, "vizml:style");
658       write_attr_color (xml, "color", &font->fg[0]);
659       write_attr_color (xml, "color2", &font->bg[0]);
660       write_attr (xml, "font-family", font->typeface);
661       write_attr_format (xml, "font-size", "%dpt", font->size);
662       write_attr (xml, "font-weight", font->bold ? "bold" : "regular");
663       write_attr (xml, "font-underline",
664                   font->underline ? "underline" : "none");
665       write_attr (xml, "labelLocationVertical",
666                   cell->valign == TABLE_VALIGN_BOTTOM ? "negative"
667                   : cell->valign == TABLE_VALIGN_TOP ? "positive"
668                   : "center");
669       write_attr_dimension (xml, "margin-bottom", cell->margin[TABLE_VERT][1]);
670       write_attr_dimension (xml, "margin-left", cell->margin[TABLE_HORZ][0]);
671       write_attr_dimension (xml, "margin-right", cell->margin[TABLE_HORZ][1]);
672       write_attr_dimension (xml, "margin-top", cell->margin[TABLE_VERT][0]);
673       write_attr (xml, "textAlignment",
674                   cell->halign == TABLE_HALIGN_LEFT ? "left"
675                   : cell->halign == TABLE_HALIGN_RIGHT ? "right"
676                   : cell->halign == TABLE_HALIGN_CENTER ? "center"
677                   : cell->halign == TABLE_HALIGN_DECIMAL ? "decimal"
678                   : "mixed");
679       if (cell->halign == TABLE_HALIGN_DECIMAL)
680         write_attr_dimension (xml, "decimal-offset", cell->decimal_offset);
681       end_elem (xml);
682
683       end_elem (xml);
684     }
685   end_elem (xml);
686
687   start_elem (xml, "borderProperties");
688   for (enum pivot_border b = 0; b < PIVOT_N_BORDERS; b++)
689     {
690       const struct table_border_style *border = &look->borders[b];
691
692       start_elem (xml, pivot_border_names[b]);
693
694       static const char *table_stroke_names[TABLE_N_STROKES] =
695         {
696           [TABLE_STROKE_NONE] = "none",
697           [TABLE_STROKE_SOLID] = "solid",
698           [TABLE_STROKE_DASHED] = "dashed",
699           [TABLE_STROKE_THICK] = "thick",
700           [TABLE_STROKE_THIN] = "thin",
701           [TABLE_STROKE_DOUBLE] = "double",
702         };
703       write_attr (xml, "borderStyleType", table_stroke_names[border->stroke]);
704       write_attr_color (xml, "color", &border->color);
705       end_elem (xml);
706     }
707   end_elem (xml);
708
709   start_elem (xml, "printingProperties");
710   write_attr_bool (xml, "printAllLayers", look->print_all_layers);
711   write_attr_bool (xml, "rescaleLongTableToFitPage",
712                    look->shrink_to_fit[TABLE_HORZ]);
713   write_attr_bool (xml, "rescaleWideTableToFitPage",
714                    look->shrink_to_fit[TABLE_VERT]);
715   write_attr_format (xml, "windowOrphanLines", "%zu", look->n_orphan_lines);
716   if (look->continuation && look->continuation[0]
717       && (look->top_continuation || look->bottom_continuation))
718     {
719       write_attr (xml, "continuationText", look->continuation);
720       write_attr_bool (xml, "continuationTextAtTop", look->top_continuation);
721       write_attr_bool (xml, "continuationTextAtBottom",
722                        look->bottom_continuation);
723     }
724   end_elem (xml);
725
726   xmlTextWriterEndDocument (xml);
727
728   xmlFreeTextWriter (xml);
729
730   fflush (file);
731   bool ok = !ferror (file);
732   if (fclose (file) == EOF)
733     ok = false;
734
735   if (!ok)
736     return xasprintf (_("%s: error writing file (%s)"),
737                       filename, strerror (errno));
738
739   return NULL;
740 }