Add support for PNG images in .spv files.
[pspp] / src / output / spv / spv.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2017 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 #ifndef OUTPUT_SPV_H
18 #define OUTPUT_SPV_H 1
19
20 /* SPSS Viewer (SPV) file reader.
21
22    An SPV file, represented as struct spv_reader, contains a number of
23    top-level headings, each of which recursively contains other headings and
24    tables.  Here, we model a heading, text, table, or other element as an
25    "item", and a an SPV file as a single root item that contains each of the
26    top-level headings as a child item.
27  */
28
29 #include <stdbool.h>
30 #include <stddef.h>
31 #include <stdint.h>
32
33 #ifdef HAVE_CAIRO
34 #include <cairo.h>
35 #endif
36
37 #include "libpspp/compiler.h"
38
39 struct fmt_spec;
40 struct pivot_table;
41 struct spv_data;
42 struct spv_reader;
43 struct spvlb_table;
44 struct string;
45 struct _xmlDoc;
46
47 /* SPV files. */
48
49 char *spv_open (const char *filename, struct spv_reader **) WARN_UNUSED_RESULT;
50 void spv_close (struct spv_reader *);
51
52 char *spv_detect (const char *filename) WARN_UNUSED_RESULT;
53
54 const char *spv_get_errors (const struct spv_reader *);
55 void spv_clear_errors (struct spv_reader *);
56
57 struct spv_item *spv_get_root (const struct spv_reader *);
58 void spv_item_dump (const struct spv_item *, int indentation);
59
60 const struct page_setup *spv_get_page_setup (const struct spv_reader *);
61
62 /* Items.
63
64    An spv_item represents of the elements that can occur in an SPV file.  Items
65    form a tree because "heading" items can have an arbitrary number of child
66    items, which in turn may also be headings.  The root item, that is, the item
67    returned by spv_get_root(), is always a heading. */
68
69 enum spv_item_type
70   {
71     SPV_ITEM_HEADING,
72     SPV_ITEM_TEXT,
73     SPV_ITEM_TABLE,
74     SPV_ITEM_GRAPH,
75     SPV_ITEM_MODEL,
76     SPV_ITEM_IMAGE,
77     SPV_ITEM_TREE,
78   };
79
80 const char *spv_item_type_to_string (enum spv_item_type);
81
82 #define SPV_CLASSES                                \
83     SPV_CLASS(CHARTS, "charts")                    \
84     SPV_CLASS(HEADINGS, "headings")                \
85     SPV_CLASS(LOGS, "logs")                        \
86     SPV_CLASS(MODELS, "models")                    \
87     SPV_CLASS(TABLES, "tables")                    \
88     SPV_CLASS(TEXTS, "texts")                      \
89     SPV_CLASS(TREES, "trees")                      \
90     SPV_CLASS(WARNINGS, "warnings")                \
91     SPV_CLASS(OUTLINEHEADERS, "outlineheaders")    \
92     SPV_CLASS(PAGETITLE, "pagetitle")              \
93     SPV_CLASS(NOTES, "notes")                      \
94     SPV_CLASS(UNKNOWN, "unknown")                  \
95     SPV_CLASS(OTHER, "other")
96 enum spv_item_class
97   {
98 #define SPV_CLASS(ENUM, NAME) SPV_CLASS_##ENUM,
99     SPV_CLASSES
100 #undef SPV_CLASS
101   };
102 enum
103   {
104 #define SPV_CLASS(ENUM, NAME) +1
105     SPV_N_CLASSES = SPV_CLASSES
106 #undef SPV_CLASS
107 };
108 #define SPV_ALL_CLASSES ((1u << SPV_N_CLASSES) - 1)
109
110 const char *spv_item_class_to_string (enum spv_item_class);
111 enum spv_item_class spv_item_class_from_string (const char *);
112
113 struct spv_item
114   {
115     struct spv_reader *spv;
116     struct spv_item *parent;
117     size_t parent_idx;         /* item->parent->children[parent_idx] == item */
118
119     bool error;
120
121     char *structure_member;
122
123     enum spv_item_type type;
124     char *label;                /* Localized label. */
125     char *command_id;           /* Non-localized unique command identifier. */
126
127     /* Whether the item is visible.
128        For SPV_ITEM_HEADING, false indicates that the item is collapsed.
129        For SPV_ITEM_TABLE, false indicates that the item is not shown. */
130     bool visible;
131
132     /* SPV_ITEM_HEADING only. */
133     struct spv_item **children;
134     size_t n_children, allocated_children;
135
136     /* SPV_ITEM_TABLE only. */
137     struct pivot_table *table;    /* NULL if not yet loaded. */
138     struct pivot_table_look *table_look;
139     char *bin_member;
140     char *xml_member;
141     char *subtype;
142
143     /* SPV_ITEM_TEXT only.  */
144     struct pivot_value *text;
145
146     /* SPV_ITEM_IMAGE only. */
147     char *png_member;
148 #ifdef HAVE_CAIRO
149     cairo_surface_t *image;
150 #endif
151   };
152
153 void spv_item_format_path (const struct spv_item *, struct string *);
154
155 void spv_item_load (const struct spv_item *);
156
157 enum spv_item_type spv_item_get_type (const struct spv_item *);
158 enum spv_item_class spv_item_get_class (const struct spv_item *);
159
160 const char *spv_item_get_label (const struct spv_item *);
161
162 bool spv_item_is_heading (const struct spv_item *);
163 size_t spv_item_get_n_children (const struct spv_item *);
164 struct spv_item *spv_item_get_child (const struct spv_item *, size_t idx);
165
166 bool spv_item_is_table (const struct spv_item *);
167 const struct pivot_table *spv_item_get_table (const struct spv_item *);
168
169 bool spv_item_is_text (const struct spv_item *);
170 const struct pivot_value *spv_item_get_text (const struct spv_item *);
171
172 bool spv_item_is_image (const struct spv_item *);
173 #ifdef HAVE_CAIRO
174 cairo_surface_t *spv_item_get_image (const struct spv_item *);
175 #endif
176
177 bool spv_item_is_visible (const struct spv_item *);
178
179 #define SPV_ITEM_FOR_EACH(ITER, ROOT) \
180   for ((ITER) = (ROOT); (ITER) != NULL; (ITER) = spv_item_next(ITER))
181 #define SPV_ITEM_FOR_EACH_SKIP_ROOT(ITER, ROOT) \
182   for ((ITER) = (ROOT); ((ITER) = spv_item_next(ITER)) != NULL;)
183 struct spv_item *spv_item_next (const struct spv_item *);
184
185 const struct spv_item *spv_item_get_parent (const struct spv_item *);
186 size_t spv_item_get_level (const struct spv_item *);
187
188 const char *spv_item_get_command_id (const struct spv_item *);
189 const char *spv_item_get_subtype (const struct spv_item *);
190
191 char *spv_item_get_structure (const struct spv_item *, struct _xmlDoc **)
192   WARN_UNUSED_RESULT;
193
194 bool spv_item_is_light_table (const struct spv_item *);
195 char *spv_item_get_light_table (const struct spv_item *,
196                                     struct spvlb_table **)
197   WARN_UNUSED_RESULT;
198 char *spv_item_get_raw_light_table (const struct spv_item *,
199                                     void **data, size_t *size)
200   WARN_UNUSED_RESULT;
201
202 bool spv_item_is_legacy_table (const struct spv_item *);
203 char *spv_item_get_raw_legacy_data (const struct spv_item *item,
204                                     void **data, size_t *size)
205   WARN_UNUSED_RESULT;
206 char *spv_item_get_legacy_data (const struct spv_item *, struct spv_data *)
207   WARN_UNUSED_RESULT;
208 char *spv_item_get_legacy_table (const struct spv_item *, struct _xmlDoc **)
209   WARN_UNUSED_RESULT;
210
211 void spv_item_set_table_look (struct spv_item *,
212                               const struct pivot_table_look *);
213
214 char *spv_decode_fmt_spec (uint32_t u32, struct fmt_spec *) WARN_UNUSED_RESULT;
215
216 #endif /* output/spv/spv.h */