3 * Copyright (C) 2003-2009 Jim Evins <evins@snaught.com>.
5 * This file is part of libglabels.
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.
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.
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/>.
25 #include <glib/gi18n.h>
29 #include "libglabels-private.h"
32 /*========================================================*/
33 /* Private macros and constants. */
34 /*========================================================*/
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)
43 /*========================================================*/
45 /*========================================================*/
50 gdouble points_per_unit;
54 /*========================================================*/
55 /* Private globals. */
56 /*========================================================*/
58 static UnitTableEntry unit_table[] = {
60 /* The ids are identical to the absolute length units supported in
61 the CSS2 Specification (Section 4.3.2) */
63 /* This table must be sorted exactly as the enumerations in lglUnits */
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},
77 * @units: Units (#lglUnits)
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).
88 lgl_units_get_id (lglUnits units)
90 if ( (units >= LGL_UNITS_FIRST) && (units <= LGL_UNITS_LAST) )
92 return unit_table[units].id;
96 /* Default to "pt", if invalid. */
97 return unit_table[LGL_UNITS_POINT].id;
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).
111 * Returns: units (#lglUnits).
115 lgl_units_from_id (const gchar *id)
119 /* An empty or missing id defaults to points. */
120 if ( (id == NULL) || (strlen (id) == 0) )
122 return LGL_UNITS_POINT;
125 for ( units = LGL_UNITS_FIRST; units <= LGL_UNITS_LAST; units++) {
126 if (g_ascii_strcasecmp (id, unit_table[units].id) == 0) {
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) {
138 /* For compatibility with old preferences. */
139 if (g_ascii_strcasecmp (id, "Millimeters") == 0) {
143 return LGL_UNITS_INVALID;
148 * lgl_units_get_name:
149 * @units: Units (#lglUnits)
151 * Return a unique name string for the given units. This name is human
152 * readable and will be translated to the current locale.
154 * Returns: name string.
158 lgl_units_get_name (lglUnits units)
160 if ( (units >= LGL_UNITS_FIRST) && (units <= LGL_UNITS_LAST) )
162 return gettext ((char *)unit_table[units].name);
166 /* Default to "points", if invalid. */
167 return gettext ((char *)unit_table[LGL_UNITS_POINT].name);
173 * lgl_units_from_name:
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.
179 * Returns: units (#lglUnits).
183 lgl_units_from_name (const gchar *name)
187 for ( units = LGL_UNITS_FIRST; units <= LGL_UNITS_LAST; units++) {
188 if (g_ascii_strcasecmp (name, gettext ((char *)unit_table[units].name) ) == 0) {
193 return LGL_UNITS_INVALID;
198 * lgl_units_get_points_per_unit:
199 * @units: Units (#lglUnits)
201 * Return a scale factor for the given units in points/unit.
203 * Returns: scale factor.
207 lgl_units_get_points_per_unit (lglUnits units)
209 if ( (units >= LGL_UNITS_FIRST) && (units <= LGL_UNITS_LAST) )
211 return unit_table[units].points_per_unit;
215 /* Default to "points", if invalid. */
222 * lgl_units_get_units_per_point:
223 * @units: Units (#lglUnits)
225 * Return a scale factor for the given units in units/point.
227 * Returns: scale factor.
231 lgl_units_get_units_per_point (lglUnits units)
233 if ( (units >= LGL_UNITS_FIRST) && (units <= LGL_UNITS_LAST) )
235 return 1.0 / unit_table[units].points_per_unit;
239 /* Default to "points", if invalid. */
248 * Local Variables: -- emacs
250 * c-basic-offset: 8 -- emacs
251 * tab-width: 8 -- emacs
252 * indent-tabs-mode: nil -- emacs