]> git.sur5r.net Git - glabels/blob - libglabels/lgl-units.c
Renamed libglabels source files to have "lgl-" prefix.
[glabels] / libglabels / lgl-units.c
1 /*
2  *  lgl-units.c
3  *  Copyright (C) 2003-2010  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of libglabels.
6  *
7  *  libglabels is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU Lesser General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  libglabels is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public License
18  *  along with libglabels.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22
23 #include "lgl-units.h"
24
25 #include <glib/gi18n.h>
26 #include <glib.h>
27 #include <string.h>
28
29 #include "libglabels-private.h"
30
31
32 /*========================================================*/
33 /* Private macros and constants.                          */
34 /*========================================================*/
35
36 #define POINTS_PER_POINT    1.0 /* internal units are points. */
37 #define POINTS_PER_INCH    72.0
38 #define POINTS_PER_MM       2.83464566929
39 #define POINTS_PER_CM       (10.0*POINTS_PER_MM)
40 #define POINTS_PER_PICA     (1.0/12.0)
41
42
43 /*========================================================*/
44 /* Private types.                                         */
45 /*========================================================*/
46
47 typedef struct {
48         gchar       *id;
49         gchar       *name;
50         gdouble      points_per_unit;
51 } UnitTableEntry;
52
53
54 /*========================================================*/
55 /* Private globals.                                       */
56 /*========================================================*/
57
58 static UnitTableEntry unit_table[] = {
59
60         /* The ids are identical to the absolute length units supported in
61            the CSS2 Specification (Section 4.3.2) */
62
63         /* This table must be sorted exactly as the enumerations in lglUnits */
64
65         /* [LGL_UNITS_POINT] */   {"pt", N_("points"), POINTS_PER_POINT},
66         /* [LGL_UNITS_INCH]  */   {"in", N_("inches"), POINTS_PER_INCH},
67         /* [LGL_UNITS_MM]    */   {"mm", N_("mm"),     POINTS_PER_MM},
68         /* [LGL_UNITS_CM]    */   {"cm", N_("cm"),     POINTS_PER_CM},
69         /* [LGL_UNITS_PICA]  */   {"pc", N_("picas"),  POINTS_PER_PICA},
70
71 };
72
73
74
75 /**
76  * lgl_units_get_id:
77  * @units:       Units (#lglUnits)
78  *
79  * Return a unique ID string for the given units.  This ID is how units
80  * are encoded in libglabels XML files and will remain constant across
81  * all locales.  IDs are identical to the absolute length units supported
82  * in the CSS2 Specification (Section 4.3.2). 
83  *
84  * Returns: ID string.
85  *
86  */
87 const gchar *
88 lgl_units_get_id (lglUnits     units)
89 {
90         if ( (units >= LGL_UNITS_FIRST) && (units <= LGL_UNITS_LAST) )
91         {
92                 return unit_table[units].id;
93         }
94         else
95         {
96                 /* Default to "pt", if invalid. */
97                 return unit_table[LGL_UNITS_POINT].id;
98         }
99 }
100
101
102 /**
103  * lgl_units_from_id:
104  * @id:       ID string
105  *
106  * Return the unique #lglUnits for the given ID string.
107  * This ID is how units are encoded in libglabels XML files and will remain
108  * constant across all locales.  IDs are identical to the absolute length
109  * units supported in the CSS2 Specification (Section 4.3.2). 
110  *
111  * Returns: units (#lglUnits).
112  *
113  */
114 lglUnits
115 lgl_units_from_id (const gchar *id)
116 {
117         lglUnits units;
118
119         /* An empty or missing id defaults to points. */
120         if ( (id == NULL) || (strlen (id) == 0) )
121         {
122                 return LGL_UNITS_POINT;
123         }
124
125         for ( units = LGL_UNITS_FIRST; units <= LGL_UNITS_LAST; units++) {
126                 if (g_ascii_strcasecmp (id, unit_table[units].id) == 0) {
127                         return units;
128                 }
129         }
130
131         /* Try name as a fallback. (Will catch some legacy preferences.) */
132         for ( units = LGL_UNITS_FIRST; units <= LGL_UNITS_LAST; units++) {
133                 if (g_ascii_strcasecmp (id, unit_table[units].name) == 0) {
134                         return units;
135                 }
136         }
137
138         /* For compatibility with old preferences. */
139         if (g_ascii_strcasecmp (id, "Millimeters") == 0) {
140                 return LGL_UNITS_MM;
141         }
142
143         return LGL_UNITS_INVALID;
144 }
145
146
147 /**
148  * lgl_units_get_name:
149  * @units:       Units (#lglUnits)
150  *
151  * Return a unique name string for the given units.  This name is human
152  * readable and will be translated to the current locale.
153  *
154  * Returns: name string.
155  *
156  */
157 const gchar *
158 lgl_units_get_name (lglUnits     units)
159 {
160         if ( (units >= LGL_UNITS_FIRST) && (units <= LGL_UNITS_LAST) )
161         {
162                 return gettext ((char *)unit_table[units].name);
163         }
164         else
165         {
166                 /* Default to "points", if invalid. */
167                 return gettext ((char *)unit_table[LGL_UNITS_POINT].name);
168         }
169 }
170
171
172 /**
173  * lgl_units_from_name:
174  * @name:       NAME string
175  *
176  * Return the unique #lglUnits for the given name string.  This name is
177  * human readable and is expected to be translated to the current locale.
178  *
179  * Returns: units (#lglUnits).
180  *
181  */
182 lglUnits
183 lgl_units_from_name (const gchar *name)
184 {
185         lglUnits units;
186
187         for ( units = LGL_UNITS_FIRST; units <= LGL_UNITS_LAST; units++) {
188                 if (g_ascii_strcasecmp (name, gettext ((char *)unit_table[units].name) ) == 0) {
189                         return units;
190                 }
191         }
192
193         return LGL_UNITS_INVALID;
194 }
195
196
197 /**
198  * lgl_units_get_points_per_unit:
199  * @units:       Units (#lglUnits)
200  *
201  * Return a scale factor for the given units in points/unit.
202  *
203  * Returns: scale factor.
204  *
205  */
206 gdouble
207 lgl_units_get_points_per_unit (lglUnits     units)
208 {
209         if ( (units >= LGL_UNITS_FIRST) && (units <= LGL_UNITS_LAST) )
210         {
211                 return unit_table[units].points_per_unit;
212         }
213         else
214         {
215                 /* Default to "points", if invalid. */
216                 return 1.0;
217         }
218 }
219
220
221 /**
222  * lgl_units_get_units_per_point:
223  * @units:       Units (#lglUnits)
224  *
225  * Return a scale factor for the given units in units/point.
226  *
227  * Returns: scale factor.
228  *
229  */
230 gdouble
231 lgl_units_get_units_per_point (lglUnits     units)
232 {
233         if ( (units >= LGL_UNITS_FIRST) && (units <= LGL_UNITS_LAST) )
234         {
235                 return 1.0 / unit_table[units].points_per_unit;
236         }
237         else
238         {
239                 /* Default to "points", if invalid. */
240                 return 1.0;
241         }
242 }
243
244
245
246
247 /*
248  * Local Variables:       -- emacs
249  * mode: C                -- emacs
250  * c-basic-offset: 8      -- emacs
251  * tab-width: 8           -- emacs
252  * indent-tabs-mode: nil  -- emacs
253  * End:                   -- emacs
254  */