1 /*
2  * Copyright (C) 2000, Matias Atria
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 2 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, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include <config.h>
20 #include <string.h>
21 
22 #include "common.h"
23 #include "mdvi.h"
24 #include "private.h"
25 
26 static const DviPaperSpec papers[] = {
27 	{"ISO", 0, 0},
28 	{"4A0", "1682mm", "2378mm"},
29 	{"2A0", "1189mm", "1682mm"},
30 	{"A0", "841mm", "1189mm"},
31 	{"A1", "594mm", "841mm"},
32 	{"A2", "420mm", "594mm"},
33 	{"A3", "297mm", "420mm"},
34 	{"A4", "210mm", "297mm"},
35 	{"A5", "148mm", "210mm"},
36 	{"A6", "105mm", "148mm"},
37 	{"A7", "74mm", "105mm"},
38 	{"A8", "52mm", "74mm"},
39 	{"A9", "37mm", "52mm"},
40 	{"A10", "26mm", "37mm"},
41 	{"B0", "1000mm", "1414mm"},
42 	{"B1", "707mm", "1000mm"},
43 	{"B2", "500mm", "707mm"},
44 	{"B3", "353mm", "500mm"},
45 	{"B4", "250mm", "353mm"},
46 	{"B5", "176mm", "250mm"},
47 	{"B6", "125mm", "176mm"},
48 	{"B7", "88mm", "125mm"},
49 	{"B8", "62mm", "88mm"},
50 	{"B9", "44mm", "62mm"},
51 	{"B10", "31mm", "44mm"},
52 	{"C0", "917mm", "1297mm"},
53 	{"C1", "648mm", "917mm"},
54 	{"C2", "458mm", "648mm"},
55 	{"C3", "324mm", "458mm"},
56 	{"C4", "229mm", "324mm"},
57 	{"C5", "162mm", "229mm"},
58 	{"C6", "114mm", "162mm"},
59 	{"C7", "81mm", "114mm"},
60 	{"C8", "57mm", "81mm"},
61 	{"C9", "40mm", "57mm"},
62 	{"C10", "28mm", "40mm"},
63 	{"US", 0, 0},
64 	{"archA", "9in", "12in"},
65 	{"archB", "12in", "18in"},
66 	{"archC", "18in", "24in"},
67 	{"archD", "24in", "36in"},
68 	{"archE", "36in", "48in"},
69 	{"executive", "7.5in", "10in"},
70 	{"flsa", "8.5in", "13in"},
71 	{"flse", "8.5in", "13in"},
72 	{"halfletter", "5.5in", "8.5in"},
73 	{"letter", "8.5in", "11in"},
74 	{"legal", "8.5in", "14in"},
75 	{"ledger", "17in", "11in"},
76 	{"note", "7.5in", "10in"},
77 	{"tabloid", "11in", "17in"},
78 	{"statement", "5.5in", "8.5in"},
79 	{0, 0, 0}
80 };
81 
str2class(const char * name)82 static DviPaperClass str2class(const char *name)
83 {
84 	if(STRCEQ(name, "ISO"))
85 		return MDVI_PAPER_CLASS_ISO;
86 	else if(STRCEQ(name, "US"))
87 		return MDVI_PAPER_CLASS_US;
88 	return MDVI_PAPER_CLASS_CUSTOM;
89 }
90 
mdvi_get_paper_size(const char * name,DviPaper * paper)91 int	mdvi_get_paper_size(const char *name, DviPaper *paper)
92 {
93 	const DviPaperSpec *sp;
94 	double	a, b;
95 	char	c, d, e, f;
96 	char	buf[32];
97 
98 	paper->pclass = MDVI_PAPER_CLASS_CUSTOM;
99 	if(sscanf(name, "%lfx%lf%c%c", &a, &b, &c, &d) == 4) {
100 		sprintf(buf, "%12.16f%c%c", a, c, d);
101 		paper->inches_wide = unit2pix_factor(buf);
102 		sprintf(buf, "%12.16f%c%c", b, c, d);
103 		paper->inches_tall = unit2pix_factor(buf);
104 		paper->name = _("custom");
105 		return 0;
106 	} else if(sscanf(name, "%lf%c%c,%lf%c%c", &a, &c, &d, &b, &e, &f) == 6) {
107 		sprintf(buf, "%12.16f%c%c", a, c, d);
108 		paper->inches_wide = unit2pix_factor(buf);
109 		sprintf(buf, "%12.16f%c%c", b, e, f);
110 		paper->inches_tall = unit2pix_factor(buf);
111 		paper->name = _("custom");
112 		return 0;
113 	}
114 
115 	for(sp = &papers[0]; sp->name; sp++) {
116 		if(!sp->width || !sp->height) {
117 			paper->pclass = str2class(sp->name);
118 			continue;
119 		}
120 		if(strcasecmp(sp->name, name) == 0) {
121 			paper->inches_wide = unit2pix_factor(sp->width);
122 			paper->inches_tall = unit2pix_factor(sp->height);
123 			paper->name = sp->name;
124 			return 0;
125 		}
126 	}
127 	return -1;
128 }
129 
mdvi_get_paper_specs(DviPaperClass pclass)130 DviPaperSpec *mdvi_get_paper_specs(DviPaperClass pclass)
131 {
132 	int	i;
133 	int	first, count;
134 	DviPaperSpec *spec, *ptr;
135 
136 	first = -1;
137 	count = 0;
138 	if(pclass == MDVI_PAPER_CLASS_ANY ||
139 	   pclass == MDVI_PAPER_CLASS_CUSTOM) {
140 	   	first = 0;
141 		count = (sizeof(papers) / sizeof(papers[0])) - 3;
142 	} else for(i = 0; papers[i].name; i++) {
143 		if(papers[i].width == NULL) {
144 			if(str2class(papers[i].name) == pclass)
145 				first = i;
146 			else if(first >= 0)
147 				break;
148 		} else if(first >= 0)
149 			count++;
150 	}
151 	ptr = spec = xnalloc(DviPaperSpec, count + 1);
152 	for(i = first; papers[i].name&& count > 0; i++) {
153 		if(papers[i].width) {
154 			ptr->name = papers[i].name;
155 			ptr->width = papers[i].width;
156 			ptr->height = papers[i].height;
157 			ptr++;
158 			count--;
159 		}
160 	}
161 	ptr->name = NULL;
162 	ptr->width = NULL;
163 	ptr->height = NULL;
164 
165 	return spec;
166 }
167 
mdvi_free_paper_specs(DviPaperSpec * spec)168 void	mdvi_free_paper_specs(DviPaperSpec *spec)
169 {
170 	mdvi_free(spec);
171 }
172