Debian Codegraph @

libhylafax/PageSize.c++

raw
1 /* $Id$ */
2 /*
3 * Copyright (c) 1993-1996 Sam Leffler
4 * Copyright (c) 1993-1996 Silicon Graphics, Inc.
5 * HylaFAX is a trademark of Silicon Graphics
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26 /*
27 * Page Size Support.
28 */
29 #include "Types.h"
30 #include "PageSize.h"
31 #include "Array.h"
32 #include "config.h"
33
34 #include <string.h>
35 #include <ctype.h>
36 #include <stdlib.h>
37
38 #include "NLS.h"
39
40 fxDECLARE_StructArray(PageInfoArray, PageInfo)
41 fxIMPLEMENT_StructArray(PageInfoArray, PageInfo)
42
43 PageInfoArray* PageSizeInfo::pageInfo = NULL;
44
45 #include <stdarg.h>
46
47 static void
48 parseError(const char* file, u_int lineno, const char* fmt ...)
49 {
50 va_list ap;
51 va_start(ap, fmt);
52 fprintf(stderr, NLS::TEXT("%s: line %u: "), file, lineno);
53 vfprintf(stderr, fmt, ap);
54 va_end(ap);
55 }
56
57 bool
58 PageSizeInfo::skipws(char*& cp,
59 const char* file, const char* item, u_int lineno)
60 {
61 if (isspace(*cp))
62 *cp++ = '\0';
63 while (isspace(*cp))
64 cp++;
65 if (*cp == '\0') {
66 parseError(file, lineno,
67 NLS::TEXT("Unexpected end of line after \"%s\".\n"), item);
68 return (false);
69 } else
70 return (true);
71 }
72
73 PageInfoArray*
74 PageSizeInfo::readPageInfoFile()
75 {
76 fxStr file = FAX_LIBDATA;
77 file.append("/");
78 file.append(FAX_PAGESIZES);
79 PageInfoArray* info = new PageInfoArray;
80 FILE* fp = fopen(file, "r");
81 u_int lineno = 0;
82 if (fp) {
83 char line[1024];
84 while (fgets(line, sizeof (line), fp)) {
85 lineno++;
86 char* cp = strchr(line, '#');
87 if (cp)
88 *cp = '\0';
89 else if ((cp = strchr(line, '\n')))
90 *cp = '\0';
91 for (cp = line; isspace(*cp); cp++)
92 ;
93 if (*cp == '\0')
94 continue;
95 // name<tab>width<ws>height<ws>gwidth<ws>gheight<ws>top<ws>left
96 PageInfo pi;
97 pi.name = cp;
98 while (*cp != '\t')
99 cp++;
100 if (!skipws(cp, file, NLS::TEXT("page size name"), lineno))
101 continue;
102 pi.abbr = cp;
103 while (*cp != '\t')
104 cp++;
105 if (!skipws(cp, file, NLS::TEXT("page size abbreviation"), lineno))
106 continue;
107 pi.w = (BMU) strtoul(cp, &cp, 10);
108 if (!skipws(cp, file, NLS::TEXT("page width"), lineno))
109 continue;
110 pi.h = (BMU) strtoul(cp, &cp, 10);
111 if (!skipws(cp, file, NLS::TEXT("page height"), lineno))
112 continue;
113 pi.grw = (BMU) strtoul(cp, &cp, 10);
114 if (!skipws(cp, file, NLS::TEXT("guaranteed page width"), lineno))
115 continue;
116 pi.grh = (BMU) strtoul(cp, &cp, 10);
117 if (!skipws(cp, file, NLS::TEXT("guaranteed page height"), lineno))
118 continue;
119 pi.top = (BMU) strtoul(cp, &cp, 10);
120 if (!skipws(cp, file, NLS::TEXT("top margin"), lineno))
121 continue;
122 pi.left = (BMU) strtoul(cp, &cp, 10);
123 pi.name = strdup(pi.name);
124 pi.abbr = strdup(pi.abbr);
125 info->append(pi);
126 }
127 fclose(fp);
128 } else {
129 fprintf(stderr,
130 NLS::TEXT("Warning, no page size database file \"%s\", using builtin default.\n"),
131 (const char*)file);
132 PageInfo pi;
133 pi.name = strdup("default");
134 pi.abbr = strdup("NA-LET");
135 // North American Letter
136 pi.w = 10200;
137 pi.h = 13200;
138 pi.grw = 9240;
139 pi.grh = 12400;
140 pi.top = 472;
141 pi.left = 345;
142 info->append(pi);
143 }
144 return info;
145 }
146
147 const PageInfo*
148 PageSizeInfo::getPageInfoByName(const char* name)
149 {
150 int c = tolower(name[0]);
151 size_t len = strlen(name);
152 for (int i = 0, n = pageInfo->length(); i < n; i++) {
153 const PageInfo& pi = (*pageInfo)[i];
154 if (strncasecmp(pi.abbr, name, len) == 0)
155 return &pi;
156 for (const char* cp = pi.name; *cp != '\0'; cp++)
157 if (tolower(*cp) == c && strncasecmp(cp, name, len) == 0)
158 return &pi;
159 }
160 return (NULL);
161 }
162
163 PageSizeInfo*
164 PageSizeInfo::getPageSizeByName(const char* name)
165 {
166 if (pageInfo == NULL)
167 pageInfo = readPageInfoFile();
168 const PageInfo* info = getPageInfoByName(name);
169 return info ? new PageSizeInfo(*info) : (PageSizeInfo*) NULL;
170 }
171
172 PageSizeInfo*
173 PageSizeInfo::getPageSizeBySize(float wmm, float hmm)
174 {
175 BMU w = fromMM(wmm);
176 BMU h = fromMM(hmm);
177
178 if (pageInfo == NULL)
179 pageInfo = readPageInfoFile();
180 int best = 0;
181 u_long bestMeasure = (u_long)-1;
182 for (int i = 0, n = pageInfo->length(); i < n; i++) {
183 int dw = (int)((*pageInfo)[i].w - w);
184 int dh = (int)((*pageInfo)[i].h - h);
185 u_long measure = dw*dw + dh*dh;
186 if (measure < bestMeasure) {
187 best = i;
188 bestMeasure = measure;
189 }
190 }
191 #define THRESHOLD 720000 // .5" in each dimension
192 return bestMeasure < THRESHOLD ?
193 new PageSizeInfo((*pageInfo)[best]) : (PageSizeInfo*) NULL;
194 }
195
196 PageSizeInfo::PageSizeInfo()
197 {
198 if (pageInfo == NULL)
199 pageInfo = readPageInfoFile();
200 info = getPageInfoByName("default");
201 }
202 PageSizeInfo::PageSizeInfo(const PageInfo& i) : info(&i) {}
203 PageSizeInfo::~PageSizeInfo() {}
204
205 /*
206 * C stub interfaces...
207 */
208 extern "C" struct pageSizeInfo*
209 getPageSize(const char* name)
210 {
211 return (struct pageSizeInfo*) PageSizeInfo::getPageSizeByName(name);
212 }
213
214 extern "C" struct pageSizeInfo*
215 closestPageSize(float w, float h)
216 {
217 return (struct pageSizeInfo*) PageSizeInfo::getPageSizeBySize(w,h);
218 }
219
220 extern "C" void
221 delPageSize(struct pageSizeInfo* info)
222 {
223 delete (PageSizeInfo*) info;
224 }
225
226 extern "C" const char*
227 getPageName(const struct pageSizeInfo* info)
228 {
229 return ((const PageSizeInfo*) info)->name();
230 }
231
232 extern "C" const char*
233 getPageAbbrev(const struct pageSizeInfo* info)
234 {
235 return ((const PageSizeInfo*) info)->abbrev();
236 }
237
238 extern "C" float
239 getPageWidth(const struct pageSizeInfo* info)
240 {
241 return ((const PageSizeInfo*) info)->width();
242 }
243
244 extern "C" float
245 getPageHeight(const struct pageSizeInfo* info)
246 {
247 return ((const PageSizeInfo*) info)->height();
248 }
249
250 extern "C" float
251 getPageGuarHeight(const struct pageSizeInfo* info)
252 {
253 return ((const PageSizeInfo*) info)->guarHeight();
254 }
255
256 extern "C" float
257 getPageGuarWidth(const struct pageSizeInfo* info)
258 {
259 return ((const PageSizeInfo*) info)->guarWidth();
260 }
261
262 extern "C" float
263 getPageTopMargin(const struct pageSizeInfo* info)
264 {
265 return ((const PageSizeInfo*) info)->topMargin();
266 }
267
268 extern "C" float
269 getPageLeftMargin(const struct pageSizeInfo* info)
270 {
271 return ((const PageSizeInfo*) info)->leftMargin();
272 }
273
274 /*
275 * Page size iterator support.
276 */
277
278 PageSizeInfoIter::PageSizeInfoIter()
279 {
280 i = 0;
281 }
282 PageSizeInfoIter::~PageSizeInfoIter() {}
283 void PageSizeInfoIter::operator++() { i++; }
284 void PageSizeInfoIter::operator++(int) { i++; }
285 PageSizeInfoIter::operator const PageSizeInfo&()
286 {
287 if (i < PageSizeInfo::pageInfo->length())
288 pi.info = &(*PageSizeInfo::pageInfo)[i];
289 return (pi);
290 }
291 bool PageSizeInfoIter::notDone()
292 { return i < PageSizeInfo::pageInfo->length(); }