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/chart-item.h"
38 #include "output/driver.h"
39 #include "output/group-item.h"
40 #include "output/image-item.h"
41 #include "output/page-eject-item.h"
42 #include "output/page-setup-item.h"
43 #include "output/pivot-table.h"
44 #include "output/table-item.h"
45 #include "output/text-item.h"
47 #include "gl/xalloc.h"
48 #include "gl/xvasprintf.h"
51 #define _(msgid) gettext (msgid)
52 #define N_(msgid) (msgid)
56 struct zip_writer *zw;
65 struct page_setup *page_setup;
69 static void spv_writer_close_heading (struct spv_writer *);
71 char * WARN_UNUSED_RESULT
72 spv_writer_open (const char *filename, struct spv_writer **writerp)
76 struct zip_writer *zw = zip_writer_create (filename);
78 return xasprintf (_("%s: create failed"), filename);
80 struct spv_writer *w = xmalloc (sizeof *w);
81 *w = (struct spv_writer) { .zw = zw };
86 char * WARN_UNUSED_RESULT
87 spv_writer_close (struct spv_writer *w)
92 zip_writer_add_string (w->zw, "META-INF/MANIFEST.MF", "allowPivoting=true");
94 while (w->heading_depth)
95 spv_writer_close_heading (w);
98 if (!zip_writer_close (w->zw))
99 error = xstrdup (_("I/O error writing SPV file"));
101 page_setup_destroy (w->page_setup);
107 write_attr (struct spv_writer *w, const char *name, const char *value)
109 xmlTextWriterWriteAttribute (w->xml,
110 CHAR_CAST (xmlChar *, name),
111 CHAR_CAST (xmlChar *, value));
114 static void PRINTF_FORMAT (3, 4)
115 write_attr_format (struct spv_writer *w, const char *name,
116 const char *format, ...)
119 va_start (args, format);
120 char *value = xvasprintf (format, args);
123 write_attr (w, name, value);
128 start_elem (struct spv_writer *w, const char *name)
130 xmlTextWriterStartElement (w->xml, CHAR_CAST (xmlChar *, name));
134 end_elem (struct spv_writer *w)
136 xmlTextWriterEndElement (w->xml);
140 write_text (struct spv_writer *w, const char *text)
142 xmlTextWriterWriteString (w->xml, CHAR_CAST (xmlChar *, text));
146 write_page_heading (struct spv_writer *w, const struct page_heading *h,
149 start_elem (w, name);
152 start_elem (w, "pageParagraph");
153 for (size_t i = 0; i < h->n; i++)
155 start_elem (w, "text");
156 write_attr (w, "type", "title");
157 write_text (w, h->paragraphs[i].markup); /* XXX */
166 write_page_setup (struct spv_writer *w, const struct page_setup *ps)
168 start_elem (w, "pageSetup");
169 write_attr_format (w, "initial-page-number", "%d", ps->initial_page_number);
170 write_attr (w, "chart-size",
171 (ps->chart_size == PAGE_CHART_AS_IS ? "as-is"
172 : ps->chart_size == PAGE_CHART_FULL_HEIGHT ? "full-height"
173 : ps->chart_size == PAGE_CHART_HALF_HEIGHT ? "half-height"
174 : "quarter-height"));
175 write_attr_format (w, "margin-left", "%.2fin", ps->margins[TABLE_HORZ][0]);
176 write_attr_format (w, "margin-right", "%.2fin", ps->margins[TABLE_HORZ][1]);
177 write_attr_format (w, "margin-top", "%.2fin", ps->margins[TABLE_VERT][0]);
178 write_attr_format (w, "margin-bottom", "%.2fin", ps->margins[TABLE_VERT][1]);
179 write_attr_format (w, "paper-height", "%.2fin", ps->paper[TABLE_VERT]);
180 write_attr_format (w, "paper-width", "%.2fin", ps->paper[TABLE_HORZ]);
181 write_attr (w, "reference-orientation",
182 ps->orientation == PAGE_PORTRAIT ? "portrait" : "landscape");
183 write_attr_format (w, "space-after", "%.1fpt", ps->object_spacing * 72.0);
184 write_page_heading (w, &ps->headings[0], "pageHeader");
185 write_page_heading (w, &ps->headings[1], "pageFooter");
190 spv_writer_open_file (struct spv_writer *w)
192 w->heading = create_temp_file ();
196 w->xml = xmlNewTextWriter (xmlOutputBufferCreateFile (w->heading, NULL));
197 xmlTextWriterStartDocument (w->xml, NULL, "UTF-8", NULL);
198 start_elem (w, "heading");
200 time_t t = time (NULL);
201 struct tm *tm = gmtime (&t);
202 char *tm_s = asctime (tm);
203 write_attr (w, "creation-date-time", tm_s);
205 write_attr (w, "creator", version);
207 write_attr (w, "creator-version", "21");
209 write_attr (w, "xmlns", "http://xml.spss.com/spss/viewer/viewer-tree");
210 write_attr (w, "xmlns:vps", "http://xml.spss.com/spss/viewer/viewer-pagesetup");
211 write_attr (w, "xmlns:vtx", "http://xml.spss.com/spss/viewer/viewer-text");
212 write_attr (w, "xmlns:vtb", "http://xml.spss.com/spss/viewer/viewer-table");
214 start_elem (w, "label");
215 write_text (w, _("Output"));
220 write_page_setup (w, w->page_setup);
222 page_setup_destroy (w->page_setup);
223 w->page_setup = NULL;
230 spv_writer_open_heading (struct spv_writer *w, const char *command_id,
235 if (!spv_writer_open_file (w))
240 start_elem (w, "heading");
241 write_attr (w, "commandName", command_id);
245 start_elem (w, "label");
246 write_text (w, label);
251 spv_writer_close_file (struct spv_writer *w, const char *infix)
257 xmlTextWriterEndDocument (w->xml);
258 xmlFreeTextWriter (w->xml);
260 char *member_name = xasprintf ("outputViewer%010d%s.xml",
261 w->n_headings++, infix);
262 zip_writer_add (w->zw, w->heading, member_name);
269 spv_writer_close_heading (struct spv_writer *w)
271 const char *infix = "";
272 if (w->heading_depth)
279 if (!w->heading_depth)
280 spv_writer_close_file (w, infix);
284 start_container (struct spv_writer *w, const struct output_item *item)
286 start_elem (w, "container");
287 write_attr (w, "visibility", "visible");
288 if (w->need_page_break)
290 write_attr (w, "page-break-before", "always");
291 w->need_page_break = false;
294 start_elem (w, "label");
295 write_text (w, output_item_get_label (item));
300 spv_writer_put_text (struct spv_writer *w, const struct text_item *text,
301 const char *command_id)
303 bool initial_depth = w->heading_depth;
305 spv_writer_open_file (w);
307 start_container (w, &text->output_item);
309 start_elem (w, "vtx:text");
310 write_attr (w, "type", (text->type == TEXT_ITEM_TITLE ? "title"
311 : text->type == TEXT_ITEM_PAGE_TITLE ? "page-title"
314 write_attr (w, "commandName", command_id);
316 start_elem (w, "html");
317 write_text (w, text->text); /* XXX */
318 end_elem (w); /* html */
319 end_elem (w); /* vtx:text */
320 end_elem (w); /* container */
323 spv_writer_close_file (w, "");
326 static cairo_status_t
327 write_to_zip (void *zw_, const unsigned char *data, unsigned int length)
329 struct zip_writer *zw = zw_;
331 zip_writer_add_write (zw, data, length);
332 return CAIRO_STATUS_SUCCESS;
336 spv_writer_put_image (struct spv_writer *w, const struct output_item *item,
337 cairo_surface_t *image)
339 bool initial_depth = w->heading_depth;
341 spv_writer_open_file (w);
343 char *uri = xasprintf ("%010d_Imagegeneric.png", ++w->n_tables);
345 start_container (w, item);
347 start_elem (w, "object");
348 write_attr (w, "type", "unknown");
349 write_attr (w, "uri", uri);
350 end_elem (w); /* object */
351 end_elem (w); /* container */
354 spv_writer_close_file (w, "");
356 zip_writer_add_start (w->zw, uri);
357 cairo_surface_write_to_png_stream (image, write_to_zip, w->zw);
358 zip_writer_add_finish (w->zw);
374 put_uninit (struct buf *b, size_t n)
376 while (b->allocated - b->len < n)
377 b->data = x2nrealloc (b->data, &b->allocated, sizeof b->data);
378 uint8_t *p = &b->data[b->len];
384 put_byte (struct buf *b, uint8_t byte)
386 *put_uninit (b, 1) = byte;
390 put_bool (struct buf *b, bool boolean)
392 put_byte (b, boolean);
396 put_bytes (struct buf *b, const char *bytes, size_t n)
398 memcpy (put_uninit (b, n), bytes, n);
402 put_u16 (struct buf *b, uint16_t x)
404 put_uint16 (native_to_le16 (x), put_uninit (b, sizeof x));
408 put_u32 (struct buf *b, uint32_t x)
410 put_uint32 (native_to_le32 (x), put_uninit (b, sizeof x));
414 put_u64 (struct buf *b, uint64_t x)
416 put_uint64 (native_to_le64 (x), put_uninit (b, sizeof x));
420 put_be32 (struct buf *b, uint32_t x)
422 put_uint32 (native_to_be32 (x), put_uninit (b, sizeof x));
426 put_double (struct buf *b, double x)
428 float_convert (FLOAT_NATIVE_DOUBLE, &x,
429 FLOAT_IEEE_DOUBLE_LE, put_uninit (b, 8));
433 put_float (struct buf *b, float x)
435 float_convert (FLOAT_NATIVE_FLOAT, &x,
436 FLOAT_IEEE_SINGLE_LE, put_uninit (b, 4));
440 put_string (struct buf *b, const char *s_)
442 const char *s = s_ ? s_ : "";
443 size_t len = strlen (s);
445 memcpy (put_uninit (b, len), s, len);
449 put_bestring (struct buf *b, const char *s_)
451 const char *s = s_ ? s_ : "";
452 size_t len = strlen (s);
454 memcpy (put_uninit (b, len), s, len);
458 start_count (struct buf *b)
465 end_count_u32 (struct buf *b, size_t start)
467 put_uint32 (native_to_le32 (b->len - start), &b->data[start - 4]);
471 end_count_be32 (struct buf *b, size_t start)
473 put_uint32 (native_to_be32 (b->len - start), &b->data[start - 4]);
477 put_color (struct buf *buf, const struct cell_color *color)
479 char *s = xasprintf ("#%02"PRIx8"%02"PRIx8"%02"PRIx8,
480 color->r, color->g, color->b);
486 put_font_style (struct buf *buf, const struct font_style *font_style)
488 put_bool (buf, font_style->bold);
489 put_bool (buf, font_style->italic);
490 put_bool (buf, font_style->underline);
492 put_color (buf, &font_style->fg[0]);
493 put_color (buf, &font_style->bg[0]);
494 put_string (buf, font_style->typeface ? font_style->typeface : "SansSerif");
495 put_byte (buf, ceil (font_style->size * 1.33));
499 put_halign (struct buf *buf, enum table_halign halign,
500 uint32_t mixed, uint32_t decimal)
502 put_u32 (buf, (halign == TABLE_HALIGN_RIGHT ? 4
503 : halign == TABLE_HALIGN_LEFT ? 2
504 : halign == TABLE_HALIGN_CENTER ? 0
505 : halign == TABLE_HALIGN_MIXED ? mixed
510 put_valign (struct buf *buf, enum table_valign valign)
512 put_u32 (buf, (valign == TABLE_VALIGN_TOP ? 1
513 : valign == TABLE_VALIGN_CENTER ? 0
518 put_cell_style (struct buf *buf, const struct cell_style *cell_style)
520 put_halign (buf, cell_style->halign, 0xffffffad, 6);
521 put_valign (buf, cell_style->valign);
522 put_double (buf, cell_style->decimal_offset);
523 put_u16 (buf, cell_style->margin[H][0]);
524 put_u16 (buf, cell_style->margin[H][1]);
525 put_u16 (buf, cell_style->margin[V][0]);
526 put_u16 (buf, cell_style->margin[V][1]);
530 put_style_pair (struct buf *buf, const struct font_style *font_style,
531 const struct cell_style *cell_style)
535 put_byte (buf, 0x31);
536 put_font_style (buf, font_style);
539 put_byte (buf, 0x58);
543 put_byte (buf, 0x31);
544 put_cell_style (buf, cell_style);
547 put_byte (buf, 0x58);
551 put_value_mod (struct buf *buf, const struct pivot_value *value,
552 const char *template)
554 if (value->n_footnotes || value->n_subscripts
555 || template || value->font_style || value->cell_style)
557 put_byte (buf, 0x31);
560 put_u32 (buf, value->n_footnotes);
561 for (size_t i = 0; i < value->n_footnotes; i++)
562 put_u16 (buf, value->footnote_indexes[i]);
565 put_u32 (buf, value->n_subscripts);
566 for (size_t i = 0; i < value->n_subscripts; i++)
567 put_string (buf, value->subscripts[i]);
569 /* Template and style. */
570 uint32_t v3_start = start_count (buf);
571 uint32_t template_string_start = start_count (buf);
574 uint32_t inner_start = start_count (buf);
575 end_count_u32 (buf, inner_start);
577 put_byte (buf, 0x31);
578 put_string (buf, template);
580 end_count_u32 (buf, template_string_start);
581 put_style_pair (buf, value->font_style, value->cell_style);
582 end_count_u32 (buf, v3_start);
585 put_byte (buf, 0x58);
589 put_format (struct buf *buf, const struct fmt_spec *f, bool honor_small)
591 int type = f->type == FMT_F && honor_small ? 40 : fmt_to_io (f->type);
592 put_u32 (buf, (type << 16) | (f->w << 8) | f->d);
596 show_values_to_spvlb (enum settings_value_show show)
598 return (show == SETTINGS_VALUE_SHOW_DEFAULT ? 0
599 : show == SETTINGS_VALUE_SHOW_VALUE ? 1
600 : show == SETTINGS_VALUE_SHOW_LABEL ? 2
605 put_show_values (struct buf *buf, enum settings_value_show show)
607 put_byte (buf, show_values_to_spvlb (show));
611 put_value (struct buf *buf, const struct pivot_value *value)
615 case PIVOT_VALUE_NUMERIC:
616 if (value->numeric.var_name || value->numeric.value_label)
619 put_value_mod (buf, value, NULL);
620 put_format (buf, &value->numeric.format, value->numeric.honor_small);
621 put_double (buf, value->numeric.x);
622 put_string (buf, value->numeric.var_name);
623 put_string (buf, value->numeric.value_label);
624 put_show_values (buf, value->numeric.show);
629 put_value_mod (buf, value, NULL);
630 put_format (buf, &value->numeric.format, value->numeric.honor_small);
631 put_double (buf, value->numeric.x);
635 case PIVOT_VALUE_STRING:
637 put_value_mod (buf, value, NULL);
638 size_t len = strlen (value->string.s);
639 if (value->string.hex)
640 put_format (buf, &(struct fmt_spec) { FMT_AHEX, len * 2, 0 }, false);
642 put_format (buf, &(struct fmt_spec) { FMT_A, len, 0 }, false);
643 put_string (buf, value->string.value_label);
644 put_string (buf, value->string.var_name);
645 put_show_values (buf, value->string.show);
646 put_string (buf, value->string.s);
649 case PIVOT_VALUE_VARIABLE:
651 put_value_mod (buf, value, NULL);
652 put_string (buf, value->variable.var_name);
653 put_string (buf, value->variable.var_label);
654 put_show_values (buf, value->variable.show);
657 case PIVOT_VALUE_TEXT:
659 put_string (buf, value->text.local);
660 put_value_mod (buf, value, NULL);
661 put_string (buf, value->text.id);
662 put_string (buf, value->text.c);
663 put_byte (buf, 1); /* XXX user-provided */
666 case PIVOT_VALUE_TEMPLATE:
668 put_value_mod (buf, value, value->template.id);
669 put_string (buf, value->template.local);
670 put_u32 (buf, value->template.n_args);
671 for (size_t i = 0; i < value->template.n_args; i++)
673 const struct pivot_argument *arg = &value->template.args[i];
674 assert (arg->n >= 1);
677 put_u32 (buf, arg->n);
679 for (size_t j = 0; j < arg->n; j++)
682 put_bytes (buf, "\0\0\0\0", 4);
683 put_value (buf, arg->values[j]);
689 put_value (buf, arg->values[0]);
700 put_optional_value (struct buf *buf, const struct pivot_value *value)
704 put_byte (buf, 0x31);
705 put_value (buf, value);
708 put_byte (buf, 0x58);
712 put_category (struct buf *buf, const struct pivot_category *c)
714 put_value (buf, c->name);
715 if (pivot_category_is_leaf (c))
717 put_bytes (buf, "\0\0\0", 3);
719 put_u32 (buf, c->data_index);
724 put_bytes (buf, "\0\0\1", 3);
725 put_u32 (buf, 0); /* x23 */
727 put_u32 (buf, c->n_subs);
728 for (size_t i = 0; i < c->n_subs; i++)
729 put_category (buf, c->subs[i]);
734 put_y0 (struct buf *buf, const struct pivot_table *table)
736 put_u32 (buf, table->settings.epoch);
737 put_byte (buf, table->settings.decimal);
742 put_custom_currency (struct buf *buf, const struct pivot_table *table)
745 for (int i = 0; i < 5; i++)
747 enum fmt_type types[5] = { FMT_CCA, FMT_CCB, FMT_CCC, FMT_CCD, FMT_CCE };
748 char *cc = fmt_number_style_to_string (fmt_settings_get_style (
749 &table->settings, types[i]));
750 put_string (buf, cc);
756 put_x1 (struct buf *buf, const struct pivot_table *table)
758 put_byte (buf, 0); /* x14 */
759 put_byte (buf, table->show_title ? 1 : 10);
760 put_byte (buf, 0); /* x16 */
761 put_byte (buf, 0); /* lang */
762 put_show_values (buf, table->show_variables);
763 put_show_values (buf, table->show_values);
764 put_u32 (buf, -1); /* x18 */
765 put_u32 (buf, -1); /* x19 */
766 for (int i = 0; i < 17; i++)
768 put_bool (buf, false); /* x20 */
769 put_byte (buf, table->show_caption);
773 put_x2 (struct buf *buf)
775 put_u32 (buf, 0); /* n-row-heights */
776 put_u32 (buf, 0); /* n-style-map */
777 put_u32 (buf, 0); /* n-styles */
782 put_y1 (struct buf *buf, const struct pivot_table *table)
784 put_string (buf, table->command_c);
785 put_string (buf, table->command_local);
786 put_string (buf, table->language);
787 put_string (buf, "UTF-8"); /* XXX */
788 put_string (buf, table->locale);
789 put_bytes (buf, "\0\0\1\1", 4);
794 put_y2 (struct buf *buf, const struct pivot_table *table)
796 put_custom_currency (buf, table);
802 put_x3 (struct buf *buf, const struct pivot_table *table)
806 put_byte (buf, 4); /* x21 */
811 put_double (buf, table->small);
813 put_string (buf, table->dataset);
814 put_string (buf, table->datafile);
816 put_u32 (buf, table->date);
822 encode_current_layer (const struct pivot_table *table)
824 uint32_t current_layer = 0;
826 const struct pivot_axis *axis = &table->axes[PIVOT_AXIS_LAYER];
827 for (size_t i = axis->n_dimensions - 1; i < axis->n_dimensions; i--)
829 const struct pivot_dimension *d = axis->dimensions[i];
830 current_layer = current_layer * d->n_leaves + table->current_layer[i];
833 return current_layer;
837 put_light_table (struct buf *buf, uint64_t table_id,
838 const struct pivot_table *table)
841 put_bytes (buf, "\1\0", 2);
843 put_bool (buf, true);
844 put_bool (buf, false);
845 put_bool (buf, table->rotate_inner_column_labels);
846 put_bool (buf, table->rotate_outer_row_labels);
847 put_bool (buf, true);
849 put_u32 (buf, table->look->width_ranges[H][0]);
850 put_u32 (buf, table->look->width_ranges[H][1]);
851 put_u32 (buf, table->look->width_ranges[V][0]);
852 put_u32 (buf, table->look->width_ranges[V][1]);
853 put_u64 (buf, table_id);
856 put_value (buf, table->title);
857 put_value (buf, table->subtype);
858 put_optional_value (buf, table->title);
859 put_optional_value (buf, table->corner_text);
860 put_optional_value (buf, table->caption);
863 put_u32 (buf, table->n_footnotes);
864 for (size_t i = 0; i < table->n_footnotes; i++)
866 const struct pivot_footnote *f = table->footnotes[i];
867 put_value (buf, f->content);
868 put_optional_value (buf, f->marker);
869 put_u32 (buf, f->show ? 1 : -1);
873 for (size_t i = 0; i < PIVOT_N_AREAS; i++)
875 const struct table_area_style *a = &table->look->areas[i];
876 put_byte (buf, i + 1);
877 put_byte (buf, 0x31);
878 put_string (buf, (a->font_style.typeface
879 ? a->font_style.typeface
881 put_float (buf, ceil (a->font_style.size * 1.33));
882 put_u32 (buf, ((a->font_style.bold ? 1 : 0)
883 | (a->font_style.italic ? 2 : 0)));
884 put_bool (buf, a->font_style.underline);
885 put_halign (buf, a->cell_style.halign, 64173, 61453);
886 put_valign (buf, a->cell_style.valign);
888 put_color (buf, &a->font_style.fg[0]);
889 put_color (buf, &a->font_style.bg[0]);
892 = (!cell_color_equal (&a->font_style.fg[0], &a->font_style.fg[1])
893 || !cell_color_equal (&a->font_style.bg[0], &a->font_style.bg[1]));
897 put_color (buf, &a->font_style.fg[1]);
898 put_color (buf, &a->font_style.bg[1]);
902 put_string (buf, "");
903 put_string (buf, "");
906 put_u32 (buf, a->cell_style.margin[H][0]);
907 put_u32 (buf, a->cell_style.margin[H][1]);
908 put_u32 (buf, a->cell_style.margin[V][0]);
909 put_u32 (buf, a->cell_style.margin[V][1]);
913 uint32_t borders_start = start_count (buf);
915 put_be32 (buf, PIVOT_N_BORDERS);
916 for (size_t i = 0; i < PIVOT_N_BORDERS; i++)
918 const struct table_border_style *b = &table->look->borders[i];
920 put_be32 (buf, (b->stroke == TABLE_STROKE_NONE ? 0
921 : b->stroke == TABLE_STROKE_SOLID ? 1
922 : b->stroke == TABLE_STROKE_DASHED ? 2
923 : b->stroke == TABLE_STROKE_THICK ? 3
924 : b->stroke == TABLE_STROKE_THIN ? 4
926 put_be32 (buf, ((b->color.alpha << 24)
931 put_bool (buf, table->show_grid_lines);
932 put_bytes (buf, "\0\0\0", 3);
933 end_count_u32 (buf, borders_start);
935 /* Print Settings. */
936 uint32_t ps_start = start_count (buf);
938 put_bool (buf, table->look->print_all_layers);
939 put_bool (buf, table->look->paginate_layers);
940 put_bool (buf, table->look->shrink_to_fit[H]);
941 put_bool (buf, table->look->shrink_to_fit[V]);
942 put_bool (buf, table->look->top_continuation);
943 put_bool (buf, table->look->bottom_continuation);
944 put_be32 (buf, table->look->n_orphan_lines);
945 put_bestring (buf, table->look->continuation);
946 end_count_u32 (buf, ps_start);
948 /* Table Settings. */
949 uint32_t ts_start = start_count (buf);
952 put_be32 (buf, encode_current_layer (table));
953 put_bool (buf, table->look->omit_empty);
954 put_bool (buf, table->look->row_labels_in_corner);
955 put_bool (buf, !table->look->show_numeric_markers);
956 put_bool (buf, table->look->footnote_marker_superscripts);
958 uint32_t keep_start = start_count (buf);
959 put_be32 (buf, 0); /* n-row-breaks */
960 put_be32 (buf, 0); /* n-column-breaks */
961 put_be32 (buf, 0); /* n-row-keeps */
962 put_be32 (buf, 0); /* n-column-keeps */
963 put_be32 (buf, 0); /* n-row-point-keeps */
964 put_be32 (buf, 0); /* n-column-point-keeps */
965 end_count_be32 (buf, keep_start);
966 put_bestring (buf, table->notes);
967 put_bestring (buf, table->look->name);
968 for (size_t i = 0; i < 82; i++)
970 end_count_u32 (buf, ts_start);
973 put_u32 (buf, 0); /* n-widths */
974 put_string (buf, "en_US.ISO_8859-1:1987"); /* XXX */
975 put_u32 (buf, 0); /* XXX current-layer */
980 put_custom_currency (buf, table);
981 uint32_t formats_start = start_count (buf);
982 uint32_t x1_start = start_count (buf);
984 uint32_t x2_start = start_count (buf);
986 end_count_u32 (buf, x2_start);
987 end_count_u32 (buf, x1_start);
988 uint32_t x3_start = start_count (buf);
990 end_count_u32 (buf, x3_start);
991 end_count_u32 (buf, formats_start);
994 put_u32 (buf, table->n_dimensions);
995 int *x2 = xnmalloc (table->n_dimensions, sizeof *x2);
996 for (size_t i = 0; i < table->axes[PIVOT_AXIS_LAYER].n_dimensions; i++)
998 for (size_t i = 0; i < table->axes[PIVOT_AXIS_ROW].n_dimensions; i++)
999 x2[i + table->axes[PIVOT_AXIS_LAYER].n_dimensions] = 0;
1000 for (size_t i = 0; i < table->axes[PIVOT_AXIS_COLUMN].n_dimensions; i++)
1002 + table->axes[PIVOT_AXIS_LAYER].n_dimensions
1003 + table->axes[PIVOT_AXIS_ROW].n_dimensions] = 1;
1004 for (size_t i = 0; i < table->n_dimensions; i++)
1006 const struct pivot_dimension *d = table->dimensions[i];
1007 put_value (buf, d->root->name);
1008 put_byte (buf, 0); /* x1 */
1009 put_byte (buf, x2[i]);
1010 put_u32 (buf, 2); /* x3 */
1011 put_bool (buf, !d->root->show_label);
1012 put_bool (buf, d->hide_all_labels);
1016 put_u32 (buf, d->root->n_subs);
1017 for (size_t j = 0; j < d->root->n_subs; j++)
1018 put_category (buf, d->root->subs[j]);
1023 put_u32 (buf, table->axes[PIVOT_AXIS_LAYER].n_dimensions);
1024 put_u32 (buf, table->axes[PIVOT_AXIS_ROW].n_dimensions);
1025 put_u32 (buf, table->axes[PIVOT_AXIS_COLUMN].n_dimensions);
1026 for (size_t i = 0; i < table->axes[PIVOT_AXIS_LAYER].n_dimensions; i++)
1027 put_u32 (buf, table->axes[PIVOT_AXIS_LAYER].dimensions[i]->top_index);
1028 for (size_t i = 0; i < table->axes[PIVOT_AXIS_ROW].n_dimensions; i++)
1029 put_u32 (buf, table->axes[PIVOT_AXIS_ROW].dimensions[i]->top_index);
1030 for (size_t i = 0; i < table->axes[PIVOT_AXIS_COLUMN].n_dimensions; i++)
1031 put_u32 (buf, table->axes[PIVOT_AXIS_COLUMN].dimensions[i]->top_index);
1034 put_u32 (buf, hmap_count (&table->cells));
1035 const struct pivot_cell *cell;
1036 HMAP_FOR_EACH (cell, struct pivot_cell, hmap_node, &table->cells)
1039 for (size_t j = 0; j < table->n_dimensions; j++)
1040 index = (table->dimensions[j]->n_leaves * index) + cell->idx[j];
1041 put_u64 (buf, index);
1043 put_value (buf, cell->value);
1048 spv_writer_put_table (struct spv_writer *w, const struct table_item *item)
1050 int table_id = ++w->n_tables;
1052 bool initial_depth = w->heading_depth;
1054 spv_writer_open_file (w);
1056 start_container (w, &item->output_item);
1058 char *subtype = (item->pt->subtype
1059 ? pivot_value_to_string (item->pt->subtype, item->pt)
1060 : xstrdup ("unknown"));
1062 start_elem (w, "vtb:table");
1063 write_attr (w, "commandName", item->pt->command_c);
1064 write_attr (w, "type", "table"); /* XXX */
1065 write_attr (w, "subType", subtype);
1066 write_attr_format (w, "tableId", "%d", table_id);
1070 start_elem (w, "vtb:tableStructure");
1071 start_elem (w, "vtb:dataPath");
1072 char *data_path = xasprintf ("%010d_lightTableData.bin", table_id);
1073 write_text (w, data_path);
1074 end_elem (w); /* vtb:dataPath */
1075 end_elem (w); /* vtb:tableStructure */
1076 end_elem (w); /* vtb:table */
1077 end_elem (w); /* container */
1080 spv_writer_close_file (w, "");
1082 struct buf buf = { NULL, 0, 0 };
1083 put_light_table (&buf, table_id, item->pt);
1084 zip_writer_add_memory (w->zw, data_path, buf.data, buf.len);
1091 spv_writer_write (struct spv_writer *w, const struct output_item *item)
1093 if (is_group_open_item (item))
1094 spv_writer_open_heading (w,
1095 to_group_open_item (item)->command_name,
1096 to_group_open_item (item)->command_name);
1097 else if (is_group_close_item (item))
1098 spv_writer_close_heading (w);
1099 else if (is_table_item (item))
1100 spv_writer_put_table (w, to_table_item (item));
1101 else if (is_chart_item (item))
1103 cairo_surface_t *surface = xr_draw_image_chart (
1104 to_chart_item (item),
1105 &(struct cell_color) CELL_COLOR_BLACK,
1106 &(struct cell_color) CELL_COLOR_WHITE);
1107 if (cairo_surface_status (surface) == CAIRO_STATUS_SUCCESS)
1108 spv_writer_put_image (w, item, surface);
1109 cairo_surface_destroy (surface);
1111 else if (is_image_item (item))
1112 spv_writer_put_image (w, item, to_image_item (item)->image);
1113 else if (is_text_item (item))
1115 char *command_id = output_get_command_name ();
1116 spv_writer_put_text (w, to_text_item (item),
1120 else if (is_page_eject_item (item))
1121 w->need_page_break = true;
1122 else if (is_page_setup_item (item))
1124 page_setup_destroy (w->page_setup);
1125 w->page_setup = page_setup_clone (to_page_setup_item (item)->page_setup);