5dbf99b15ad3e6303357a061fd7adc390932eee3
[pspp] / src / output / table-provider.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997, 1998, 1999, 2000, 2009, 2011, 2013, 2014, 2018 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_TABLE_PROVIDER
18 #define OUTPUT_TABLE_PROVIDER 1
19
20 #include "output/table.h"
21
22 /* An item of contents within a table cell. */
23 struct cell_contents
24   {
25     unsigned int options;       /* TAB_*. */
26     char *text;                 /* A paragraph of text. */
27
28     /* Optional footnote(s). */
29     char **footnotes;
30     size_t n_footnotes;
31   };
32
33 /* A cell in a table. */
34 struct table_cell
35   {
36     /* Occupied table region.
37
38        d[TABLE_HORZ][0] is the leftmost column.
39        d[TABLE_HORZ][1] is the rightmost column, plus 1.
40        d[TABLE_VERT][0] is the top row.
41        d[TABLE_VERT][1] is the bottom row, plus 1.
42
43        For an ordinary cell:
44            d[TABLE_HORZ][1] == d[TABLE_HORZ][0] + 1
45        and d[TABLE_VERT][1] == d[TABLE_VERT][0] + 1
46
47        For a joined cell:
48           d[TABLE_HORZ][1] > d[TABLE_HORZ][0] + 1
49        or d[TABLE_VERT][1] > d[TABLE_VERT][0] + 1
50        or both. */
51     int d[TABLE_N_AXES][2];
52
53     /* The cell's contents.
54
55        Most table cells contain only one item (a paragraph of text), but cells
56        are allowed to be empty (n_contents == 0) or contain a nested table, or
57        multiple items.
58
59        'inline_contents' provides a place to store a single item to handle the
60        common case.
61     */
62     const struct cell_contents *contents;
63     size_t n_contents;
64     struct cell_contents inline_contents;
65
66     /* Called to free the cell's data, if nonnull. */
67     void (*destructor) (void *destructor_aux);
68     void *destructor_aux;
69   };
70
71 void table_cell_free (struct table_cell *);
72
73 /* Returns the number of columns that CELL spans.  This is 1 for an ordinary
74    cell and greater than one for a cell that joins multiple columns. */
75 static inline int
76 table_cell_colspan (const struct table_cell *cell)
77 {
78   return cell->d[TABLE_HORZ][1] - cell->d[TABLE_HORZ][0];
79 }
80
81 /* Returns the number of rows that CELL spans.  This is 1 for an ordinary cell
82    and greater than one for a cell that joins multiple rows. */
83 static inline int
84 table_cell_rowspan (const struct table_cell *cell)
85 {
86   return cell->d[TABLE_VERT][1] - cell->d[TABLE_VERT][0];
87 }
88
89 /* Returns true if CELL is a joined cell, that is, if it spans multiple rows
90    or columns.  Otherwise, returns false. */
91 static inline bool
92 table_cell_is_joined (const struct table_cell *cell)
93 {
94   return table_cell_colspan (cell) > 1 || table_cell_rowspan (cell) > 1;
95 }
96 \f
97 /* Declarations to allow defining table classes. */
98
99 struct table_class
100   {
101     /* Frees TABLE.
102
103        The table class may assume that any cells that were retrieved by calling
104        the 'get_cell' function have been freed (by calling their destructors)
105        before this function is called. */
106     void (*destroy) (struct table *table);
107
108     /* Initializes CELL with the contents of the table cell at column X and row
109        Y within TABLE.  All members of CELL must be initialized, except that if
110        'destructor' is set to a null pointer, then 'destructor_aux' need not be
111        initialized.  The 'contents' member of CELL must be set to a nonnull
112        value.
113
114        The table class must allow any number of cells in the table to be
115        retrieved simultaneously; that is, TABLE must not assume that a given
116        cell will be freed before another one is retrieved using 'get_cell'.
117
118        The table class must allow joined cells to be retrieved, with identical
119        contents, using any (X,Y) location inside the cell.
120
121        The table class must not allow cells to overlap.
122
123        The table class should not allow a joined cell to cross the border
124        between header rows/columns and the interior of the table.  That is, a
125        joined cell should be entirely within headers rows and columns or
126        entirely outside them.
127
128        The table class may assume that CELL will be freed before TABLE is
129        destroyed. */
130     void (*get_cell) (const struct table *table, int x, int y,
131                       struct table_cell *cell);
132
133     /* Returns one of the TAL_* enumeration constants (declared in
134        output/table.h) representing a rule running alongside one of the cells
135        in TABLE.
136
137        See table_get_rule() in table.c for a detailed explanation of the
138        meaning of AXIS and X and Y, including a diagram. */
139     int (*get_rule) (const struct table *table,
140                      enum table_axis axis, int x, int y);
141
142     /* This function is optional and most table classes will not implement it.
143
144        If provided, this function must take ownership of A and B and return a
145        table that consists of tables A and B "pasted together", that is, a
146        table whose size is the sum of the sizes of A and B along the axis
147        specified by ORIENTATION.  A and B will ordinarily have the same size
148        along the axis opposite ORIENTATION; no particular handling of tables
149        that have different sizes along that axis is required.
150
151        The handling of rules at the seam between A and B is not specified, but
152        table_rule_combine() is one reasonable way to do it.
153
154        Called only if neither A and B is shared (as returned by
155        table_is_shared()).
156
157        Called if A or B or both is of the class defined by this table class.
158        That is, the implementation must be prepared to deal with the case where
159        A or B is not the ordinarily expected table class.
160
161        This function may return a null pointer if it cannot implement the paste
162        operation, in which case the caller will use a fallback
163        implementation.
164
165        This function is used to implement table_paste(). */
166     struct table *(*paste) (struct table *a, struct table *b,
167                             enum table_axis orientation);
168
169     /* This function is optional and most table classes will not implement it.
170
171        If provided, this function must take ownership of TABLE and return a new
172        table whose contents are the TABLE's rows RECT[TABLE_VERT][0] through
173        RECT[TABLE_VERT][1], exclusive, and the TABLE's columns
174        RECT[TABLE_HORZ][0] through RECT[TABLE_HORZ][1].
175
176        Called only if TABLE is not shared (as returned by table_is_shared()).
177
178        This function may return a null pointer if it cannot implement the
179        select operation, in which case the caller will use a fallback
180        implementation.
181
182        This function is used to implement table_select(). */
183     struct table *(*select) (struct table *table, int rect[TABLE_N_AXES][2]);
184   };
185
186 void table_init (struct table *, const struct table_class *);
187
188 /* Table class implementations can call these functions or just set the
189    table's n[] and h[][] members directly. */
190 void table_set_nc (struct table *, int nc);
191 void table_set_nr (struct table *, int nr);
192 \f
193 /* For use primarily by output drivers. */
194
195 void table_get_cell (const struct table *, int x, int y, struct table_cell *);
196 int table_get_rule (const struct table *, enum table_axis, int x, int y);
197
198 #endif /* output/table-provider.h */