2 * (GLABELS) Label and Business Card Creation program for GNOME
4 * xml-label-04.c: GLabels xml label compat module
6 * Copyright (C) 2001-2002 Jim Evins <evins@snaught.com>.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "xml-label-04.h"
28 #include "label-text.h"
29 #include "label-box.h"
30 #include "label-line.h"
31 #include "label-ellipse.h"
32 #include "label-image.h"
33 #include "label-barcode.h"
37 static gboolean xml04_parse_media_description (xmlNodePtr node,
39 static void xml04_parse_object (xmlNodePtr node,
40 glLabelObject *object);
41 static void xml04_parse_text_props (xmlNodePtr node,
43 static void xml04_parse_box_props (xmlNodePtr node,
45 static void xml04_parse_line_props (xmlNodePtr node,
47 static void xml04_parse_ellipse_props (xmlNodePtr node,
48 glLabelEllipse *object);
49 static void xml04_parse_image_props (xmlNodePtr node,
50 glLabelImage *object);
51 static void xml04_parse_barcode_props (xmlNodePtr node,
52 glLabelBarcode *object);
53 static void xml04_parse_merge_properties (xmlNodePtr node,
57 /****************************************************************************/
58 /* PRIVATE. Parse xml doc structure and create label. */
59 /****************************************************************************/
60 glLabel *gl_xml_label_04_parse (xmlNodePtr root,
61 glXMLLabelStatus *status)
69 gl_debug (DEBUG_XML, "START");
71 *status = XML_LABEL_OK;
73 if (g_strcasecmp (root->name, "Label") != 0) {
74 g_warning (_("Bad root node = \"%s\""), root->name);
75 *status = XML_LABEL_ERROR_OPEN_PARSE;
79 label = GL_LABEL (gl_label_new ());
82 !(g_strcasecmp (xmlGetProp (root, "rotate"), "false") == 0);
83 gl_label_set_rotate_flag (label, rotate_flag);
85 for (node = root->xmlChildrenNode; node != NULL; node = node->next) {
87 gl_debug (DEBUG_XML, "node name = \"%s\"", node->name);
89 if (!xmlNodeIsText (node)) {
90 if (g_strcasecmp (node->name, "Media_Type") == 0) {
91 if (!xml04_parse_media_description (node, label)) {
92 *status = XML_LABEL_UNKNOWN_MEDIA;
94 } else if (g_strcasecmp (node->name, "Text") == 0) {
95 object = gl_label_text_new (label);
96 xml04_parse_object (node, GL_LABEL_OBJECT(object));
97 xml04_parse_text_props (node, GL_LABEL_TEXT(object));
98 } else if (g_strcasecmp (node->name, "Box") == 0) {
99 object = gl_label_box_new (label);
100 xml04_parse_object (node, GL_LABEL_OBJECT(object));
101 xml04_parse_box_props (node, GL_LABEL_BOX(object));
102 } else if (g_strcasecmp (node->name, "Line") == 0) {
103 object = gl_label_line_new (label);
104 xml04_parse_object (node, GL_LABEL_OBJECT(object));
105 xml04_parse_line_props (node, GL_LABEL_LINE(object));
106 } else if (g_strcasecmp (node->name, "Ellipse") == 0) {
107 object = gl_label_ellipse_new (label);
108 xml04_parse_object (node, GL_LABEL_OBJECT(object));
109 xml04_parse_ellipse_props (node,
110 GL_LABEL_ELLIPSE(object));
111 } else if (g_strcasecmp (node->name, "Image") == 0) {
112 object = gl_label_image_new (label);
113 xml04_parse_object (node, GL_LABEL_OBJECT(object));
114 xml04_parse_image_props (node, GL_LABEL_IMAGE(object));
115 } else if (g_strcasecmp (node->name, "Barcode") == 0) {
116 object = gl_label_barcode_new (label);
117 xml04_parse_object (node, GL_LABEL_OBJECT(object));
118 xml04_parse_barcode_props (node,
119 GL_LABEL_BARCODE(object));
120 } else if (g_strcasecmp (node->name, "Merge_Properties") == 0) {
121 xml04_parse_merge_properties (node, label);
123 g_warning (_("bad node = \"%s\""), node->name);
128 gl_debug (DEBUG_XML, "END");
133 /*--------------------------------------------------------------------------*/
134 /* PRIVATE. Parse Media Description node. */
135 /*--------------------------------------------------------------------------*/
137 xml04_parse_media_description (xmlNodePtr node,
140 gchar *template_name;
141 glTemplate *template;
144 gl_debug (DEBUG_XML, "START");
146 template_name = xmlNodeGetContent (node);
148 template = gl_template_from_name (template_name);
149 if (template == NULL) {
150 g_warning ("Undefined template \"%s\"", template_name);
152 template = gl_template_from_name (NULL);
158 gl_label_set_template (label, template);
160 gl_template_free (&template);
161 g_free (template_name);
163 gl_debug (DEBUG_XML, "END");
168 /*--------------------------------------------------------------------------*/
169 /* PRIVATE. Parse XML Object Node */
170 /*--------------------------------------------------------------------------*/
172 xml04_parse_object (xmlNodePtr object_node,
173 glLabelObject *object)
177 gl_debug (DEBUG_XML, "START");
179 x = g_strtod (xmlGetProp (object_node, "x"), NULL);
180 y = g_strtod (xmlGetProp (object_node, "y"), NULL);
182 gl_label_object_set_position (object, x, y);
184 gl_debug (DEBUG_XML, "END");
187 /*--------------------------------------------------------------------------*/
188 /* PRIVATE. Parse XML Label->Text Node Properties */
189 /*--------------------------------------------------------------------------*/
191 xml04_parse_text_props (xmlNodePtr object_node,
196 GnomeFontWeight font_weight;
197 gboolean font_italic_flag;
199 GtkJustification just;
200 xmlNodePtr line_node, text_node;
201 glTextNode *node_text;
202 GList *nodes, *lines;
205 gl_debug (DEBUG_XML, "START");
207 font_family = xmlGetProp (object_node, "font_family");
208 font_size = g_strtod (xmlGetProp (object_node, "font_size"), NULL);
210 gl_util_string_to_weight (xmlGetProp (object_node, "font_weight"));
212 !(g_strcasecmp (xmlGetProp (object_node, "font_italic"), "false") ==
215 just = gl_util_string_to_just (xmlGetProp (object_node, "justify"));
217 sscanf (xmlGetProp (object_node, "color"), "%x", &color);
219 gl_label_text_set_props (object,
228 for (line_node = object_node->xmlChildrenNode; line_node != NULL;
229 line_node = line_node->next) {
231 if (g_strcasecmp (line_node->name, "Line") == 0) {
234 for (text_node = line_node->xmlChildrenNode;
235 text_node != NULL; text_node = text_node->next) {
237 if (g_strcasecmp (text_node->name, "Field") ==
239 node_text = g_new0 (glTextNode, 1);
240 node_text->field_flag = TRUE;
242 xmlGetProp (text_node, "name");
244 g_list_append (nodes, node_text);
245 } else if (xmlNodeIsText (text_node)) {
246 node_text = g_new0 (glTextNode, 1);
247 node_text->field_flag = FALSE;
249 xmlNodeGetContent (text_node);
251 g_list_append (nodes, node_text);
253 g_warning ("Unexpected Text Line child: \"%s\"",
258 lines = g_list_append (lines, nodes);
260 } else if (!xmlNodeIsText (line_node)) {
261 g_warning ("Unexpected Text child: \"%s\"",
267 gl_label_text_set_lines (object, lines);
269 gl_text_node_lines_free (&lines);
270 g_free (font_family);
272 /* Adjust location. In 0.4.x, text was anchored at x,y */
273 gl_label_object_get_position (GL_LABEL_OBJECT(object), &x, &y);
274 gl_label_object_get_size (GL_LABEL_OBJECT(object), &w, &h);
276 case GTK_JUSTIFY_LEFT:
279 case GTK_JUSTIFY_CENTER:
281 gl_label_object_set_position (GL_LABEL_OBJECT(object), x, y);
283 case GTK_JUSTIFY_RIGHT:
285 gl_label_object_set_position (GL_LABEL_OBJECT(object), x, y);
288 /* should not happen */
291 gl_debug (DEBUG_XML, "END");
294 /*--------------------------------------------------------------------------*/
295 /* PRIVATE. Parse XML Label->Box Node Properties */
296 /*--------------------------------------------------------------------------*/
298 xml04_parse_box_props (xmlNodePtr node,
301 gdouble w, h, line_width;
302 guint line_color, fill_color;
304 gl_debug (DEBUG_XML, "START");
306 w = g_strtod (xmlGetProp (node, "w"), NULL);
307 h = g_strtod (xmlGetProp (node, "h"), NULL);
309 line_width = g_strtod (xmlGetProp (node, "line_width"), NULL);
311 sscanf (xmlGetProp (node, "line_color"), "%x", &line_color);
312 sscanf (xmlGetProp (node, "fill_color"), "%x", &fill_color);
314 gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
315 gl_label_box_set_line_width (object, line_width);
316 gl_label_box_set_line_color (object, line_color);
317 gl_label_box_set_fill_color (object, fill_color);
319 gl_debug (DEBUG_XML, "END");
322 /*--------------------------------------------------------------------------*/
323 /* PRIVATE. Parse XML Label->Line Node Properties */
324 /*--------------------------------------------------------------------------*/
326 xml04_parse_line_props (xmlNodePtr node,
329 gdouble w, h, line_width;
332 gl_debug (DEBUG_XML, "START");
334 w = g_strtod (xmlGetProp (node, "dx"), NULL);
335 h = g_strtod (xmlGetProp (node, "dy"), NULL);
337 line_width = g_strtod (xmlGetProp (node, "line_width"), NULL);
339 sscanf (xmlGetProp (node, "line_color"), "%x", &line_color);
341 gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
342 gl_label_line_set_line_width (object, line_width);
343 gl_label_line_set_line_color (object, line_color);
345 gl_debug (DEBUG_XML, "END");
348 /*--------------------------------------------------------------------------*/
349 /* PRIVATE. Parse XML Label->Ellipse Node Properties */
350 /*--------------------------------------------------------------------------*/
352 xml04_parse_ellipse_props (xmlNodePtr node,
353 glLabelEllipse *object)
355 gdouble w, h, line_width;
356 guint line_color, fill_color;
358 gl_debug (DEBUG_XML, "START");
360 w = g_strtod (xmlGetProp (node, "w"), NULL);
361 h = g_strtod (xmlGetProp (node, "h"), NULL);
363 line_width = g_strtod (xmlGetProp (node, "line_width"), NULL);
365 sscanf (xmlGetProp (node, "line_color"), "%x", &line_color);
366 sscanf (xmlGetProp (node, "fill_color"), "%x", &fill_color);
368 gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
369 gl_label_ellipse_set_line_width (object, line_width);
370 gl_label_ellipse_set_line_color (object, line_color);
371 gl_label_ellipse_set_fill_color (object, fill_color);
373 gl_debug (DEBUG_XML, "END");
376 /*--------------------------------------------------------------------------*/
377 /* PRIVATE. Parse XML Label->Image Node Properties */
378 /*--------------------------------------------------------------------------*/
380 xml04_parse_image_props (xmlNodePtr node,
381 glLabelImage *object)
384 glTextNode *filename;
386 gl_debug (DEBUG_XML, "START");
388 w = g_strtod (xmlGetProp (node, "w"), NULL);
389 h = g_strtod (xmlGetProp (node, "h"), NULL);
390 gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
392 filename = g_new0 (glTextNode, 1);
393 filename->field_flag = FALSE;
394 filename->data = xmlGetProp (node, "filename");
395 gl_label_image_set_filename (object, filename);
396 gl_text_node_free (&filename);
398 gl_debug (DEBUG_XML, "END");
401 /*--------------------------------------------------------------------------*/
402 /* PRIVATE. Parse XML Label->Barcode Node Properties */
403 /*--------------------------------------------------------------------------*/
405 xml04_parse_barcode_props (xmlNodePtr node,
406 glLabelBarcode *object)
408 glBarcodeStyle style;
413 glTextNode *text_node;
415 gl_debug (DEBUG_XML, "START");
417 sscanf (xmlGetProp (node, "color"), "%x", &color);
418 style = gl_barcode_text_to_style (xmlGetProp (node, "style"));
419 text_flag = !(g_strcasecmp (xmlGetProp (node, "text"), "false") == 0);
420 scale = g_strtod (xmlGetProp (node, "scale"), NULL);
422 scale = 0.5; /* Set to a valid value */
424 gl_label_barcode_set_props (object, style, text_flag, color, scale);
426 child = node->xmlChildrenNode;
427 text_node = g_new0 (glTextNode, 1);
428 if (g_strcasecmp (child->name, "Field") == 0) {
429 text_node->field_flag = TRUE;
430 text_node->data = xmlGetProp (child, "name");
431 } else if (xmlNodeIsText (child)) {
432 text_node->field_flag = FALSE;
433 text_node->data = xmlNodeGetContent (child);
435 g_warning ("Unexpected Barcode child: \"%s\"", child->name);
437 gl_label_barcode_set_data (object, text_node);
439 gl_text_node_free (&text_node);
441 gl_debug (DEBUG_XML, "END");
444 /*--------------------------------------------------------------------------*/
445 /* PRIVATE. Parse XML merge properties tag. */
446 /*--------------------------------------------------------------------------*/
448 xml04_parse_merge_properties (xmlNodePtr node,
454 gl_debug (DEBUG_XML, "START");
456 merge = gl_merge_new (xmlGetProp (node, "type"));
457 gl_merge_set_src (merge, xmlGetProp (node, "src"));
459 gl_label_set_merge (label, merge);
461 g_object_unref (G_OBJECT(merge));
463 gl_debug (DEBUG_XML, "END");