2 * (LIBGLABELS) Template library for GLABELS
4 * xml.c: GLabels xml utilities module
6 * Copyright (C) 2003, 2004 Jim Evins <evins@snaught.com>.
8 * This file is part of the LIBGLABELS library.
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the Free
22 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26 #include "libglabels-private.h"
30 /*========================================================*/
31 /* Private macros and constants. */
32 /*========================================================*/
34 #define POINTS_PER_POINT 1.0 /* internal units are points. */
35 #define POINTS_PER_INCH 72.0
36 #define POINTS_PER_MM 2.83464566929
37 #define POINTS_PER_CM (10.0*POINTS_PER_MM)
38 #define POINTS_PER_PICA (1.0/12.0)
40 /*========================================================*/
42 /*========================================================*/
46 gdouble points_per_unit;
49 /*========================================================*/
50 /* Private globals. */
51 /*========================================================*/
53 static UnitTableEntry unit_table[] = {
55 /* These names are identical to the absolute length units supported in
56 the CSS2 Specification (Section 4.3.2) */
58 /* This table must be sorted exactly as the enumerations in glUnitsType */
60 /* [GL_UNITS_POINT] */ {"pt", POINTS_PER_POINT},
61 /* [GL_UNITS_INCH] */ {"in", POINTS_PER_INCH},
62 /* [GL_UNITS_MM] */ {"mm", POINTS_PER_MM},
63 /* [GL_UNITS_CM] */ {"cm", POINTS_PER_CM},
64 /* [GL_UNITS_PICA] */ {"pc", POINTS_PER_PICA},
68 static glUnitsType default_units = GL_UNITS_POINT;
70 /*========================================================*/
71 /* Private function prototypes. */
72 /*========================================================*/
75 /****************************************************************************/
76 /* Return value of property as a double. */
77 /****************************************************************************/
79 gl_xml_get_prop_double (xmlNodePtr node,
80 const gchar *property,
86 string = xmlGetProp (node, property);
87 if ( string != NULL ) {
88 val = g_strtod (string, NULL);
96 /****************************************************************************/
97 /* Return value of property as a boolean. */
98 /****************************************************************************/
100 gl_xml_get_prop_boolean (xmlNodePtr node,
101 const gchar *property,
102 gboolean default_val)
107 string = xmlGetProp (node, property);
108 if ( string != NULL ) {
109 val = !((xmlStrcasecmp (string, "false") == 0) ||
110 xmlStrEqual (string, "0"));;
119 /****************************************************************************/
120 /* Return value of property as an int. . */
121 /****************************************************************************/
123 gl_xml_get_prop_int (xmlNodePtr node,
124 const gchar *property,
130 string = xmlGetProp (node, property);
131 if ( string != NULL ) {
132 val = strtol (string, NULL, 0);
141 /****************************************************************************/
142 /* Return value of hex property as an unsigned int. */
143 /****************************************************************************/
145 gl_xml_get_prop_uint (xmlNodePtr node,
146 const gchar *property,
152 string = xmlGetProp (node, property);
153 if ( string != NULL ) {
154 val = strtoul (string, NULL, 0);
163 /****************************************************************************/
164 /* Return value of length property as a double, converting to internal units*/
165 /****************************************************************************/
167 gl_xml_get_prop_length (xmlNodePtr node,
168 const gchar *property,
172 gchar *string, *unit;
175 string = xmlGetProp (node, property);
176 if ( string != NULL ) {
178 val = g_strtod (string, &unit);
180 if (unit != string) {
181 unit = g_strchug (unit);
182 if (strlen (unit) > 0 ) {
183 for (i=GL_UNITS_FIRST; i<=GL_UNITS_LAST; i++) {
184 if (xmlStrcasecmp (unit, unit_table[i].name) == 0) {
185 val *= unit_table[i].points_per_unit;
189 if (i>GL_UNITS_LAST) {
190 g_warning ("Line %d, Node \"%s\", Property \"%s\": Unknown unit \"%s\", assuming points",
191 xmlGetLineNo (node), node->name, property,
207 /****************************************************************************/
208 /* Set property from double. */
209 /****************************************************************************/
211 gl_xml_set_prop_double (xmlNodePtr node,
212 const gchar *property,
215 gchar *string, buffer[G_ASCII_DTOSTR_BUF_SIZE];
217 /* Guarantee "C" locale by use of g_ascii_formatd */
218 string = g_ascii_formatd (buffer, G_ASCII_DTOSTR_BUF_SIZE, "%g", val);
220 xmlSetProp (node, property, string);
223 /****************************************************************************/
224 /* Set property from boolean. */
225 /****************************************************************************/
227 gl_xml_set_prop_boolean (xmlNodePtr node,
228 const gchar *property,
231 xmlSetProp (node, property, (val ? "True" : "False"));
234 /****************************************************************************/
235 /* Set property from int. */
236 /****************************************************************************/
238 gl_xml_set_prop_int (xmlNodePtr node,
239 const gchar *property,
244 string = g_strdup_printf ("%d", val);
245 xmlSetProp (node, property, string);
249 /****************************************************************************/
250 /* Set property from uint in hex. */
251 /****************************************************************************/
253 gl_xml_set_prop_uint_hex (xmlNodePtr node,
254 const gchar *property,
259 string = g_strdup_printf ("0x%08x", val);
260 xmlSetProp (node, property, string);
264 /****************************************************************************/
265 /* Set property from length. */
266 /****************************************************************************/
268 gl_xml_set_prop_length (xmlNodePtr node,
269 const gchar *property,
272 gchar *string, buffer[G_ASCII_DTOSTR_BUF_SIZE];
275 /* Convert to default units */
276 val /= unit_table[default_units].points_per_unit;
278 /* Guarantee "C" locale by use of g_ascii_formatd */
279 string = g_ascii_formatd (buffer, G_ASCII_DTOSTR_BUF_SIZE, "%g", val);
281 string_unit = g_strdup_printf ("%s%s", string, unit_table[default_units].name);
282 xmlSetProp (node, property, string_unit);
283 g_free (string_unit);
286 /****************************************************************************/
287 /* Set default length units. */
288 /****************************************************************************/
290 gl_xml_set_default_units (glUnitsType units)
292 g_return_if_fail ((units >= GL_UNITS_FIRST) && (units <= GL_UNITS_LAST));
294 default_units = units;