Debian Codegraph @

texk/psutils/paper-size.c

raw
1 /* paper-size.c: TeX Live specific definition of paper_size()
2
3 Copyright 2014 Peter Breitenlohner.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this library; if not, see <http://www.gnu.org/licenses/>. */
17
18 #include <config.h>
19 #include <psutil.h>
20 #include <paper.h>
21
22 #define CONFIG_NAME "paper.cfg"
23 #define MAX_PAPER 16 /* sufficient for "halfexecutive" */
24 static char default_paper[MAX_PAPER];
25
26 static void
27 do_init (void)
28 {
29 static int inited = 0;
30 int ch, i = 0;
31 char *fqpn;
32 FILE *fp = NULL;
33
34 if (inited)
35 return;
36
37 paperinit ();
38
39 fqpn = kpse_find_file (CONFIG_NAME, kpse_program_text_format, false);
40 if (fqpn)
41 fp = fopen (fqpn, "r");
42 if (!fp)
43 die ("can't open config file %s", CONFIG_NAME);
44
45 /* In order to specify PAPER as default papersize the config file must
46 contain a line 'p PAPER'. */
47 ch = fgetc (fp);
48 /* skip comment lines */
49 while (ch > 0 && ch != 'p') {
50 while (ch > 0 && ch != '\n' && ch != '\r')
51 ch = fgetc (fp);
52 while (ch == '\n' || ch == '\r')
53 ch = fgetc (fp);
54 }
55 /* read default papersize */
56 if (ch == 'p' && fgetc (fp) == ' ')
57 while (i < MAX_PAPER - 1 &&
58 (ch = fgetc (fp)) > 0 && ch != ' ' && ch != '\n' && ch != '\r')
59 default_paper[i++] = ch;
60 default_paper[i] = 0;
61 if (i == 0)
62 die ("can't read default papersize from file %s", fqpn);
63
64 fclose (fp);
65 free (fqpn);
66 inited = 1;
67 }
68
69 int
70 paper_size (const char *paper_name, double *width, double *height)
71 {
72 const struct paper *pi;
73
74 do_init ();
75
76 if (!(pi = paperinfo (paper_name ? paper_name : default_paper)))
77 return 0;
78
79 *width = paperpswidth (pi);
80 *height = paperpsheight (pi);
81 return 1;
82 }