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 {"pt", POINTS_PER_POINT},
59 {"in", POINTS_PER_INCH},
60 {"mm", POINTS_PER_MM},
61 {"cm", POINTS_PER_CM},
62 {"pc", POINTS_PER_PICA},
67 /*========================================================*/
68 /* Private function prototypes. */
69 /*========================================================*/
72 /****************************************************************************/
73 /* Return value of property as a double. */
74 /****************************************************************************/
76 gl_xml_get_prop_double (xmlNodePtr node,
77 const gchar *property,
83 string = xmlGetProp (node, property);
84 if ( string != NULL ) {
85 val = g_strtod (string, NULL);
93 /****************************************************************************/
94 /* Return value of property as a boolean. */
95 /****************************************************************************/
97 gl_xml_get_prop_boolean (xmlNodePtr node,
98 const gchar *property,
104 string = xmlGetProp (node, property);
105 if ( string != NULL ) {
106 val = !((xmlStrcasecmp (string, "false") == 0) ||
107 xmlStrEqual (string, "0"));;
116 /****************************************************************************/
117 /* Return value of property as an int. . */
118 /****************************************************************************/
120 gl_xml_get_prop_int (xmlNodePtr node,
121 const gchar *property,
127 string = xmlGetProp (node, property);
128 if ( string != NULL ) {
129 val = strtol (string, NULL, 0);
138 /****************************************************************************/
139 /* Return value of hex property as an unsigned int. */
140 /****************************************************************************/
142 gl_xml_get_prop_uint (xmlNodePtr node,
143 const gchar *property,
149 string = xmlGetProp (node, property);
150 if ( string != NULL ) {
151 val = strtoul (string, NULL, 0);
160 /****************************************************************************/
161 /* Return value of length property as a double, converting to internal units*/
162 /****************************************************************************/
164 gl_xml_get_prop_length (xmlNodePtr node,
165 const gchar *property,
169 gchar *string, *unit;
172 string = xmlGetProp (node, property);
173 if ( string != NULL ) {
175 val = g_strtod (string, &unit);
177 if (unit != string) {
178 unit = g_strchug (unit);
179 if (strlen (unit) > 0 ) {
180 for (i=0; unit_table[i].name != NULL; i++) {
181 if (xmlStrcasecmp (unit, unit_table[i].name) == 0) {
182 val *= unit_table[i].points_per_unit;
186 if (unit_table[i].name == NULL) {
187 g_warning ("Line %d, Node \"%s\", Property \"%s\": Unknown unit \"%s\", assuming points",
188 xmlGetLineNo (node), node->name, property,
204 /****************************************************************************/
205 /* Set property from double. */
206 /****************************************************************************/
208 gl_xml_set_prop_double (xmlNodePtr node,
209 const gchar *property,
212 gchar *string, buffer[G_ASCII_DTOSTR_BUF_SIZE];
214 /* Guarantee "C" locale by use of g_ascii_formatd */
215 string = g_ascii_formatd (buffer, G_ASCII_DTOSTR_BUF_SIZE, "%g", val);
217 xmlSetProp (node, property, string);
220 /****************************************************************************/
221 /* Set property from boolean. */
222 /****************************************************************************/
224 gl_xml_set_prop_boolean (xmlNodePtr node,
225 const gchar *property,
228 xmlSetProp (node, property, (val ? "True" : "False"));
231 /****************************************************************************/
232 /* Set property from int. */
233 /****************************************************************************/
235 gl_xml_set_prop_int (xmlNodePtr node,
236 const gchar *property,
241 string = g_strdup_printf ("%d", val);
242 xmlSetProp (node, property, string);
246 /****************************************************************************/
247 /* Set property from uint in hex. */
248 /****************************************************************************/
250 gl_xml_set_prop_uint_hex (xmlNodePtr node,
251 const gchar *property,
256 string = g_strdup_printf ("0x%08x", val);
257 xmlSetProp (node, property, string);
261 /****************************************************************************/
262 /* Set property from length. */
263 /****************************************************************************/
265 gl_xml_set_prop_length (xmlNodePtr node,
266 const gchar *property,
269 gchar *string, buffer[G_ASCII_DTOSTR_BUF_SIZE];
272 /* Guarantee "C" locale by use of g_ascii_formatd */
273 string = g_ascii_formatd (buffer, G_ASCII_DTOSTR_BUF_SIZE, "%g", val);
275 string_unit = g_strdup_printf ("%spt", string);
276 xmlSetProp (node, property, string_unit);
277 g_free (string_unit);