Merge 'master' into 'psppsheet'.
[pspp] / src / output / measure.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2007, 2009, 2010, 2011 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 #include <config.h>
18
19 #include "output/measure.h"
20
21 #include <gl/c-strtod.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #if HAVE_LC_PAPER
25 #include <langinfo.h>
26 #endif
27 #include <stdlib.h>
28
29 #include "data/file-name.h"
30 #include "libpspp/str.h"
31
32 #include "gl/error.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 static double parse_unit (const char *);
38 static bool parse_paper_size (const char *, int *h, int *v);
39 static bool get_standard_paper_size (struct substring name, int *h, int *v);
40 static bool read_paper_conf (const char *file_name, int *h, int *v);
41 static bool get_default_paper_size (int *h, int *v);
42
43 /* Determines the size of a dimensional measurement and returns
44    the size in units of 1/72000".  Units are assumed to be
45    millimeters unless otherwise specified.  Returns -1 on
46    error. */
47 int
48 measure_dimension (const char *dimen)
49 {
50   double raw, factor;
51   char *tail;
52
53   /* Number. */
54   raw = c_strtod (dimen, &tail);
55   if (raw < 0.0)
56     goto syntax_error;
57
58   /* Unit. */
59   factor = parse_unit (tail);
60   if (factor == 0.0)
61     goto syntax_error;
62
63   return raw * factor;
64
65 syntax_error:
66   error (0, 0, _("`%s' is not a valid length."), dimen);
67   return -1;
68 }
69
70 /* Stores the dimensions, in 1/72000" units, of paper identified
71    by SIZE into *H and *V.  SIZE can be the name of a kind of
72    paper ("a4", "letter", ...) or a pair of dimensions
73    ("210x297", "8.5x11in", ...).  Returns true on success, false
74    on failure.  On failure, *H and *V are set for A4 paper. */
75 bool
76 measure_paper (const char *size, int *h, int *v)
77 {
78   struct substring s;
79   bool ok;
80
81   s = ss_cstr (size);
82   ss_trim (&s, ss_cstr (CC_SPACES));
83
84   if (ss_is_empty (s))
85     {
86       /* Treat empty string as default paper size. */
87       ok = get_default_paper_size (h, v);
88     }
89   else if (isdigit (ss_first (s)))
90     {
91       /* Treat string that starts with digit as explicit size. */
92       ok = parse_paper_size (size, h, v);
93       if (!ok)
94         error (0, 0, _("syntax error in paper size `%s'"), size);
95     }
96   else
97     {
98       /* Check against standard paper sizes. */
99       ok = get_standard_paper_size (s, h, v);
100     }
101
102   /* Default to A4 on error. */
103   if (!ok)
104     {
105       *h = 210 * (72000 / 25.4);
106       *v = 297 * (72000 / 25.4);
107     }
108   return ok;
109 }
110 \f
111 /* Parses UNIT as a dimensional unit.  Returns the multiplicative
112    factor needed to change a quantity measured in that unit into
113    1/72000" units.  If UNIT is empty, it is treated as
114    millimeters.  If the unit is unrecognized, returns 0. */
115 static double
116 parse_unit (const char *unit)
117 {
118   struct unit
119     {
120       char name[3];
121       double factor;
122     };
123
124   static const struct unit units[] =
125     {
126       {"pt", 72000 / 72},
127       {"pc", 72000 / 72 * 12.0},
128       {"in", 72000},
129       {"cm", 72000 / 2.54},
130       {"mm", 72000 / 25.4},
131       {"", 72000 / 25.4},
132     };
133
134   const struct unit *p;
135
136   unit += strspn (unit, CC_SPACES);
137   for (p = units; p < units + sizeof units / sizeof *units; p++)
138     if (!strcasecmp (unit, p->name))
139       return p->factor;
140   return 0.0;
141 }
142
143 /* Stores the dimensions in 1/72000" units of paper identified by
144    SIZE, which is of form `HORZ x VERT [UNIT]' where HORZ and
145    VERT are numbers and UNIT is an optional unit of measurement,
146    into *H and *V.  Return true on success. */
147 static bool
148 parse_paper_size (const char *size, int *h, int *v)
149 {
150   double raw_h, raw_v, factor;
151   char *tail;
152
153   /* Width. */
154   raw_h = c_strtod (size, &tail);
155   if (raw_h <= 0.0)
156     return false;
157
158   /* Delimiter. */
159   tail += strspn (tail, CC_SPACES "x,");
160
161   /* Length. */
162   raw_v = c_strtod (tail, &tail);
163   if (raw_v <= 0.0)
164     return false;
165
166   /* Unit. */
167   factor = parse_unit (tail);
168   if (factor == 0.0)
169     return false;
170
171   *h = raw_h * factor + .5;
172   *v = raw_v * factor + .5;
173   return true;
174 }
175
176 static bool
177 get_standard_paper_size (struct substring name, int *h, int *v)
178 {
179   static const char *sizes[][2] =
180     {
181       {"a0", "841 x 1189 mm"},
182       {"a1", "594 x 841 mm"},
183       {"a2", "420 x 594 mm"},
184       {"a3", "297 x 420 mm"},
185       {"a4", "210 x 297 mm"},
186       {"a5", "148 x 210 mm"},
187       {"b5", "176 x 250 mm"},
188       {"a6", "105 x 148 mm"},
189       {"a7", "74 x 105 mm"},
190       {"a8", "52 x 74 mm"},
191       {"a9", "37 x 52 mm"},
192       {"a10", "26 x 37 mm"},
193       {"b0", "1000 x 1414 mm"},
194       {"b1", "707 x 1000 mm"},
195       {"b2", "500 x 707 mm"},
196       {"b3", "353 x 500 mm"},
197       {"b4", "250 x 353 mm"},
198       {"letter", "612 x 792 pt"},
199       {"legal", "612 x 1008 pt"},
200       {"executive", "522 x 756 pt"},
201       {"note", "612 x 792 pt"},
202       {"11x17", "792 x 1224 pt"},
203       {"tabloid", "792 x 1224 pt"},
204       {"statement", "396 x 612 pt"},
205       {"halfletter", "396 x 612 pt"},
206       {"halfexecutive", "378 x 522 pt"},
207       {"folio", "612 x 936 pt"},
208       {"quarto", "610 x 780 pt"},
209       {"ledger", "1224 x 792 pt"},
210       {"archA", "648 x 864 pt"},
211       {"archB", "864 x 1296 pt"},
212       {"archC", "1296 x 1728 pt"},
213       {"archD", "1728 x 2592 pt"},
214       {"archE", "2592 x 3456 pt"},
215       {"flsa", "612 x 936 pt"},
216       {"flse", "612 x 936 pt"},
217       {"csheet", "1224 x 1584 pt"},
218       {"dsheet", "1584 x 2448 pt"},
219       {"esheet", "2448 x 3168 pt"},
220     };
221
222   size_t i;
223
224   for (i = 0; i < sizeof sizes / sizeof *sizes; i++)
225     if (ss_equals_case (ss_cstr (sizes[i][0]), name))
226       {
227         bool ok = parse_paper_size (sizes[i][1], h, v);
228         assert (ok);
229         return ok;
230       }
231   error (0, 0, _("unknown paper type `%.*s'"),
232          (int) ss_length (name), ss_data (name));
233   return false;
234 }
235
236 /* Reads file FILE_NAME to find a paper size.  Stores the
237    dimensions, in 1/72000" units, into *H and *V.  Returns true
238    on success, false on failure. */
239 static bool
240 read_paper_conf (const char *file_name, int *h, int *v)
241 {
242   struct string line = DS_EMPTY_INITIALIZER;
243   int line_number = 0;
244   FILE *file;
245
246   file = fopen (file_name, "r");
247   if (file == NULL)
248     {
249       error (0, errno, _("error opening input file `%s'"), file_name);
250       return false;
251     }
252
253   for (;;)
254     {
255       struct substring name;
256
257       if (!ds_read_config_line (&line, &line_number, file))
258         {
259           if (ferror (file))
260             error (0, errno, _("error reading file `%s'"), file_name);
261           break;
262         }
263
264       name = ds_ss (&line);
265       ss_trim (&name, ss_cstr (CC_SPACES));
266       if (!ss_is_empty (name))
267         {
268           bool ok = get_standard_paper_size (name, h, v);
269           fclose (file);
270           ds_destroy (&line);
271           return ok;
272         }
273     }
274
275   fclose (file);
276   ds_destroy (&line);
277   error (0, 0, _("paper size file `%s' does not state a paper size"),
278          file_name);
279   return false;
280 }
281
282 /* The user didn't specify a paper size, so let's choose a
283    default based on his environment.  Stores the
284    dimensions, in 1/72000" units, into *H and *V.  Returns true
285    on success, false on failure. */
286 static bool
287 get_default_paper_size (int *h, int *v)
288 {
289   /* libpaper in Debian (and other distributions?) allows the
290      paper size to be specified in $PAPERSIZE or in a file
291      specified in $PAPERCONF. */
292   if (getenv ("PAPERSIZE") != NULL)
293     return get_standard_paper_size (ss_cstr (getenv ("PAPERSIZE")), h, v);
294   if (getenv ("PAPERCONF") != NULL)
295     return read_paper_conf (getenv ("PAPERCONF"), h, v);
296
297 #if HAVE_LC_PAPER
298   /* LC_PAPER is a non-standard glibc extension. */
299   *h = (int) nl_langinfo(_NL_PAPER_WIDTH) * (72000 / 25.4);
300   *v = (int) nl_langinfo(_NL_PAPER_HEIGHT) * (72000 / 25.4);
301   if (*h > 0 && *v > 0)
302      return true;
303 #endif
304
305   /* libpaper defaults to /etc/papersize. */
306   if (fn_exists ("/etc/papersize"))
307     return read_paper_conf ("/etc/papersize", h, v);
308
309   /* Can't find a default. */
310   return false;
311 }
312