]> git.sur5r.net Git - glabels/blob - libglabels/lgl-units.c
Upload to unstable
[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) || (*id == '\0') )
121         {
122                 return LGL_UNITS_POINT;
123         }
124
125         for ( units = LGL_UNITS_FIRST; units <= LGL_UNITS_LAST; units++)
126         {
127                 if (g_ascii_strcasecmp (id, unit_table[units].id) == 0)
128                 {
129                         return units;
130                 }
131         }
132
133         /* Try name as a fallback. (Will catch some legacy preferences.) */
134         for ( units = LGL_UNITS_FIRST; units <= LGL_UNITS_LAST; units++)
135         {
136                 if (g_ascii_strcasecmp (id, unit_table[units].name) == 0)
137                 {
138                         return units;
139                 }
140         }
141
142         /* For compatibility with old preferences. */
143         if (g_ascii_strcasecmp (id, "Millimeters") == 0)
144         {
145                 return LGL_UNITS_MM;
146         }
147
148         return LGL_UNITS_INVALID;
149 }
150
151
152 /**
153  * lgl_units_get_name:
154  * @units:       Units (#lglUnits)
155  *
156  * Return a unique name string for the given units.  This name is human
157  * readable and will be translated to the current locale.
158  *
159  * Returns: name string.
160  *
161  */
162 const gchar *
163 lgl_units_get_name (lglUnits     units)
164 {
165         if ( (units >= LGL_UNITS_FIRST) && (units <= LGL_UNITS_LAST) )
166         {
167                 return gettext ((char *)unit_table[units].name);
168         }
169         else
170         {
171                 /* Default to "points", if invalid. */
172                 return gettext ((char *)unit_table[LGL_UNITS_POINT].name);
173         }
174 }
175
176
177 /**
178  * lgl_units_from_name:
179  * @name:       NAME string
180  *
181  * Return the unique #lglUnits for the given name string.  This name is
182  * human readable and is expected to be translated to the current locale.
183  *
184  * Returns: units (#lglUnits).
185  *
186  */
187 lglUnits
188 lgl_units_from_name (const gchar *name)
189 {
190         lglUnits units;
191
192         for ( units = LGL_UNITS_FIRST; units <= LGL_UNITS_LAST; units++)
193         {
194                 if (g_ascii_strcasecmp (name, gettext ((char *)unit_table[units].name) ) == 0)
195                 {
196                         return units;
197                 }
198         }
199
200         return LGL_UNITS_INVALID;
201 }
202
203
204 /**
205  * lgl_units_get_points_per_unit:
206  * @units:       Units (#lglUnits)
207  *
208  * Return a scale factor for the given units in points/unit.
209  *
210  * Returns: scale factor.
211  *
212  */
213 gdouble
214 lgl_units_get_points_per_unit (lglUnits     units)
215 {
216         if ( (units >= LGL_UNITS_FIRST) && (units <= LGL_UNITS_LAST) )
217         {
218                 return unit_table[units].points_per_unit;
219         }
220         else
221         {
222                 /* Default to "points", if invalid. */
223                 return 1.0;
224         }
225 }
226
227
228 /**
229  * lgl_units_get_units_per_point:
230  * @units:       Units (#lglUnits)
231  *
232  * Return a scale factor for the given units in units/point.
233  *
234  * Returns: scale factor.
235  *
236  */
237 gdouble
238 lgl_units_get_units_per_point (lglUnits     units)
239 {
240         if ( (units >= LGL_UNITS_FIRST) && (units <= LGL_UNITS_LAST) )
241         {
242                 return 1.0 / unit_table[units].points_per_unit;
243         }
244         else
245         {
246                 /* Default to "points", if invalid. */
247                 return 1.0;
248         }
249 }
250
251
252
253
254 /*
255  * Local Variables:       -- emacs
256  * mode: C                -- emacs
257  * c-basic-offset: 8      -- emacs
258  * tab-width: 8           -- emacs
259  * indent-tabs-mode: nil  -- emacs
260  * End:                   -- emacs
261  */