5075fa9e40fc85b122e75da580944d69ac858767
[pspp] / src / output / table.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997, 1998, 1999, 2000, 2009, 2013, 2014 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_H
18 #define OUTPUT_TABLE_H 1
19
20 /* Tables.
21
22 .  A table is a rectangular grid of cells.  Cells can be joined to form larger
23    cells.  Rows and columns can be separated by rules of various types.  Rows
24    at the top and bottom of a table and columns at the left and right edges of
25    a table can be designated as headers, which means that if the table must be
26    broken across more than one page, those rows or columns are repeated on each
27    page.
28
29    A table is not itself an output_item, and thus a table cannot by itself be
30    used for output, but they can be embedded inside struct table_item (see
31    table-item.h) for that purpose. */
32
33 #include <stdbool.h>
34 #include <stdint.h>
35 #include <stddef.h>
36 #include "libpspp/compiler.h"
37
38 struct casereader;
39 struct fmt_spec;
40 struct pool;
41 struct table_item;
42 struct variable;
43
44 /* A table axis.
45
46    Many table-related declarations use 2-element arrays in place of "x" and "y"
47    variables.  This reduces code duplication significantly, because much table
48    code treats rows and columns the same way.
49
50    A lot of code that uses these enumerations assumes that the two values are 0
51    and 1, so don't change them to other values. */
52 enum table_axis
53   {
54     TABLE_HORZ,
55     TABLE_VERT
56 #define TABLE_N_AXES 2
57   };
58
59 struct cell_color
60   {
61     uint8_t alpha, r, g, b;
62   };
63
64 #define CELL_COLOR(r, g, b) { 255, r, g, b }
65 #define CELL_COLOR_BLACK CELL_COLOR (0, 0, 0)
66 #define CELL_COLOR_WHITE CELL_COLOR (255, 255, 255)
67
68 static inline bool
69 cell_color_equal (const struct cell_color *a, const struct cell_color *b)
70 {
71   return a->alpha == b->alpha && a->r == b->r && a->g == b->g && a->b == b->b;
72 }
73
74 void cell_color_dump (const struct cell_color *);
75
76 enum table_stroke
77   {
78     TABLE_STROKE_NONE,
79     TABLE_STROKE_SOLID,
80     TABLE_STROKE_DASHED,
81     TABLE_STROKE_THICK,
82     TABLE_STROKE_THIN,
83     TABLE_STROKE_DOUBLE,
84     TABLE_N_STROKES,
85   };
86
87 const char *table_stroke_to_string (enum table_stroke);
88
89 struct table_border_style
90   {
91     enum table_stroke stroke;
92     struct cell_color color;
93   };
94
95 #define TABLE_BORDER_STYLE_INITIALIZER { TABLE_STROKE_SOLID, CELL_COLOR_BLACK }
96
97 enum table_halign
98   {
99     TABLE_HALIGN_RIGHT,
100     TABLE_HALIGN_LEFT,
101     TABLE_HALIGN_CENTER,
102     TABLE_HALIGN_MIXED,
103     TABLE_HALIGN_DECIMAL
104   };
105
106 const char *table_halign_to_string (enum table_halign);
107
108 enum table_valign
109   {
110     TABLE_VALIGN_TOP,
111     TABLE_VALIGN_CENTER,
112     TABLE_VALIGN_BOTTOM,
113   };
114
115 const char *table_valign_to_string (enum table_valign);
116
117 struct cell_style
118   {
119     enum table_halign halign;
120     enum table_valign valign;
121     double decimal_offset;       /* In 1/96" units. */
122     char decimal_char;           /* Either '.' or ','. */
123     int margin[TABLE_N_AXES][2]; /* In 1/96" units. */
124   };
125
126 #define CELL_STYLE_INITIALIZER { CELL_STYLE_INITIALIZER__ }
127 #define CELL_STYLE_INITIALIZER__                                \
128         .margin = { [TABLE_HORZ][0] = 8, [TABLE_HORZ][1] = 11,  \
129                     [TABLE_VERT][0] = 1, [TABLE_VERT][1] = 1 }
130
131 void cell_style_dump (const struct cell_style *);
132
133 struct font_style
134   {
135     bool bold, italic, underline, markup;
136     struct cell_color fg[2], bg[2];
137     char *typeface;
138     int size;
139   };
140
141 #define FONT_STYLE_INITIALIZER { FONT_STYLE_INITIALIZER__ }
142 #define FONT_STYLE_INITIALIZER__                                        \
143         .fg = { [0] = CELL_COLOR_BLACK, [1] = CELL_COLOR_BLACK},        \
144         .bg = { [0] = CELL_COLOR_WHITE, [1] = CELL_COLOR_WHITE},
145
146 void font_style_copy (struct font_style *, const struct font_style *);
147 void font_style_uninit (struct font_style *);
148 void font_style_dump (const struct font_style *);
149
150 struct area_style
151   {
152     struct cell_style cell_style;
153     struct font_style font_style;
154   };
155
156 #define AREA_STYLE_INITIALIZER { AREA_STYLE_INITIALIZER__ }
157 #define AREA_STYLE_INITIALIZER__                \
158        .cell_style = CELL_STYLE_INITIALIZER,    \
159        .font_style = FONT_STYLE_INITIALIZER
160
161 struct area_style *area_style_clone (struct pool *, const struct area_style *);
162 void area_style_copy (struct area_style *, const struct area_style *);
163 void area_style_uninit (struct area_style *);
164 void area_style_free (struct area_style *);
165
166 /* Properties of a table cell. */
167 enum
168   {
169     TAB_NONE = 0,
170     TAB_FIX        = 1 << 1,    /* Use fixed font. */
171     TAB_MARKUP     = 1 << 2,    /* Text contains Pango markup. */
172     TAB_NUMERIC    = 1 << 3,    /* Cell contents are numeric. */
173     TAB_ROTATE     = 1 << 4,    /* Rotate cell contents 90 degrees. */
174
175     TAB_STYLE_SHIFT = 5,
176     TAB_STYLE_MASK = 7 << TAB_STYLE_SHIFT,
177
178     /* Horizontal alignment of cell contents. */
179     TAB_RIGHT      = 0 << 10,
180     TAB_LEFT       = 1 << 10,
181     TAB_CENTER     = 2 << 10,
182     TAB_HALIGN     = 3 << 10, /* Alignment mask. */
183
184     /* Vertical alignment of cell contents. */
185     TAB_TOP        = 0 << 12,
186     TAB_MIDDLE     = 1 << 12,
187     TAB_BOTTOM     = 2 << 12,
188     TAB_VALIGN     = 3 << 12, /* Alignment mask. */
189
190     /* Internal use by tab.c only. */
191     TAB_JOIN = 1 << 14,
192   };
193
194 /* Styles for the rules around table cells. */
195 enum
196   {
197     TAL_NONE = TABLE_STROKE_NONE,
198 #define TAL_0 TAL_NONE
199     TAL_SOLID = TABLE_STROKE_SOLID,
200 #define TAL_1 TAL_SOLID
201     TAL_DASHED = TABLE_STROKE_DASHED,
202     TAL_THICK = TABLE_STROKE_THICK,
203     TAL_THIN = TABLE_STROKE_THIN,
204     TAL_DOUBLE = TABLE_STROKE_DOUBLE,
205 #define TAL_2 TAL_DOUBLE
206   };
207
208 /* Given line styles A and B (each one of the TAL_* enumeration constants
209    above), returns a line style that "combines" them, that is, that gives a
210    reasonable line style choice for a rule for different reasons should have
211    both styles A and B. */
212 static inline int table_rule_combine (int a, int b)
213 {
214   return a > b ? a : b;
215 }
216
217 /* A table. */
218 struct table
219   {
220     struct pool *container;
221
222     /* Table size.
223
224        n[TABLE_HORZ]: Number of columns.
225        n[TABLE_VERT]: Number of rows. */
226     int n[TABLE_N_AXES];
227
228     /* Table headers.
229
230        Rows at the top and bottom of a table and columns at the left and right
231        edges of a table can be designated as headers.  If the table must be
232        broken across more than one page for output, headers rows and columns
233        are repeated on each page.
234
235        h[TABLE_HORZ][0]: Left header columns.
236        h[TABLE_HORZ][1]: Right header columns.
237        h[TABLE_VERT][0]: Top header rows.
238        h[TABLE_VERT][1]: Bottom header rows. */
239     int h[TABLE_N_AXES][2];
240
241     /* Reference count.  A table may be shared between multiple owners,
242        indicated by a reference count greater than 1.  When this is the case,
243        the table must not be modified. */
244     int ref_cnt;
245
246     /* Table contents.
247
248        Each array element in cc[] is ordinarily a "char *" pointer to a
249        string.  If TAB_JOIN (defined in tab.c) is set in ct[] for the element,
250        however, it is a joined cell and the corresponding element of cc[]
251        points to a struct tab_joined_cell. */
252     void **cc;                  /* Cell contents; void *[nr][nc]. */
253     unsigned short *ct;         /* Cell types; unsigned short[nr][nc]. */
254     struct area_style *styles[8];
255
256     /* Rules. */
257     unsigned char *rh;          /* Horiz rules; unsigned char[nr+1][nc]. */
258     unsigned char *rv;          /* Vert rules; unsigned char[nr][nc+1]. */
259     struct cell_color *rule_colors[32];
260   };
261
262 /* Reference counting. */
263 struct table *table_ref (const struct table *);
264 void table_unref (struct table *);
265 bool table_is_shared (const struct table *);
266
267 /* Returns the number of columns or rows, respectively, in T. */
268 static inline int table_nc (const struct table *t)
269         { return t->n[TABLE_HORZ]; }
270 static inline int table_nr (const struct table *t)
271         { return t->n[TABLE_VERT]; }
272
273 /* Returns the number of left, right, top, or bottom headers, respectively, in
274    T.  */
275 static inline int table_hl (const struct table *t)
276         { return t->h[TABLE_HORZ][0]; }
277 static inline int table_hr (const struct table *t)
278         { return t->h[TABLE_HORZ][1]; }
279 static inline int table_ht (const struct table *t)
280         { return t->h[TABLE_VERT][0]; }
281 static inline int table_hb (const struct table *t)
282         { return t->h[TABLE_VERT][1]; }
283
284 /* Simple kinds of output. */
285 struct table *table_from_string (const char *);
286 void table_output_text (int options, const char *string);
287 void table_output_text_format (int options, const char *, ...)
288   PRINTF_FORMAT (2, 3);
289
290 /* Rule masks. */
291 #define TAB_RULE_TYPE_MASK   7
292 #define TAB_RULE_TYPE_SHIFT  0
293 #define TAB_RULE_STYLE_MASK  (31 << TAB_RULE_STYLE_SHIFT)
294 #define TAB_RULE_STYLE_SHIFT 3
295
296 /* Tables. */
297 struct table *table_create (int nc, int nr, int hl, int hr, int ht, int hb);
298
299 /* Rules. */
300 void table_hline (struct table *, int style, int x1, int x2, int y);
301 void table_vline (struct table *, int style, int x, int y1, int y2);
302 void table_box (struct table *, int f_h, int f_v, int i_h, int i_v,
303                 int x1, int y1, int x2, int y2);
304
305 /* Cells. */
306 void table_text (struct table *, int c, int r, unsigned opt, const char *);
307 void table_text_format (struct table *, int c, int r, unsigned opt,
308                         const char *, ...)
309   PRINTF_FORMAT (5, 6);
310
311 void table_joint_text (struct table *, int x1, int y1, int x2, int y2,
312                        unsigned opt, const char *);
313
314 struct footnote *table_create_footnote (struct table *, size_t idx,
315                                         const char *content,
316                                         const char *marker,
317                                         struct area_style *);
318 void table_add_footnote (struct table *, int x, int y,
319                          const struct footnote *);
320
321 void table_add_style (struct table *, int x, int y,
322                       const struct area_style *);
323
324 bool table_cell_is_empty (const struct table *, int c, int r);
325
326 #endif /* output/table.h */