1/***********************************************************************
2 *                                                                     *
3 * $Id: hpgsbase.c 270 2006-01-29 21:12:23Z softadm $
4 *                                                                     *
5 * hpgs - HPGl Script, a hpgl/2 interpreter, which uses a Postscript   *
6 *        API for rendering a scene and thus renders to a variety of   *
7 *        devices and fileformats.                                     *
8 *                                                                     *
9 * (C) 2004-2006 ev-i Informationstechnologie GmbH  http://www.ev-i.at *
10 *                                                                     *
11 * Author: Wolfgang Glas                                               *
12 *                                                                     *
13 *  hpgs is free software; you can redistribute it and/or              *
14 * modify it under the terms of the GNU Lesser General Public          *
15 * License as published by the Free Software Foundation; either        *
16 * version 2.1 of the License, or (at your option) any later version.  *
17 *                                                                     *
18 * hpgs is distributed in the hope that it will be useful,             *
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of      *
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   *
21 * Lesser General Public License for more details.                     *
22 *                                                                     *
23 * You should have received a copy of the GNU Lesser General Public    *
24 * License along with this library; if not, write to the               *
25 * Free Software  Foundation, Inc., 59 Temple Place, Suite 330,        *
26 * Boston, MA  02111-1307  USA                                         *
27 *                                                                     *
28 ***********************************************************************
29 *                                                                     *
30 * The implementation of basic helper functions.                       *
31 *                                                                     *
32 ***********************************************************************/
33
34#include<hpgs.h>
35#include<string.h>
36
37/*!
38   Parses a physical paper size with a given unit and returns the
39   width and the height of the paper in PostScript pt (1/72 inch).
40
41   The standard formats A4,A3,A2,A1 an A0 as well
42   as their landscape counterparts A4l,A3l,A2l,A1l and A0l are
43   accepted.
44
45   If the string does not represent a standard paper size,
46   it must be of the format <width>x<height>, where <width>
47   and <height> must follow the convention of \c hpgs_parse_papersize.
48
49   The function returns 0, if the string matches these conventions or
50   -1, if the string is in a wrong format.
51*/
52int hpgs_parse_papersize(const char *str, double *pt_width, double *pt_height)
53{
54  if (strcmp(str,"A4")==0)
55    {
56      *pt_width = 595.91084545538825881889;
57      *pt_height = 842.74519960822752756850;
58    }
59  else if (strcmp(str,"A3")==0)
60    {
61      *pt_width = 842.74519960822752756850;
62      *pt_height = 1191.82169091077651766614;
63    }
64  else if (strcmp(str,"A2")==0)
65    {
66      *pt_width = 1191.82169091077651766614;
67      *pt_height = 1685.49039921645505516535;
68    }
69  else if (strcmp(str,"A1")==0)
70    {
71      *pt_width = 1685.49039921645505516535;
72      *pt_height = 2383.64338182155303536062;
73    }
74  else if (strcmp(str,"A0")==0)
75    {
76      *pt_width = 2383.64338182155303536062;
77      *pt_height = 3370.98079843291011035905;
78    }
79  else if (strcmp(str,"A4l")==0)
80    {
81      *pt_height = 595.91084545538825881889;
82      *pt_width = 842.74519960822752756850;
83    }
84  else if (strcmp(str,"A3l")==0)
85    {
86      *pt_height = 842.74519960822752756850;
87      *pt_width = 1191.82169091077651766614;
88    }
89  else if (strcmp(str,"A2l")==0)
90    {
91      *pt_height = 1191.82169091077651766614;
92      *pt_width = 1685.49039921645505516535;
93    }
94  else if (strcmp(str,"A1l")==0)
95    {
96      *pt_height = 1685.49039921645505516535;
97      *pt_width = 2383.64338182155303536062;
98    }
99  else if (strcmp(str,"A0l")==0)
100    {
101      *pt_height = 2383.64338182155303536062;
102      *pt_width = 3370.98079843291011035905;
103    }
104  else
105    {
106      // find the 'x'
107      const char *x=strchr(str,'x');
108      char wstr[32];
109      int l;
110
111      if (!x) return -1;
112      l=x-str;
113      if (l > 31) return -1;
114
115      strncpy(wstr,str,l);
116      wstr[l] = '\0';
117
118      if (hpgs_parse_length(wstr,pt_width)) return -1;
119      if (hpgs_parse_length(str+l+1,pt_height)) return -1;
120    }
121  return 0;
122}
123
124/*!
125   Parses a physical length with a given unit and returns the length
126   in PostScript pt (1/72 inch).
127
128   The following formats are accepted:
129
130   \verbatim
131     27
132     37pt
133     4.5inch
134     0.37m
135     1.5cm
136     11.34mm
137   \endverbatim
138
139   If no unit is specified, PostScript points (1/72 inch) are assumed.
140
141   The function returns 0, if the string matches these conventions or
142   -1, if the string is in a wrong format.
143
144*/
145int hpgs_parse_length(const char *str, double *pt_length)
146{
147  const char *unit="pt";
148
149  char * endptr = (char*)str;
150
151  *pt_length = strtod(str,&endptr);
152
153  if (endptr == str) return -1;
154  if (*endptr) unit = endptr;
155
156  if (strcmp(unit,"cm")==0)
157    {
158      *pt_length *= 72.0 / 2.54;
159    }
160  else if (strcmp(unit,"mm")==0)
161    {
162      *pt_length *= 72.0 / 25.4;
163    }
164  else if (strcmp(unit,"m")==0)
165    {
166      *pt_length *= 72.0 / 0.0254;
167    }
168  else if (strcmp(unit,"inch")==0)
169    {
170      *pt_length *= 72.0;
171    }
172  else if (strcmp(unit,"pt"))
173    return -1;
174
175  return 0;
176}
177