1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2019 Free Software Foundation, Inc.
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.
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.
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/>. */
19 #include "output/spv/spv-writer.h"
23 #include <libxml/xmlwriter.h>
28 #include "libpspp/array.h"
29 #include "libpspp/assertion.h"
30 #include "libpspp/cast.h"
31 #include "libpspp/float-format.h"
32 #include "libpspp/integer-format.h"
33 #include "libpspp/temp-file.h"
34 #include "libpspp/version.h"
35 #include "libpspp/zip-writer.h"
36 #include "output/cairo-chart.h"
37 #include "output/driver.h"
38 #include "output/output-item.h"
39 #include "output/page-setup.h"
40 #include "output/pivot-table.h"
42 #include "gl/xalloc.h"
43 #include "gl/xvasprintf.h"
46 #define _(msgid) gettext (msgid)
47 #define N_(msgid) (msgid)
51 struct zip_writer *zw;
60 struct page_setup *page_setup;
64 static void spv_writer_close_heading (struct spv_writer *);
66 char * WARN_UNUSED_RESULT
67 spv_writer_open (const char *filename, struct spv_writer **writerp)
71 struct zip_writer *zw = zip_writer_create (filename);
73 return xasprintf (_("%s: create failed"), filename);
75 struct spv_writer *w = xmalloc (sizeof *w);
76 *w = (struct spv_writer) { .zw = zw };
81 char * WARN_UNUSED_RESULT
82 spv_writer_close (struct spv_writer *w)
87 zip_writer_add_string (w->zw, "META-INF/MANIFEST.MF", "allowPivoting=true");
89 while (w->heading_depth)
90 spv_writer_close_heading (w);
93 if (!zip_writer_close (w->zw))
94 error = xstrdup (_("I/O error writing SPV file"));
96 page_setup_destroy (w->page_setup);
102 write_attr (struct spv_writer *w, const char *name, const char *value)
104 xmlTextWriterWriteAttribute (w->xml,
105 CHAR_CAST (xmlChar *, name),
106 CHAR_CAST (xmlChar *, value));
109 static void PRINTF_FORMAT (3, 4)
110 write_attr_format (struct spv_writer *w, const char *name,
111 const char *format, ...)
114 va_start (args, format);
115 char *value = xvasprintf (format, args);
118 write_attr (w, name, value);
123 start_elem (struct spv_writer *w, const char *name)
125 xmlTextWriterStartElement (w->xml, CHAR_CAST (xmlChar *, name));
129 end_elem (struct spv_writer *w)
131 xmlTextWriterEndElement (w->xml);
135 write_text (struct spv_writer *w, const char *text)
137 xmlTextWriterWriteString (w->xml, CHAR_CAST (xmlChar *, text));
141 write_page_heading (struct spv_writer *w, const struct page_heading *h,
144 start_elem (w, name);
147 start_elem (w, "pageParagraph");
148 for (size_t i = 0; i < h->n; i++)
150 start_elem (w, "text");
151 write_attr (w, "type", "title");
152 write_text (w, h->paragraphs[i].markup); /* XXX */
161 write_page_setup (struct spv_writer *w, const struct page_setup *ps)
163 start_elem (w, "pageSetup");
164 write_attr_format (w, "initial-page-number", "%d", ps->initial_page_number);
165 write_attr (w, "chart-size",
166 (ps->chart_size == PAGE_CHART_AS_IS ? "as-is"
167 : ps->chart_size == PAGE_CHART_FULL_HEIGHT ? "full-height"
168 : ps->chart_size == PAGE_CHART_HALF_HEIGHT ? "half-height"
169 : "quarter-height"));
170 write_attr_format (w, "margin-left", "%.2fin", ps->margins[TABLE_HORZ][0]);
171 write_attr_format (w, "margin-right", "%.2fin", ps->margins[TABLE_HORZ][1]);
172 write_attr_format (w, "margin-top", "%.2fin", ps->margins[TABLE_VERT][0]);
173 write_attr_format (w, "margin-bottom", "%.2fin", ps->margins[TABLE_VERT][1]);
174 write_attr_format (w, "paper-height", "%.2fin", ps->paper[TABLE_VERT]);
175 write_attr_format (w, "paper-width", "%.2fin", ps->paper[TABLE_HORZ]);
176 write_attr (w, "reference-orientation",
177 ps->orientation == PAGE_PORTRAIT ? "portrait" : "landscape");
178 write_attr_format (w, "space-after", "%.1fpt", ps->object_spacing * 72.0);
179 write_page_heading (w, &ps->headings[0], "pageHeader");
180 write_page_heading (w, &ps->headings[1], "pageFooter");
185 spv_writer_open_file (struct spv_writer *w)
187 w->heading = create_temp_file ();
191 w->xml = xmlNewTextWriter (xmlOutputBufferCreateFile (w->heading, NULL));
192 xmlTextWriterStartDocument (w->xml, NULL, "UTF-8", NULL);
193 start_elem (w, "heading");
195 time_t t = time (NULL);
196 struct tm *tm = gmtime (&t);
198 if (strftime (tm_s, sizeof tm_s, "%x %X", tm))
199 write_attr (w, "creation-date-time", tm_s);
201 write_attr (w, "creator", version);
203 write_attr (w, "creator-version", "21");
205 write_attr (w, "xmlns", "http://xml.spss.com/spss/viewer/viewer-tree");
206 write_attr (w, "xmlns:vps", "http://xml.spss.com/spss/viewer/viewer-pagesetup");
207 write_attr (w, "xmlns:vtx", "http://xml.spss.com/spss/viewer/viewer-text");
208 write_attr (w, "xmlns:vtb", "http://xml.spss.com/spss/viewer/viewer-table");
210 start_elem (w, "label");
211 write_text (w, _("Output"));
216 write_page_setup (w, w->page_setup);
218 page_setup_destroy (w->page_setup);
219 w->page_setup = NULL;
226 spv_writer_open_heading (struct spv_writer *w, const struct output_item *item)
230 if (!spv_writer_open_file (w))
235 start_elem (w, "heading");
236 if (item->command_name)
237 write_attr (w, "commandName", item->command_name);
239 write_attr (w, "visibility", "collapsed");
243 start_elem (w, "label");
244 write_text (w, output_item_get_label (item));
249 spv_writer_close_file (struct spv_writer *w, const char *infix)
255 xmlTextWriterEndDocument (w->xml);
256 xmlFreeTextWriter (w->xml);
258 char *member_name = xasprintf ("outputViewer%010d%s.xml",
259 w->n_headings++, infix);
260 zip_writer_add (w->zw, w->heading, member_name);
267 spv_writer_close_heading (struct spv_writer *w)
269 const char *infix = "";
270 if (w->heading_depth)
277 if (!w->heading_depth)
278 spv_writer_close_file (w, infix);
282 open_container (struct spv_writer *w, const struct output_item *item,
283 const char *inner_elem)
285 start_elem (w, "container");
286 write_attr (w, "visibility", item->show ? "visible" : "hidden");
287 if (w->need_page_break)
289 write_attr (w, "page-break-before", "always");
290 w->need_page_break = false;
293 start_elem (w, "label");
294 write_text (w, output_item_get_label (item));
297 start_elem (w, inner_elem);
298 if (item->command_name)
299 write_attr (w, "commandName", item->command_name);
303 close_container (struct spv_writer *w)
310 spv_writer_put_text (struct spv_writer *w, struct output_item *item)
312 bool initial_depth = w->heading_depth;
314 spv_writer_open_file (w);
316 open_container (w, item, "vtx:text");
317 write_attr (w, "type",
318 (item->text.subtype == TEXT_ITEM_TITLE ? "title"
319 : item->text.subtype == TEXT_ITEM_PAGE_TITLE ? "page-title"
322 start_elem (w, "html");
323 char *s = text_item_get_plain_text (item);
331 spv_writer_close_file (w, "");
333 output_item_unref (item);
336 static cairo_status_t
337 write_to_zip (void *zw_, const unsigned char *data, unsigned int length)
339 struct zip_writer *zw = zw_;
341 zip_writer_add_write (zw, data, length);
342 return CAIRO_STATUS_SUCCESS;
346 spv_writer_put_image (struct spv_writer *w, const struct output_item *item,
347 cairo_surface_t *image)
349 bool initial_depth = w->heading_depth;
351 spv_writer_open_file (w);
353 char *uri = xasprintf ("%010d_Imagegeneric.png", ++w->n_tables);
355 open_container (w, item, "object");
356 write_attr (w, "type", "unknown");
357 write_attr (w, "uri", uri);
361 spv_writer_close_file (w, "");
363 zip_writer_add_start (w->zw, uri);
364 cairo_surface_write_to_png_stream (image, write_to_zip, w->zw);
365 zip_writer_add_finish (w->zw);
381 put_uninit (struct buf *b, size_t n)
383 while (b->allocated - b->len < n)
384 b->data = x2nrealloc (b->data, &b->allocated, sizeof b->data);
385 uint8_t *p = &b->data[b->len];
391 put_byte (struct buf *b, uint8_t byte)
393 *put_uninit (b, 1) = byte;
397 put_bool (struct buf *b, bool boolean)
399 put_byte (b, boolean);
403 put_bytes (struct buf *b, const char *bytes, size_t n)
405 memcpy (put_uninit (b, n), bytes, n);
409 put_u16 (struct buf *b, uint16_t x)
411 put_uint16 (native_to_le16 (x), put_uninit (b, sizeof x));
415 put_u32 (struct buf *b, uint32_t x)
417 put_uint32 (native_to_le32 (x), put_uninit (b, sizeof x));
421 put_u64 (struct buf *b, uint64_t x)
423 put_uint64 (native_to_le64 (x), put_uninit (b, sizeof x));
427 put_be32 (struct buf *b, uint32_t x)
429 put_uint32 (native_to_be32 (x), put_uninit (b, sizeof x));
433 put_double (struct buf *b, double x)
435 float_convert (FLOAT_NATIVE_DOUBLE, &x,
436 FLOAT_IEEE_DOUBLE_LE, put_uninit (b, 8));
440 put_float (struct buf *b, float x)
442 float_convert (FLOAT_NATIVE_FLOAT, &x,
443 FLOAT_IEEE_SINGLE_LE, put_uninit (b, 4));
447 put_string (struct buf *b, const char *s_)
449 const char *s = s_ ? s_ : "";
450 size_t len = strlen (s);
452 memcpy (put_uninit (b, len), s, len);
456 put_bestring (struct buf *b, const char *s_)
458 const char *s = s_ ? s_ : "";
459 size_t len = strlen (s);
461 memcpy (put_uninit (b, len), s, len);
465 start_count (struct buf *b)
472 end_count_u32 (struct buf *b, size_t start)
474 put_uint32 (native_to_le32 (b->len - start), &b->data[start - 4]);
478 end_count_be32 (struct buf *b, size_t start)
480 put_uint32 (native_to_be32 (b->len - start), &b->data[start - 4]);
484 put_color (struct buf *buf, const struct cell_color *color)
486 char *s = xasprintf ("#%02"PRIx8"%02"PRIx8"%02"PRIx8,
487 color->r, color->g, color->b);
493 put_font_style (struct buf *buf, const struct font_style *font_style)
495 put_bool (buf, font_style->bold);
496 put_bool (buf, font_style->italic);
497 put_bool (buf, font_style->underline);
499 put_color (buf, &font_style->fg[0]);
500 put_color (buf, &font_style->bg[0]);
501 put_string (buf, font_style->typeface ? font_style->typeface : "SansSerif");
502 put_byte (buf, ceil (font_style->size * 1.33));
506 put_halign (struct buf *buf, enum table_halign halign,
507 uint32_t mixed, uint32_t decimal)
509 put_u32 (buf, (halign == TABLE_HALIGN_RIGHT ? 4
510 : halign == TABLE_HALIGN_LEFT ? 2
511 : halign == TABLE_HALIGN_CENTER ? 0
512 : halign == TABLE_HALIGN_MIXED ? mixed
517 put_valign (struct buf *buf, enum table_valign valign)
519 put_u32 (buf, (valign == TABLE_VALIGN_TOP ? 1
520 : valign == TABLE_VALIGN_CENTER ? 0
525 put_cell_style (struct buf *buf, const struct cell_style *cell_style)
527 put_halign (buf, cell_style->halign, 0xffffffad, 6);
528 put_valign (buf, cell_style->valign);
529 put_double (buf, cell_style->decimal_offset);
530 put_u16 (buf, cell_style->margin[H][0]);
531 put_u16 (buf, cell_style->margin[H][1]);
532 put_u16 (buf, cell_style->margin[V][0]);
533 put_u16 (buf, cell_style->margin[V][1]);
537 put_style_pair (struct buf *buf, const struct font_style *font_style,
538 const struct cell_style *cell_style)
542 put_byte (buf, 0x31);
543 put_font_style (buf, font_style);
546 put_byte (buf, 0x58);
550 put_byte (buf, 0x31);
551 put_cell_style (buf, cell_style);
554 put_byte (buf, 0x58);
558 put_value_mod (struct buf *buf, const struct pivot_value *value,
559 const char *template)
561 if (value->n_footnotes || value->n_subscripts
562 || template || value->font_style || value->cell_style)
564 put_byte (buf, 0x31);
567 put_u32 (buf, value->n_footnotes);
568 for (size_t i = 0; i < value->n_footnotes; i++)
569 put_u16 (buf, value->footnote_indexes[i]);
572 put_u32 (buf, value->n_subscripts);
573 for (size_t i = 0; i < value->n_subscripts; i++)
574 put_string (buf, value->subscripts[i]);
576 /* Template and style. */
577 uint32_t v3_start = start_count (buf);
578 uint32_t template_string_start = start_count (buf);
581 uint32_t inner_start = start_count (buf);
582 end_count_u32 (buf, inner_start);
584 put_byte (buf, 0x31);
585 put_string (buf, template);
587 end_count_u32 (buf, template_string_start);
588 put_style_pair (buf, value->font_style, value->cell_style);
589 end_count_u32 (buf, v3_start);
592 put_byte (buf, 0x58);
596 put_format (struct buf *buf, const struct fmt_spec *f, bool honor_small)
598 int type = f->type == FMT_F && honor_small ? 40 : fmt_to_io (f->type);
599 put_u32 (buf, (type << 16) | (f->w << 8) | f->d);
603 show_values_to_spvlb (enum settings_value_show show)
605 return (show == SETTINGS_VALUE_SHOW_DEFAULT ? 0
606 : show == SETTINGS_VALUE_SHOW_VALUE ? 1
607 : show == SETTINGS_VALUE_SHOW_LABEL ? 2
612 put_show_values (struct buf *buf, enum settings_value_show show)
614 put_byte (buf, show_values_to_spvlb (show));
618 put_value (struct buf *buf, const struct pivot_value *value)
622 case PIVOT_VALUE_NUMERIC:
623 if (value->numeric.var_name || value->numeric.value_label)
626 put_value_mod (buf, value, NULL);
627 put_format (buf, &value->numeric.format, value->numeric.honor_small);
628 put_double (buf, value->numeric.x);
629 put_string (buf, value->numeric.var_name);
630 put_string (buf, value->numeric.value_label);
631 put_show_values (buf, value->numeric.show);
636 put_value_mod (buf, value, NULL);
637 put_format (buf, &value->numeric.format, value->numeric.honor_small);
638 put_double (buf, value->numeric.x);
642 case PIVOT_VALUE_STRING:
644 put_value_mod (buf, value, NULL);
645 size_t len = strlen (value->string.s);
646 if (value->string.hex)
647 put_format (buf, &(struct fmt_spec) { FMT_AHEX, len * 2, 0 }, false);
649 put_format (buf, &(struct fmt_spec) { FMT_A, len, 0 }, false);
650 put_string (buf, value->string.value_label);
651 put_string (buf, value->string.var_name);
652 put_show_values (buf, value->string.show);
653 put_string (buf, value->string.s);
656 case PIVOT_VALUE_VARIABLE:
658 put_value_mod (buf, value, NULL);
659 put_string (buf, value->variable.var_name);
660 put_string (buf, value->variable.var_label);
661 put_show_values (buf, value->variable.show);
664 case PIVOT_VALUE_TEXT:
666 put_string (buf, value->text.local);
667 put_value_mod (buf, value, NULL);
668 put_string (buf, value->text.id);
669 put_string (buf, value->text.c);
670 put_byte (buf, 1); /* XXX user-provided */
673 case PIVOT_VALUE_TEMPLATE:
675 put_value_mod (buf, value, value->template.id);
676 put_string (buf, value->template.local);
677 put_u32 (buf, value->template.n_args);
678 for (size_t i = 0; i < value->template.n_args; i++)
680 const struct pivot_argument *arg = &value->template.args[i];
681 assert (arg->n >= 1);
684 put_u32 (buf, arg->n);
686 for (size_t j = 0; j < arg->n; j++)
689 put_bytes (buf, "\0\0\0\0", 4);
690 put_value (buf, arg->values[j]);
696 put_value (buf, arg->values[0]);
707 put_optional_value (struct buf *buf, const struct pivot_value *value)
711 put_byte (buf, 0x31);
712 put_value (buf, value);
715 put_byte (buf, 0x58);
719 put_category (struct buf *buf, const struct pivot_category *c)
721 put_value (buf, c->name);
722 if (pivot_category_is_leaf (c))
724 put_bytes (buf, "\0\0\0", 3);
726 put_u32 (buf, c->data_index);
731 put_bytes (buf, "\0\0\1", 3);
732 put_u32 (buf, 0); /* x23 */
734 put_u32 (buf, c->n_subs);
735 for (size_t i = 0; i < c->n_subs; i++)
736 put_category (buf, c->subs[i]);
741 put_y0 (struct buf *buf, const struct pivot_table *table)
743 put_u32 (buf, table->settings.epoch);
744 put_byte (buf, table->settings.decimal);
749 put_custom_currency (struct buf *buf, const struct pivot_table *table)
752 for (int i = 0; i < 5; i++)
754 enum fmt_type types[5] = { FMT_CCA, FMT_CCB, FMT_CCC, FMT_CCD, FMT_CCE };
755 char *cc = fmt_number_style_to_string (fmt_settings_get_style (
756 &table->settings, types[i]));
757 put_string (buf, cc);
763 put_x1 (struct buf *buf, const struct pivot_table *table)
765 put_byte (buf, 0); /* x14 */
766 put_byte (buf, table->show_title ? 1 : 10);
767 put_byte (buf, 0); /* x16 */
768 put_byte (buf, 0); /* lang */
769 put_show_values (buf, table->show_variables);
770 put_show_values (buf, table->show_values);
771 put_u32 (buf, -1); /* x18 */
772 put_u32 (buf, -1); /* x19 */
773 for (int i = 0; i < 17; i++)
775 put_bool (buf, false); /* x20 */
776 put_byte (buf, table->show_caption);
780 put_x2 (struct buf *buf)
782 put_u32 (buf, 0); /* n-row-heights */
783 put_u32 (buf, 0); /* n-style-map */
784 put_u32 (buf, 0); /* n-styles */
789 put_y1 (struct buf *buf, const struct pivot_table *table)
791 put_string (buf, table->command_c);
792 put_string (buf, table->command_local);
793 put_string (buf, table->language);
794 put_string (buf, "UTF-8"); /* XXX */
795 put_string (buf, table->locale);
796 put_bytes (buf, "\0\0\1\1", 4);
801 put_y2 (struct buf *buf, const struct pivot_table *table)
803 put_custom_currency (buf, table);
809 put_x3 (struct buf *buf, const struct pivot_table *table)
813 put_byte (buf, 4); /* x21 */
818 put_double (buf, table->small);
820 put_string (buf, table->dataset);
821 put_string (buf, table->datafile);
823 put_u32 (buf, table->date);
829 encode_current_layer (const struct pivot_table *table)
831 uint32_t current_layer = 0;
833 const struct pivot_axis *axis = &table->axes[PIVOT_AXIS_LAYER];
834 for (size_t i = axis->n_dimensions - 1; i < axis->n_dimensions; i--)
836 const struct pivot_dimension *d = axis->dimensions[i];
837 current_layer = current_layer * d->n_leaves + table->current_layer[i];
840 return current_layer;
844 put_light_table (struct buf *buf, uint64_t table_id,
845 const struct pivot_table *table)
848 put_bytes (buf, "\1\0", 2);
850 put_bool (buf, true);
851 put_bool (buf, false);
852 put_bool (buf, table->rotate_inner_column_labels);
853 put_bool (buf, table->rotate_outer_row_labels);
854 put_bool (buf, true);
856 put_u32 (buf, table->look->width_ranges[H][0]);
857 put_u32 (buf, table->look->width_ranges[H][1]);
858 put_u32 (buf, table->look->width_ranges[V][0]);
859 put_u32 (buf, table->look->width_ranges[V][1]);
860 put_u64 (buf, table_id);
863 put_value (buf, table->title);
864 put_value (buf, table->subtype);
865 put_optional_value (buf, table->title);
866 put_optional_value (buf, table->corner_text);
867 put_optional_value (buf, table->caption);
870 put_u32 (buf, table->n_footnotes);
871 for (size_t i = 0; i < table->n_footnotes; i++)
873 const struct pivot_footnote *f = table->footnotes[i];
874 put_value (buf, f->content);
875 put_optional_value (buf, f->marker);
876 put_u32 (buf, f->show ? 1 : -1);
880 for (size_t i = 0; i < PIVOT_N_AREAS; i++)
882 const struct table_area_style *a = &table->look->areas[i];
883 put_byte (buf, i + 1);
884 put_byte (buf, 0x31);
885 put_string (buf, (a->font_style.typeface
886 ? a->font_style.typeface
888 put_float (buf, ceil (a->font_style.size * 1.33));
889 put_u32 (buf, ((a->font_style.bold ? 1 : 0)
890 | (a->font_style.italic ? 2 : 0)));
891 put_bool (buf, a->font_style.underline);
892 put_halign (buf, a->cell_style.halign, 64173, 61453);
893 put_valign (buf, a->cell_style.valign);
895 put_color (buf, &a->font_style.fg[0]);
896 put_color (buf, &a->font_style.bg[0]);
899 = (!cell_color_equal (&a->font_style.fg[0], &a->font_style.fg[1])
900 || !cell_color_equal (&a->font_style.bg[0], &a->font_style.bg[1]));
904 put_color (buf, &a->font_style.fg[1]);
905 put_color (buf, &a->font_style.bg[1]);
909 put_string (buf, "");
910 put_string (buf, "");
913 put_u32 (buf, a->cell_style.margin[H][0]);
914 put_u32 (buf, a->cell_style.margin[H][1]);
915 put_u32 (buf, a->cell_style.margin[V][0]);
916 put_u32 (buf, a->cell_style.margin[V][1]);
920 uint32_t borders_start = start_count (buf);
922 put_be32 (buf, PIVOT_N_BORDERS);
923 for (size_t i = 0; i < PIVOT_N_BORDERS; i++)
925 const struct table_border_style *b = &table->look->borders[i];
927 put_be32 (buf, (b->stroke == TABLE_STROKE_NONE ? 0
928 : b->stroke == TABLE_STROKE_SOLID ? 1
929 : b->stroke == TABLE_STROKE_DASHED ? 2
930 : b->stroke == TABLE_STROKE_THICK ? 3
931 : b->stroke == TABLE_STROKE_THIN ? 4
933 put_be32 (buf, ((b->color.alpha << 24)
938 put_bool (buf, table->show_grid_lines);
939 put_bytes (buf, "\0\0\0", 3);
940 end_count_u32 (buf, borders_start);
942 /* Print Settings. */
943 uint32_t ps_start = start_count (buf);
945 put_bool (buf, table->look->print_all_layers);
946 put_bool (buf, table->look->paginate_layers);
947 put_bool (buf, table->look->shrink_to_fit[H]);
948 put_bool (buf, table->look->shrink_to_fit[V]);
949 put_bool (buf, table->look->top_continuation);
950 put_bool (buf, table->look->bottom_continuation);
951 put_be32 (buf, table->look->n_orphan_lines);
952 put_bestring (buf, table->look->continuation);
953 end_count_u32 (buf, ps_start);
955 /* Table Settings. */
956 uint32_t ts_start = start_count (buf);
959 put_be32 (buf, encode_current_layer (table));
960 put_bool (buf, table->look->omit_empty);
961 put_bool (buf, table->look->row_labels_in_corner);
962 put_bool (buf, !table->look->show_numeric_markers);
963 put_bool (buf, table->look->footnote_marker_superscripts);
965 uint32_t keep_start = start_count (buf);
966 put_be32 (buf, 0); /* n-row-breaks */
967 put_be32 (buf, 0); /* n-column-breaks */
968 put_be32 (buf, 0); /* n-row-keeps */
969 put_be32 (buf, 0); /* n-column-keeps */
970 put_be32 (buf, 0); /* n-row-point-keeps */
971 put_be32 (buf, 0); /* n-column-point-keeps */
972 end_count_be32 (buf, keep_start);
973 put_bestring (buf, table->notes);
974 put_bestring (buf, table->look->name);
975 for (size_t i = 0; i < 82; i++)
977 end_count_u32 (buf, ts_start);
980 put_u32 (buf, 0); /* n-widths */
981 put_string (buf, "en_US.ISO_8859-1:1987"); /* XXX */
982 put_u32 (buf, 0); /* XXX current-layer */
987 put_custom_currency (buf, table);
988 uint32_t formats_start = start_count (buf);
989 uint32_t x1_start = start_count (buf);
991 uint32_t x2_start = start_count (buf);
993 end_count_u32 (buf, x2_start);
994 end_count_u32 (buf, x1_start);
995 uint32_t x3_start = start_count (buf);
997 end_count_u32 (buf, x3_start);
998 end_count_u32 (buf, formats_start);
1001 put_u32 (buf, table->n_dimensions);
1002 int *x2 = xnmalloc (table->n_dimensions, sizeof *x2);
1003 for (size_t i = 0; i < table->axes[PIVOT_AXIS_LAYER].n_dimensions; i++)
1005 for (size_t i = 0; i < table->axes[PIVOT_AXIS_ROW].n_dimensions; i++)
1006 x2[i + table->axes[PIVOT_AXIS_LAYER].n_dimensions] = 0;
1007 for (size_t i = 0; i < table->axes[PIVOT_AXIS_COLUMN].n_dimensions; i++)
1009 + table->axes[PIVOT_AXIS_LAYER].n_dimensions
1010 + table->axes[PIVOT_AXIS_ROW].n_dimensions] = 1;
1011 for (size_t i = 0; i < table->n_dimensions; i++)
1013 const struct pivot_dimension *d = table->dimensions[i];
1014 put_value (buf, d->root->name);
1015 put_byte (buf, 0); /* x1 */
1016 put_byte (buf, x2[i]);
1017 put_u32 (buf, 2); /* x3 */
1018 put_bool (buf, !d->root->show_label);
1019 put_bool (buf, d->hide_all_labels);
1023 put_u32 (buf, d->root->n_subs);
1024 for (size_t j = 0; j < d->root->n_subs; j++)
1025 put_category (buf, d->root->subs[j]);
1030 put_u32 (buf, table->axes[PIVOT_AXIS_LAYER].n_dimensions);
1031 put_u32 (buf, table->axes[PIVOT_AXIS_ROW].n_dimensions);
1032 put_u32 (buf, table->axes[PIVOT_AXIS_COLUMN].n_dimensions);
1033 for (size_t i = 0; i < table->axes[PIVOT_AXIS_LAYER].n_dimensions; i++)
1034 put_u32 (buf, table->axes[PIVOT_AXIS_LAYER].dimensions[i]->top_index);
1035 for (size_t i = 0; i < table->axes[PIVOT_AXIS_ROW].n_dimensions; i++)
1036 put_u32 (buf, table->axes[PIVOT_AXIS_ROW].dimensions[i]->top_index);
1037 for (size_t i = 0; i < table->axes[PIVOT_AXIS_COLUMN].n_dimensions; i++)
1038 put_u32 (buf, table->axes[PIVOT_AXIS_COLUMN].dimensions[i]->top_index);
1041 put_u32 (buf, hmap_count (&table->cells));
1042 const struct pivot_cell *cell;
1043 HMAP_FOR_EACH (cell, struct pivot_cell, hmap_node, &table->cells)
1046 for (size_t j = 0; j < table->n_dimensions; j++)
1047 index = (table->dimensions[j]->n_leaves * index) + cell->idx[j];
1048 put_u64 (buf, index);
1050 put_value (buf, cell->value);
1055 spv_writer_put_table (struct spv_writer *w, const struct output_item *item)
1057 int table_id = ++w->n_tables;
1059 bool initial_depth = w->heading_depth;
1061 spv_writer_open_file (w);
1063 open_container (w, item, "vtb:table");
1065 write_attr (w, "type", "table"); /* XXX */
1066 write_attr_format (w, "tableId", "%d", table_id);
1067 char *subtype = (item->table->subtype
1068 ? pivot_value_to_string (item->table->subtype, item->table)
1069 : xstrdup ("unknown"));
1070 write_attr (w, "subType", subtype);
1073 start_elem (w, "vtb:tableStructure");
1074 start_elem (w, "vtb:dataPath");
1075 char *data_path = xasprintf ("%010d_lightTableData.bin", table_id);
1076 write_text (w, data_path);
1077 end_elem (w); /* vtb:dataPath */
1078 end_elem (w); /* vtb:tableStructure */
1080 close_container (w);
1083 spv_writer_close_file (w, "");
1085 struct buf buf = { NULL, 0, 0 };
1086 put_light_table (&buf, table_id, item->table);
1087 zip_writer_add_memory (w->zw, data_path, buf.data, buf.len);
1094 spv_writer_write (struct spv_writer *w, const struct output_item *item)
1098 case OUTPUT_ITEM_CHART:
1100 cairo_surface_t *surface = xr_draw_image_chart (
1102 &(struct cell_color) CELL_COLOR_BLACK,
1103 &(struct cell_color) CELL_COLOR_WHITE);
1104 if (cairo_surface_status (surface) == CAIRO_STATUS_SUCCESS)
1105 spv_writer_put_image (w, item, surface);
1106 cairo_surface_destroy (surface);
1110 case OUTPUT_ITEM_GROUP:
1111 spv_writer_open_heading (w, item);
1112 for (size_t i = 0; i < item->group.n_children; i++)
1113 spv_writer_write (w, item->group.children[i]);
1114 spv_writer_close_heading (w);
1117 case OUTPUT_ITEM_IMAGE:
1118 spv_writer_put_image (w, item, item->image);
1121 case OUTPUT_ITEM_MESSAGE:
1122 spv_writer_put_text (
1123 w, message_item_to_text_item (output_item_ref (item)));
1126 case OUTPUT_ITEM_PAGE_BREAK:
1127 w->need_page_break = true;
1130 case OUTPUT_ITEM_TABLE:
1131 spv_writer_put_table (w, item);
1134 case OUTPUT_ITEM_TEXT:
1135 spv_writer_put_text (w, output_item_ref (item));
1141 spv_writer_set_page_setup (struct spv_writer *w,
1142 const struct page_setup *ps)
1144 page_setup_destroy (w->page_setup);
1145 w->page_setup = page_setup_clone (ps);