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"
34 #include <libglabels/xml.h>
38 static gboolean xml04_parse_media_description (xmlNodePtr node,
40 static void xml04_parse_object (xmlNodePtr node,
41 glLabelObject *object);
42 static void xml04_parse_text_props (xmlNodePtr node,
44 static void xml04_parse_box_props (xmlNodePtr node,
46 static void xml04_parse_line_props (xmlNodePtr node,
48 static void xml04_parse_ellipse_props (xmlNodePtr node,
49 glLabelEllipse *object);
50 static void xml04_parse_image_props (xmlNodePtr node,
51 glLabelImage *object);
52 static void xml04_parse_barcode_props (xmlNodePtr node,
53 glLabelBarcode *object);
54 static void xml04_parse_merge_properties (xmlNodePtr node,
58 /****************************************************************************/
59 /* PRIVATE. Parse xml doc structure and create label. */
60 /****************************************************************************/
61 glLabel *gl_xml_label_04_parse (xmlNodePtr root,
62 glXMLLabelStatus *status)
70 gl_debug (DEBUG_XML, "START");
72 *status = XML_LABEL_OK;
74 if (!xmlStrEqual (root->name, "Label")) {
75 g_warning (_("Bad root node = \"%s\""), root->name);
76 *status = XML_LABEL_ERROR_OPEN_PARSE;
80 label = GL_LABEL (gl_label_new ());
82 rotate_flag = gl_xml_get_prop_boolean (root, "rotate", FALSE);
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 (xmlStrEqual (node->name, "Media_Type")) {
91 if (!xml04_parse_media_description (node, label)) {
92 *status = XML_LABEL_UNKNOWN_MEDIA;
94 } else if (xmlStrEqual (node->name, "Text")) {
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 (xmlStrEqual (node->name, "Box")) {
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 (xmlStrEqual (node->name, "Line")) {
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 (xmlStrEqual (node->name, "Ellipse")) {
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 (xmlStrEqual (node->name, "Image")) {
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 (xmlStrEqual (node->name, "Barcode")) {
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 (xmlStrEqual (node->name, "Merge_Properties")) {
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 = gl_xml_get_prop_double (object_node, "x", 0);
180 y = gl_xml_get_prop_double (object_node, "y", 0);
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;
206 gl_debug (DEBUG_XML, "START");
208 font_family = xmlGetProp (object_node, "font_family");
209 font_size = gl_xml_get_prop_double (object_node, "font_size", 0);
210 string = xmlGetProp (object_node, "font_weight");
211 font_weight = gl_util_string_to_weight (string);
213 font_italic_flag = gl_xml_get_prop_boolean (object_node, "font_italic", FALSE);
215 string = xmlGetProp (object_node, "justify");
216 just = gl_util_string_to_just (string);
219 color = gl_xml_get_prop_uint (object_node, "color", 0);
221 gl_label_object_set_font_family (GL_LABEL_OBJECT(object), font_family);
222 gl_label_object_set_font_size (GL_LABEL_OBJECT(object), font_size);
223 gl_label_object_set_font_weight (GL_LABEL_OBJECT(object), font_weight);
224 gl_label_object_set_font_italic_flag (GL_LABEL_OBJECT(object), font_italic_flag);
225 gl_label_object_set_text_color (GL_LABEL_OBJECT(object), color);
226 gl_label_object_set_text_alignment (GL_LABEL_OBJECT(object), just);
229 for (line_node = object_node->xmlChildrenNode; line_node != NULL;
230 line_node = line_node->next) {
232 if (xmlStrEqual (line_node->name, "Line")) {
234 gl_debug (DEBUG_XML, "->Line node");
237 for (text_node = line_node->xmlChildrenNode;
238 text_node != NULL; text_node = text_node->next) {
240 if (xmlStrEqual (text_node->name, "Field")) {
241 gl_debug (DEBUG_XML, "->Line->Field node");
242 node_text = g_new0 (glTextNode, 1);
243 node_text->field_flag = TRUE;
245 xmlGetProp (text_node, "name");
247 g_list_append (nodes, node_text);
248 } else if (xmlNodeIsText (text_node)) {
249 gl_debug (DEBUG_XML, "->Line->\"literal\" node");
250 node_text = g_new0 (glTextNode, 1);
251 node_text->field_flag = FALSE;
253 xmlNodeGetContent (text_node);
254 gl_debug (DEBUG_XML, "text = \"%s\"",
257 g_list_append (nodes, node_text);
259 g_warning ("Unexpected Text Line child: \"%s\"",
264 lines = g_list_append (lines, nodes);
266 } else if (!xmlNodeIsText (line_node)) {
267 g_warning ("Unexpected Text child: \"%s\"",
273 gl_label_text_set_lines (object, lines);
275 gl_text_node_lines_free (&lines);
276 g_free (font_family);
278 /* Adjust location. In 0.4.x, text was anchored at x,y */
279 gl_label_object_get_position (GL_LABEL_OBJECT(object), &x, &y);
280 gl_label_object_get_size (GL_LABEL_OBJECT(object), &w, &h);
282 case GTK_JUSTIFY_LEFT:
285 case GTK_JUSTIFY_CENTER:
287 gl_label_object_set_position (GL_LABEL_OBJECT(object), x, y);
289 case GTK_JUSTIFY_RIGHT:
291 gl_label_object_set_position (GL_LABEL_OBJECT(object), x, y);
294 /* should not happen */
298 gl_debug (DEBUG_XML, "END");
301 /*--------------------------------------------------------------------------*/
302 /* PRIVATE. Parse XML Label->Box Node Properties */
303 /*--------------------------------------------------------------------------*/
305 xml04_parse_box_props (xmlNodePtr node,
308 gdouble w, h, line_width;
309 guint line_color, fill_color;
311 gl_debug (DEBUG_XML, "START");
313 w = gl_xml_get_prop_double (node, "w", 0);
314 h = gl_xml_get_prop_double (node, "h", 0);
316 line_width = gl_xml_get_prop_double (node, "line_width", 0);
318 line_color = gl_xml_get_prop_uint (node, "line_color", 0);
319 fill_color = gl_xml_get_prop_uint (node, "fill_color", 0);
321 gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
322 gl_label_object_set_line_width (GL_LABEL_OBJECT(object), line_width);
323 gl_label_object_set_line_color (GL_LABEL_OBJECT(object), line_color);
324 gl_label_object_set_fill_color (GL_LABEL_OBJECT(object), fill_color);
326 gl_debug (DEBUG_XML, "END");
329 /*--------------------------------------------------------------------------*/
330 /* PRIVATE. Parse XML Label->Line Node Properties */
331 /*--------------------------------------------------------------------------*/
333 xml04_parse_line_props (xmlNodePtr node,
336 gdouble w, h, line_width;
339 gl_debug (DEBUG_XML, "START");
341 w = gl_xml_get_prop_double (node, "dx", 0);
342 h = gl_xml_get_prop_double (node, "dy", 0);
344 line_width = gl_xml_get_prop_double (node, "line_width", 0);
346 line_color = gl_xml_get_prop_uint (node, "line_color", 0);
348 gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
349 gl_label_object_set_line_width (GL_LABEL_OBJECT(object), line_width);
350 gl_label_object_set_line_color (GL_LABEL_OBJECT(object), line_color);
352 gl_debug (DEBUG_XML, "END");
355 /*--------------------------------------------------------------------------*/
356 /* PRIVATE. Parse XML Label->Ellipse Node Properties */
357 /*--------------------------------------------------------------------------*/
359 xml04_parse_ellipse_props (xmlNodePtr node,
360 glLabelEllipse *object)
362 gdouble w, h, line_width;
363 guint line_color, fill_color;
365 gl_debug (DEBUG_XML, "START");
367 w = gl_xml_get_prop_double (node, "w", 0);
368 h = gl_xml_get_prop_double (node, "h", 0);
370 line_width = gl_xml_get_prop_double (node, "line_width", 0);
372 line_color = gl_xml_get_prop_uint (node, "line_color", 0);
373 fill_color = gl_xml_get_prop_uint (node, "fill_color", 0);
375 gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
376 gl_label_object_set_line_width (GL_LABEL_OBJECT(object), line_width);
377 gl_label_object_set_line_color (GL_LABEL_OBJECT(object), line_color);
378 gl_label_object_set_fill_color (GL_LABEL_OBJECT(object), fill_color);
380 gl_debug (DEBUG_XML, "END");
383 /*--------------------------------------------------------------------------*/
384 /* PRIVATE. Parse XML Label->Image Node Properties */
385 /*--------------------------------------------------------------------------*/
387 xml04_parse_image_props (xmlNodePtr node,
388 glLabelImage *object)
391 glTextNode *filename;
393 gl_debug (DEBUG_XML, "START");
395 w = gl_xml_get_prop_double (node, "w", 0);
396 h = gl_xml_get_prop_double (node, "h", 0);
397 gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
399 filename = g_new0 (glTextNode, 1);
400 filename->field_flag = FALSE;
401 filename->data = xmlGetProp (node, "filename");
402 gl_label_image_set_filename (object, filename);
403 gl_text_node_free (&filename);
405 gl_debug (DEBUG_XML, "END");
408 /*--------------------------------------------------------------------------*/
409 /* PRIVATE. Parse XML Label->Barcode Node Properties */
410 /*--------------------------------------------------------------------------*/
412 xml04_parse_barcode_props (xmlNodePtr node,
413 glLabelBarcode *object)
415 glBarcodeStyle style;
420 glTextNode *text_node;
423 gl_debug (DEBUG_XML, "START");
425 color = gl_xml_get_prop_uint (node, "color", 0);
427 string = xmlGetProp (node, "style");
428 style = gl_barcode_text_to_style (string);
431 text_flag = gl_xml_get_prop_boolean (node, "text", FALSE);
432 scale = gl_xml_get_prop_double (node, "scale", 1.0);
434 scale = 0.5; /* Set to a valid value */
436 gl_label_barcode_set_props (object, style, text_flag, TRUE);
437 gl_label_object_set_line_color (GL_LABEL_OBJECT(object), color);
439 child = node->xmlChildrenNode;
440 text_node = g_new0 (glTextNode, 1);
441 if (xmlStrEqual (child->name, "Field")) {
442 text_node->field_flag = TRUE;
443 text_node->data = xmlGetProp (child, "name");
444 } else if (xmlNodeIsText (child)) {
445 text_node->field_flag = FALSE;
446 text_node->data = xmlNodeGetContent (child);
448 g_warning ("Unexpected Barcode child: \"%s\"", child->name);
450 gl_label_barcode_set_data (object, text_node);
452 gl_text_node_free (&text_node);
454 gl_debug (DEBUG_XML, "END");
457 /*--------------------------------------------------------------------------*/
458 /* PRIVATE. Parse XML merge properties tag. */
459 /*--------------------------------------------------------------------------*/
461 xml04_parse_merge_properties (xmlNodePtr node,
468 gl_debug (DEBUG_XML, "START");
470 string = xmlGetProp (node, "type");
471 merge = gl_merge_new (string);
474 string = xmlGetProp (node, "src");
475 gl_merge_set_src (merge, string);
478 gl_label_set_merge (label, merge);
480 g_object_unref (G_OBJECT(merge));
482 gl_debug (DEBUG_XML, "END");