1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2017, 2018 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.h"
23 #include <libxml/HTMLparser.h>
24 #include <libxml/xmlreader.h>
28 #include "libpspp/assertion.h"
29 #include "libpspp/cast.h"
30 #include "libpspp/hash-functions.h"
31 #include "libpspp/message.h"
32 #include "libpspp/str.h"
33 #include "libpspp/zip-reader.h"
34 #include "output/page-setup-item.h"
35 #include "output/pivot-table.h"
36 #include "output/spv/detail-xml-parser.h"
37 #include "output/spv/light-binary-parser.h"
38 #include "output/spv/spv-css-parser.h"
39 #include "output/spv/spv-legacy-data.h"
40 #include "output/spv/spv-legacy-decoder.h"
41 #include "output/spv/spv-light-decoder.h"
42 #include "output/spv/spv-table-look.h"
43 #include "output/spv/structure-xml-parser.h"
45 #include "gl/c-ctype.h"
46 #include "gl/intprops.h"
47 #include "gl/minmax.h"
48 #include "gl/xalloc.h"
49 #include "gl/xvasprintf.h"
53 #define _(msgid) gettext (msgid)
54 #define N_(msgid) (msgid)
58 struct string zip_errs;
59 struct zip_reader *zip;
60 struct spv_item *root;
61 struct page_setup *page_setup;
64 const struct page_setup *
65 spv_get_page_setup (const struct spv_reader *spv)
67 return spv->page_setup;
71 spv_item_type_to_string (enum spv_item_type type)
75 case SPV_ITEM_HEADING: return "heading";
76 case SPV_ITEM_TEXT: return "text";
77 case SPV_ITEM_TABLE: return "table";
78 case SPV_ITEM_GRAPH: return "graph";
79 case SPV_ITEM_MODEL: return "model";
80 case SPV_ITEM_IMAGE: return "image";
81 default: return "**error**";
86 spv_item_class_to_string (enum spv_item_class class)
90 #define SPV_CLASS(ENUM, NAME) case SPV_CLASS_##ENUM: return NAME;
98 spv_item_class_from_string (const char *name)
100 #define SPV_CLASS(ENUM, NAME) \
101 if (!strcmp (name, NAME)) return SPV_CLASS_##ENUM;
105 return (enum spv_item_class) SPV_N_CLASSES;
109 spv_item_get_type (const struct spv_item *item)
115 spv_item_get_class (const struct spv_item *item)
117 const char *label = spv_item_get_label (item);
123 case SPV_ITEM_HEADING:
124 return SPV_CLASS_HEADINGS;
127 return (!strcmp (label, "Title") ? SPV_CLASS_OUTLINEHEADERS
128 : !strcmp (label, "Log") ? SPV_CLASS_LOGS
129 : !strcmp (label, "Page Title") ? SPV_CLASS_PAGETITLE
133 return (!strcmp (label, "Warnings") ? SPV_CLASS_WARNINGS
134 : !strcmp (label, "Notes") ? SPV_CLASS_NOTES
138 return SPV_CLASS_CHARTS;
141 return SPV_CLASS_MODELS;
144 return SPV_CLASS_OTHER;
147 return SPV_CLASS_TREES;
150 return SPV_CLASS_UNKNOWN;
155 spv_item_get_label (const struct spv_item *item)
161 spv_item_is_heading (const struct spv_item *item)
163 return item->type == SPV_ITEM_HEADING;
167 spv_item_get_n_children (const struct spv_item *item)
169 return item->n_children;
173 spv_item_get_child (const struct spv_item *item, size_t idx)
175 assert (idx < item->n_children);
176 return item->children[idx];
180 spv_item_is_table (const struct spv_item *item)
182 return item->type == SPV_ITEM_TABLE;
186 spv_item_is_text (const struct spv_item *item)
188 return item->type == SPV_ITEM_TEXT;
191 const struct pivot_value *
192 spv_item_get_text (const struct spv_item *item)
194 assert (spv_item_is_text (item));
199 spv_item_is_image (const struct spv_item *item)
201 return item->type == SPV_ITEM_IMAGE;
205 static cairo_status_t
206 read_from_zip_member (void *zm_, unsigned char *data, unsigned int length)
208 struct zip_member *zm = zm_;
210 return CAIRO_STATUS_READ_ERROR;
214 int n = zip_member_read (zm, data, length);
216 return CAIRO_STATUS_READ_ERROR;
222 return CAIRO_STATUS_SUCCESS;
226 spv_item_get_image (const struct spv_item *item_)
228 struct spv_item *item = CONST_CAST (struct spv_item *, item_);
229 assert (spv_item_is_image (item));
233 struct zip_member *zm = zip_member_open (item->spv->zip,
235 item->image = cairo_image_surface_create_from_png_stream (
236 read_from_zip_member, zm);
238 zip_member_finish (zm);
246 spv_item_next (const struct spv_item *item)
248 if (item->n_children)
249 return item->children[0];
253 size_t idx = item->parent_idx + 1;
255 if (idx < item->n_children)
256 return item->children[idx];
262 const struct spv_item *
263 spv_item_get_parent (const struct spv_item *item)
269 spv_item_get_level (const struct spv_item *item)
272 for (; item->parent; item = item->parent)
278 spv_item_get_command_id (const struct spv_item *item)
280 return item->command_id;
284 spv_item_get_subtype (const struct spv_item *item)
286 return item->subtype;
290 spv_item_is_visible (const struct spv_item *item)
292 return item->visible;
296 spv_item_destroy (struct spv_item *item)
300 free (item->structure_member);
303 free (item->command_id);
305 for (size_t i = 0; i < item->n_children; i++)
306 spv_item_destroy (item->children[i]);
307 free (item->children);
309 pivot_table_unref (item->table);
310 pivot_table_look_unref (item->table_look);
311 free (item->bin_member);
312 free (item->xml_member);
313 free (item->subtype);
315 pivot_value_destroy (item->text);
317 free (item->png_member);
320 cairo_surface_destroy (item->image);
328 spv_heading_add_child (struct spv_item *parent, struct spv_item *child)
330 assert (parent->type == SPV_ITEM_HEADING);
331 assert (!child->parent);
333 child->parent = parent;
334 child->parent_idx = parent->n_children;
336 if (parent->n_children >= parent->allocated_children)
337 parent->children = x2nrealloc (parent->children,
338 &parent->allocated_children,
339 sizeof *parent->children);
340 parent->children[parent->n_children++] = child;
344 find_xml_child_element (xmlNode *parent, const char *child_name)
346 for (xmlNode *node = parent->children; node; node = node->next)
347 if (node->type == XML_ELEMENT_NODE
349 && !strcmp (CHAR_CAST (char *, node->name), child_name))
356 get_xml_attr (const xmlNode *node, const char *name)
358 return CHAR_CAST (char *, xmlGetProp (node, CHAR_CAST (xmlChar *, name)));
362 put_xml_attr (const char *name, const char *value, struct string *dst)
367 ds_put_format (dst, " %s=\"", name);
368 for (const char *p = value; *p; p++)
373 ds_put_cstr (dst, " ");
376 ds_put_cstr (dst, "&");
379 ds_put_cstr (dst, "<");
382 ds_put_cstr (dst, ">");
385 ds_put_cstr (dst, """);
388 ds_put_byte (dst, *p);
392 ds_put_byte (dst, '"');
396 extract_html_text (const xmlNode *node, int base_font_size, struct string *s)
398 if (node->type == XML_ELEMENT_NODE)
400 const char *name = CHAR_CAST (char *, node->name);
401 if (!strcmp (name, "br"))
402 ds_put_byte (s, '\n');
403 else if (strcmp (name, "style"))
405 const char *tag = NULL;
406 if (strchr ("biu", name[0]) && name[1] == '\0')
409 ds_put_format (s, "<%s>", tag);
411 else if (!strcmp (name, "font"))
414 ds_put_format (s, "<%s", tag);
416 char *face = get_xml_attr (node, "face");
417 put_xml_attr ("face", face, s);
420 char *color = get_xml_attr (node, "color");
424 put_xml_attr ("color", color, s);
428 if (sscanf (color, "rgb (%"SCNu8", %"SCNu8", %"SCNu8")",
432 snprintf (color2, sizeof color2,
433 "#%02"PRIx8"%02"PRIx8"%02"PRIx8,
435 put_xml_attr ("color", color2, s);
441 char *size_s = get_xml_attr (node, "size");
442 int html_size = size_s ? atoi (size_s) : 0;
444 if (html_size >= 1 && html_size <= 7)
446 static const double scale[7] = {
447 .444, .556, .667, .778, 1.0, 1.33, 2.0
449 double size = base_font_size * scale[html_size - 1];
451 char size2[INT_BUFSIZE_BOUND (int)];
452 snprintf (size2, sizeof size2, "%.0f", size * 1024.);
453 put_xml_attr ("size", size2, s);
456 ds_put_cstr (s, ">");
458 for (const xmlNode *child = node->children; child;
460 extract_html_text (child, base_font_size, s);
462 ds_put_format (s, "</%s>", tag);
465 else if (node->type == XML_TEXT_NODE)
467 /* U+00A0 NONBREAKING SPACE is really, really common in SPV text and it
468 makes it impossible to break syntax across lines. Translate it into a
469 regular space. (Note that U+00A0 is C2 A0 in UTF-8.)
471 Do the same for U+2007 FIGURE SPACE, which also crops out weirdly
473 ds_extend (s, ds_length (s) + xmlStrlen (node->content));
474 for (const uint8_t *p = node->content; *p;)
477 if (p[0] == 0xc2 && p[1] == 0xa0)
482 else if (p[0] == 0xe2 && p[1] == 0x80 && p[2] == 0x87)
492 int last = ds_last (s);
493 if (last != EOF && !c_isspace (last))
497 ds_put_cstr (s, "<");
499 ds_put_cstr (s, ">");
501 ds_put_cstr (s, "&");
509 parse_embedded_html (const xmlNode *node)
511 /* Extract HTML from XML node. */
512 char *html_s = CHAR_CAST (char *, xmlNodeGetContent (node));
516 xmlDoc *html_doc = htmlReadMemory (
517 html_s, strlen (html_s),
518 NULL, "UTF-8", (HTML_PARSE_RECOVER | HTML_PARSE_NOERROR
519 | HTML_PARSE_NOWARNING | HTML_PARSE_NOBLANKS
520 | HTML_PARSE_NONET));
526 /* Given NODE, which should contain HTML content, returns the text within that
527 content as an allocated string. The caller must eventually free the
528 returned string (with xmlFree()). */
530 decode_embedded_html (const xmlNode *node, struct font_style *font_style)
532 struct string markup = DS_EMPTY_INITIALIZER;
533 *font_style = (struct font_style) FONT_STYLE_INITIALIZER;
534 font_style->size = 10;
536 xmlDoc *html_doc = parse_embedded_html (node);
539 xmlNode *root = xmlDocGetRootElement (html_doc);
540 xmlNode *head = root ? find_xml_child_element (root, "head") : NULL;
541 xmlNode *style = head ? find_xml_child_element (head, "style") : NULL;
544 uint8_t *style_s = xmlNodeGetContent (style);
545 spv_parse_css_style (CHAR_CAST (char *, style_s), font_style);
550 extract_html_text (root, font_style->size, &markup);
551 xmlFreeDoc (html_doc);
554 font_style->markup = true;
555 return ds_steal_cstr (&markup);
559 xstrdup_if_nonempty (const char *s)
561 return s && s[0] ? xstrdup (s) : NULL;
565 decode_container_text (const struct spvsx_container_text *ct,
566 struct spv_item *item)
568 item->type = SPV_ITEM_TEXT;
569 item->command_id = xstrdup_if_nonempty (ct->command_name);
571 item->text = xzalloc (sizeof *item->text);
572 item->text->type = PIVOT_VALUE_TEXT;
573 item->text->font_style = xmalloc (sizeof *item->text->font_style);
574 item->text->text.local = decode_embedded_html (ct->html->node_.raw,
575 item->text->font_style);
579 decode_page_p (const xmlNode *in, struct page_paragraph *out)
581 char *style = get_xml_attr (in, "style");
582 out->halign = (style && strstr (style, "center") ? TABLE_HALIGN_CENTER
583 : style && strstr (style, "right") ? TABLE_HALIGN_RIGHT
584 : TABLE_HALIGN_LEFT);
587 struct font_style font_style;
588 out->markup = decode_embedded_html (in, &font_style);
589 font_style_uninit (&font_style);
593 decode_page_paragraph (const struct spvsx_page_paragraph *page_paragraph,
594 struct page_heading *ph)
596 memset (ph, 0, sizeof *ph);
598 const struct spvsx_page_paragraph_text *page_paragraph_text
599 = page_paragraph->page_paragraph_text;
600 if (!page_paragraph_text)
603 xmlDoc *html_doc = parse_embedded_html (page_paragraph_text->node_.raw);
607 xmlNode *root = xmlDocGetRootElement (html_doc);
608 xmlNode *body = find_xml_child_element (root, "body");
610 for (const xmlNode *node = body->children; node; node = node->next)
611 if (node->type == XML_ELEMENT_NODE
612 && !strcmp (CHAR_CAST (const char *, node->name), "p"))
614 ph->paragraphs = xrealloc (ph->paragraphs,
615 (ph->n + 1) * sizeof *ph->paragraphs);
616 decode_page_p (node, &ph->paragraphs[ph->n++]);
618 xmlFreeDoc (html_doc);
622 spv_item_load (const struct spv_item *item)
624 if (spv_item_is_table (item))
625 spv_item_get_table (item);
627 else if (spv_item_is_image (item))
628 spv_item_get_image (item);
633 spv_item_is_light_table (const struct spv_item *item)
635 return item->type == SPV_ITEM_TABLE && !item->xml_member;
638 char * WARN_UNUSED_RESULT
639 spv_item_get_raw_light_table (const struct spv_item *item,
640 void **data, size_t *size)
642 return zip_member_read_all (item->spv->zip, item->bin_member, data, size);
645 char * WARN_UNUSED_RESULT
646 spv_item_get_light_table (const struct spv_item *item,
647 struct spvlb_table **tablep)
651 if (!spv_item_is_light_table (item))
652 return xstrdup ("not a light binary table object");
656 char *error = spv_item_get_raw_light_table (item, &data, &size);
660 struct spvbin_input input;
661 spvbin_input_init (&input, data, size);
663 struct spvlb_table *table = NULL;
665 ? xasprintf ("light table member is empty")
666 : !spvlb_parse_table (&input, &table)
667 ? spvbin_input_to_error (&input, NULL)
668 : input.ofs != input.size
669 ? xasprintf ("expected end of file at offset %#zx", input.ofs)
673 struct string s = DS_EMPTY_INITIALIZER;
674 spv_item_format_path (item, &s);
675 ds_put_format (&s, " (%s): %s", item->bin_member, error);
678 error = ds_steal_cstr (&s);
687 pivot_table_open_light (struct spv_item *item)
689 assert (spv_item_is_light_table (item));
691 struct spvlb_table *raw_table;
692 char *error = spv_item_get_light_table (item, &raw_table);
694 error = decode_spvlb_table (raw_table, &item->table);
695 spvlb_free_table (raw_table);
701 spv_item_is_legacy_table (const struct spv_item *item)
703 return item->type == SPV_ITEM_TABLE && item->xml_member;
706 char * WARN_UNUSED_RESULT
707 spv_item_get_raw_legacy_data (const struct spv_item *item,
708 void **data, size_t *size)
710 if (!spv_item_is_legacy_table (item))
711 return xstrdup ("not a legacy table object");
713 return zip_member_read_all (item->spv->zip, item->bin_member, data, size);
716 char * WARN_UNUSED_RESULT
717 spv_item_get_legacy_data (const struct spv_item *item, struct spv_data *data)
721 char *error = spv_item_get_raw_legacy_data (item, &raw, &size);
724 error = spv_legacy_data_decode (raw, size, data);
731 static char * WARN_UNUSED_RESULT
732 spv_read_xml_member (struct spv_reader *spv, const char *member_name,
733 bool keep_blanks, const char *root_element_name,
738 struct zip_member *zm = zip_member_open (spv->zip, member_name);
740 return ds_steal_cstr (&spv->zip_errs);
742 xmlParserCtxt *parser;
743 xmlKeepBlanksDefault (keep_blanks);
744 parser = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL);
747 zip_member_finish (zm);
748 return xasprintf (_("%s: Failed to create XML parser"), member_name);
753 while ((retval = zip_member_read (zm, buf, sizeof buf)) > 0)
754 xmlParseChunk (parser, buf, retval, false);
755 xmlParseChunk (parser, NULL, 0, true);
757 xmlDoc *doc = parser->myDoc;
758 bool well_formed = parser->wellFormed;
759 xmlFreeParserCtxt (parser);
763 char *error = ds_steal_cstr (&spv->zip_errs);
764 zip_member_finish (zm);
768 zip_member_finish (zm);
773 return xasprintf(_("%s: document is not well-formed"), member_name);
776 const xmlNode *root_node = xmlDocGetRootElement (doc);
777 assert (root_node->type == XML_ELEMENT_NODE);
778 if (strcmp (CHAR_CAST (char *, root_node->name), root_element_name))
781 return xasprintf(_("%s: root node is \"%s\" but \"%s\" was expected"),
783 CHAR_CAST (char *, root_node->name), root_element_name);
790 char * WARN_UNUSED_RESULT
791 spv_item_get_legacy_table (const struct spv_item *item, xmlDoc **docp)
793 assert (spv_item_is_legacy_table (item));
795 return spv_read_xml_member (item->spv, item->xml_member, false,
796 "visualization", docp);
799 char * WARN_UNUSED_RESULT
800 spv_item_get_structure (const struct spv_item *item, struct _xmlDoc **docp)
802 return spv_read_xml_member (item->spv, item->structure_member, false,
807 identify_item (const struct spv_item *item)
809 return (item->label ? item->label
810 : item->command_id ? item->command_id
811 : spv_item_type_to_string (item->type));
815 spv_item_format_path (const struct spv_item *item, struct string *s)
817 enum { MAX_STACK = 32 };
818 const struct spv_item *stack[MAX_STACK];
821 while (item != NULL && item->parent && n < MAX_STACK)
830 ds_put_byte (s, '/');
832 const char *name = identify_item (item);
833 ds_put_cstr (s, name);
839 for (size_t i = 0; i < item->parent->n_children; i++)
841 const struct spv_item *sibling = item->parent->children[i];
844 else if (!strcmp (name, identify_item (sibling)))
848 ds_put_format (s, "[%zu]", index);
853 static char * WARN_UNUSED_RESULT
854 pivot_table_open_legacy (struct spv_item *item)
856 assert (spv_item_is_legacy_table (item));
858 struct spv_data data;
859 char *error = spv_item_get_legacy_data (item, &data);
862 struct string s = DS_EMPTY_INITIALIZER;
863 spv_item_format_path (item, &s);
864 ds_put_format (&s, " (%s): %s", item->bin_member, error);
867 return ds_steal_cstr (&s);
871 error = spv_read_xml_member (item->spv, item->xml_member, false,
872 "visualization", &doc);
875 spv_data_uninit (&data);
879 struct spvxml_context ctx = SPVXML_CONTEXT_INIT (ctx);
880 struct spvdx_visualization *v;
881 spvdx_parse_visualization (&ctx, xmlDocGetRootElement (doc), &v);
882 error = spvxml_context_finish (&ctx, &v->node_);
885 error = decode_spvdx_table (v, item->subtype, item->table_look,
886 &data, &item->table);
890 struct string s = DS_EMPTY_INITIALIZER;
891 spv_item_format_path (item, &s);
892 ds_put_format (&s, " (%s): %s", item->xml_member, error);
895 error = ds_steal_cstr (&s);
898 spv_data_uninit (&data);
899 spvdx_free_visualization (v);
906 const struct pivot_table *
907 spv_item_get_table (const struct spv_item *item_)
909 struct spv_item *item = CONST_CAST (struct spv_item *, item_);
911 assert (spv_item_is_table (item));
914 char *error = (item->xml_member
915 ? pivot_table_open_legacy (item)
916 : pivot_table_open_light (item));
920 msg (ME, "%s", error);
921 item->table = pivot_table_create_for_text (
922 pivot_value_new_text (N_("Error")),
923 pivot_value_new_user_text (error, -1));
931 /* Constructs a new spv_item from XML and stores it in *ITEMP. Returns NULL if
932 successful, otherwise an error message for the caller to use and free (with
935 XML should be a 'heading' or 'container' element. */
936 static char * WARN_UNUSED_RESULT
937 spv_decode_container (const struct spvsx_container *c,
938 const char *structure_member,
939 struct spv_item *parent)
941 struct spv_item *item = xzalloc (sizeof *item);
942 item->spv = parent->spv;
943 item->label = xstrdup (c->label->text);
944 item->visible = c->visibility == SPVSX_VISIBILITY_VISIBLE;
945 item->structure_member = xstrdup (structure_member);
947 assert (c->n_seq == 1);
948 struct spvxml_node *content = c->seq[0];
949 if (spvsx_is_container_text (content))
950 decode_container_text (spvsx_cast_container_text (content), item);
951 else if (spvsx_is_table (content))
953 item->type = SPV_ITEM_TABLE;
955 struct spvsx_table *table = spvsx_cast_table (content);
956 const struct spvsx_table_structure *ts = table->table_structure;
957 item->bin_member = xstrdup (ts->data_path->text);
958 item->command_id = xstrdup_if_nonempty (table->command_name);
959 item->subtype = xstrdup_if_nonempty (table->sub_type);
962 item->xml_member = ts->path ? xstrdup (ts->path->text) : NULL;
963 char *error = (table->table_properties
964 ? spv_table_look_decode (table->table_properties,
966 : xstrdup ("Legacy table lacks tableProperties"));
969 spv_item_destroy (item);
974 else if (spvsx_is_graph (content))
976 struct spvsx_graph *graph = spvsx_cast_graph (content);
977 item->type = SPV_ITEM_GRAPH;
978 item->command_id = xstrdup_if_nonempty (graph->command_name);
981 else if (spvsx_is_model (content))
983 struct spvsx_model *model = spvsx_cast_model (content);
984 item->type = SPV_ITEM_MODEL;
985 item->command_id = xstrdup_if_nonempty (model->command_name);
988 else if (spvsx_is_object (content))
990 struct spvsx_object *object = spvsx_cast_object (content);
991 item->type = SPV_ITEM_IMAGE;
992 item->png_member = xstrdup (object->uri);
994 else if (spvsx_is_image (content))
996 struct spvsx_image *image = spvsx_cast_image (content);
997 item->type = SPV_ITEM_IMAGE;
998 item->png_member = xstrdup (image->data_path->text);
1000 else if (spvsx_is_tree (content))
1001 item->type = SPV_ITEM_TREE;
1005 spv_heading_add_child (parent, item);
1009 static char * WARN_UNUSED_RESULT
1010 spv_decode_children (struct spv_reader *spv, const char *structure_member,
1011 struct spvxml_node **seq, size_t n_seq,
1012 struct spv_item *parent)
1014 for (size_t i = 0; i < n_seq; i++)
1016 const struct spvxml_node *node = seq[i];
1019 if (spvsx_is_container (node))
1021 const struct spvsx_container *container
1022 = spvsx_cast_container (node);
1023 error = spv_decode_container (container, structure_member, parent);
1025 else if (spvsx_is_heading (node))
1027 const struct spvsx_heading *subheading = spvsx_cast_heading (node);
1028 struct spv_item *subitem = xzalloc (sizeof *subitem);
1029 subitem->structure_member = xstrdup (structure_member);
1030 subitem->spv = parent->spv;
1031 subitem->type = SPV_ITEM_HEADING;
1032 subitem->label = xstrdup (subheading->label->text);
1033 if (subheading->command_name)
1034 subitem->command_id = xstrdup (subheading->command_name);
1035 subitem->visible = !subheading->heading_visibility_present;
1036 spv_heading_add_child (parent, subitem);
1038 error = spv_decode_children (spv, structure_member,
1039 subheading->seq, subheading->n_seq,
1052 static struct page_setup *
1053 decode_page_setup (const struct spvsx_page_setup *in, const char *file_name)
1055 struct page_setup *out = xmalloc (sizeof *out);
1056 *out = (struct page_setup) PAGE_SETUP_INITIALIZER;
1058 out->initial_page_number = in->initial_page_number;
1060 if (in->paper_width != DBL_MAX)
1061 out->paper[TABLE_HORZ] = in->paper_width;
1062 if (in->paper_height != DBL_MAX)
1063 out->paper[TABLE_VERT] = in->paper_height;
1065 if (in->margin_left != DBL_MAX)
1066 out->margins[TABLE_HORZ][0] = in->margin_left;
1067 if (in->margin_right != DBL_MAX)
1068 out->margins[TABLE_HORZ][1] = in->margin_right;
1069 if (in->margin_top != DBL_MAX)
1070 out->margins[TABLE_VERT][0] = in->margin_top;
1071 if (in->margin_bottom != DBL_MAX)
1072 out->margins[TABLE_VERT][1] = in->margin_bottom;
1074 if (in->space_after != DBL_MAX)
1075 out->object_spacing = in->space_after;
1078 out->chart_size = (in->chart_size == SPVSX_CHART_SIZE_FULL_HEIGHT
1079 ? PAGE_CHART_FULL_HEIGHT
1080 : in->chart_size == SPVSX_CHART_SIZE_HALF_HEIGHT
1081 ? PAGE_CHART_HALF_HEIGHT
1082 : in->chart_size == SPVSX_CHART_SIZE_QUARTER_HEIGHT
1083 ? PAGE_CHART_QUARTER_HEIGHT
1084 : PAGE_CHART_AS_IS);
1086 decode_page_paragraph (in->page_header->page_paragraph, &out->headings[0]);
1087 decode_page_paragraph (in->page_footer->page_paragraph, &out->headings[1]);
1089 out->file_name = xstrdup (file_name);
1094 static char * WARN_UNUSED_RESULT
1095 spv_heading_read (struct spv_reader *spv,
1096 const char *file_name, const char *member_name)
1099 char *error = spv_read_xml_member (spv, member_name, true, "heading", &doc);
1103 struct spvxml_context ctx = SPVXML_CONTEXT_INIT (ctx);
1104 struct spvsx_root_heading *root;
1105 spvsx_parse_root_heading (&ctx, xmlDocGetRootElement (doc), &root);
1106 error = spvxml_context_finish (&ctx, &root->node_);
1108 if (!error && root->page_setup)
1109 spv->page_setup = decode_page_setup (root->page_setup, file_name);
1111 for (size_t i = 0; !error && i < root->n_seq; i++)
1112 error = spv_decode_children (spv, member_name, root->seq, root->n_seq,
1117 char *s = xasprintf ("%s: %s", member_name, error);
1122 spvsx_free_root_heading (root);
1129 spv_get_root (const struct spv_reader *spv)
1135 spv_detect__ (struct zip_reader *zip, char **errorp)
1139 const char *member = "META-INF/MANIFEST.MF";
1140 if (!zip_reader_contains_member (zip, member))
1145 *errorp = zip_member_read_all (zip, "META-INF/MANIFEST.MF",
1150 const char *magic = "allowPivoting=true";
1151 bool is_spv = size == strlen (magic) && !memcmp (magic, data, size);
1157 /* Returns NULL if FILENAME is an SPV file, otherwise an error string that the
1158 caller must eventually free(). */
1159 char * WARN_UNUSED_RESULT
1160 spv_detect (const char *filename)
1162 struct string zip_error;
1163 struct zip_reader *zip = zip_reader_create (filename, &zip_error);
1165 return ds_steal_cstr (&zip_error);
1168 if (spv_detect__ (zip, &error) <= 0 && !error)
1169 error = xasprintf("%s: not an SPV file", filename);
1170 zip_reader_destroy (zip);
1171 ds_destroy (&zip_error);
1175 char * WARN_UNUSED_RESULT
1176 spv_open (const char *filename, struct spv_reader **spvp)
1180 struct spv_reader *spv = xzalloc (sizeof *spv);
1181 ds_init_empty (&spv->zip_errs);
1182 spv->zip = zip_reader_create (filename, &spv->zip_errs);
1185 char *error = ds_steal_cstr (&spv->zip_errs);
1191 int detect = spv_detect__ (spv->zip, &error);
1195 return error ? error : xasprintf("%s: not an SPV file", filename);
1198 spv->root = xzalloc (sizeof *spv->root);
1199 spv->root->spv = spv;
1200 spv->root->type = SPV_ITEM_HEADING;
1201 for (size_t i = 0; ; i++)
1203 const char *member_name = zip_reader_get_member_name (spv->zip, i);
1207 struct substring member_name_ss = ss_cstr (member_name);
1208 if (ss_starts_with (member_name_ss, ss_cstr ("outputViewer"))
1209 && ss_ends_with (member_name_ss, ss_cstr (".xml")))
1211 char *error = spv_heading_read (spv, filename, member_name);
1225 spv_close (struct spv_reader *spv)
1229 ds_destroy (&spv->zip_errs);
1230 zip_reader_destroy (spv->zip);
1231 spv_item_destroy (spv->root);
1232 page_setup_destroy (spv->page_setup);
1238 spv_item_set_table_look (struct spv_item *item,
1239 const struct pivot_table_look *look)
1241 /* If this is a table, install the table look in it.
1243 (We can't just set item->table_look because light tables ignore it and
1244 legacy tables sometimes override it.) */
1245 if (spv_item_is_table (item))
1247 spv_item_load (item);
1248 pivot_table_set_look (item->table, look);
1251 for (size_t i = 0; i < item->n_children; i++)
1252 spv_item_set_table_look (item->children[i], look);
1255 char * WARN_UNUSED_RESULT
1256 spv_decode_fmt_spec (uint32_t u32, struct fmt_spec *out)
1259 || (u32 == 0x10000 || u32 == 1 /* both used as string formats */))
1261 *out = fmt_for_output (FMT_F, 40, 2);
1265 uint8_t raw_type = u32 >> 16;
1266 uint8_t w = u32 >> 8;
1270 *out = (struct fmt_spec) { .type = FMT_F, .w = w, .d = d };
1271 bool ok = raw_type >= 40 || fmt_from_io (raw_type, &out->type);
1274 fmt_fix_output (out);
1275 ok = fmt_check_width_compat (out, 0);
1281 *out = fmt_for_output (FMT_F, 40, 2);
1282 return xasprintf ("bad format %#"PRIx32, u32);