3 * Copyright (C) 2001-2009 Jim Evins <evins@snaught.com>.
5 * This file is part of gLabels.
7 * gLabels is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU 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 * gLabels 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 General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with gLabels. If not, see <http://www.gnu.org/licenses/>.
23 #include "xml-label-04.h"
25 #include <glib/gi18n.h>
27 #include <libglabels.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 "bc-backends.h"
40 static gboolean xml04_parse_media_description (xmlNodePtr node,
42 static void xml04_parse_object (xmlNodePtr node,
43 glLabelObject *object);
44 static void xml04_parse_text_props (xmlNodePtr node,
46 static void xml04_parse_box_props (xmlNodePtr node,
48 static void xml04_parse_line_props (xmlNodePtr node,
50 static void xml04_parse_ellipse_props (xmlNodePtr node,
51 glLabelEllipse *object);
52 static void xml04_parse_image_props (xmlNodePtr node,
53 glLabelImage *object);
54 static void xml04_parse_barcode_props (xmlNodePtr node,
55 glLabelBarcode *object);
56 static void xml04_parse_merge_properties (xmlNodePtr node,
60 /****************************************************************************/
61 /* PRIVATE. Parse xml doc structure and create label. */
62 /****************************************************************************/
63 glLabel *gl_xml_label_04_parse (xmlNodePtr root,
64 glXMLLabelStatus *status)
71 gl_debug (DEBUG_XML, "START");
73 *status = XML_LABEL_OK;
75 if (!xmlStrEqual (root->name, (xmlChar *)"Label")) {
76 g_message ("Bad root node = \"%s\"", root->name);
77 *status = XML_LABEL_ERROR_OPEN_PARSE;
81 label = GL_LABEL (gl_label_new ());
83 rotate_flag = lgl_xml_get_prop_boolean (root, "rotate", FALSE);
84 gl_label_set_rotate_flag (label, rotate_flag, FALSE);
86 for (node = root->xmlChildrenNode; node != NULL; node = node->next) {
88 gl_debug (DEBUG_XML, "node name = \"%s\"", node->name);
90 if (!xmlNodeIsText (node)) {
91 if (xmlStrEqual (node->name, (xmlChar *)"Media_Type")) {
92 if (!xml04_parse_media_description (node, label)) {
93 *status = XML_LABEL_UNKNOWN_MEDIA;
95 } else if (xmlStrEqual (node->name, (xmlChar *)"Text")) {
96 object = gl_label_text_new (label, FALSE);
97 xml04_parse_object (node, GL_LABEL_OBJECT(object));
98 xml04_parse_text_props (node, GL_LABEL_TEXT(object));
99 } else if (xmlStrEqual (node->name, (xmlChar *)"Box")) {
100 object = gl_label_box_new (label, FALSE);
101 xml04_parse_object (node, GL_LABEL_OBJECT(object));
102 xml04_parse_box_props (node, GL_LABEL_BOX(object));
103 } else if (xmlStrEqual (node->name, (xmlChar *)"Line")) {
104 object = gl_label_line_new (label, FALSE);
105 xml04_parse_object (node, GL_LABEL_OBJECT(object));
106 xml04_parse_line_props (node, GL_LABEL_LINE(object));
107 } else if (xmlStrEqual (node->name, (xmlChar *)"Ellipse")) {
108 object = gl_label_ellipse_new (label, FALSE);
109 xml04_parse_object (node, GL_LABEL_OBJECT(object));
110 xml04_parse_ellipse_props (node,
111 GL_LABEL_ELLIPSE(object));
112 } else if (xmlStrEqual (node->name, (xmlChar *)"Image")) {
113 object = gl_label_image_new (label, FALSE);
114 xml04_parse_object (node, GL_LABEL_OBJECT(object));
115 xml04_parse_image_props (node, GL_LABEL_IMAGE(object));
116 } else if (xmlStrEqual (node->name, (xmlChar *)"Barcode")) {
117 object = gl_label_barcode_new (label, FALSE);
118 xml04_parse_object (node, GL_LABEL_OBJECT(object));
119 xml04_parse_barcode_props (node,
120 GL_LABEL_BARCODE(object));
121 } else if (xmlStrEqual (node->name, (xmlChar *)"Merge_Properties")) {
122 xml04_parse_merge_properties (node, label);
124 g_message ("bad node = \"%s\"", node->name);
129 gl_debug (DEBUG_XML, "END");
135 /*--------------------------------------------------------------------------*/
136 /* PRIVATE. Parse Media Description node. */
137 /*--------------------------------------------------------------------------*/
139 xml04_parse_media_description (xmlNodePtr node,
142 xmlChar *template_name;
143 lglTemplate *template;
146 gl_debug (DEBUG_XML, "START");
148 template_name = xmlNodeGetContent (node);
150 template = lgl_db_lookup_template_from_name ((gchar *)template_name);
151 if (template == NULL) {
152 g_message ("Undefined template \"%s\"", template_name);
154 template = lgl_db_lookup_template_from_name (NULL);
160 gl_label_set_template (label, template, FALSE);
162 lgl_template_free (template);
163 xmlFree (template_name);
165 gl_debug (DEBUG_XML, "END");
171 /*--------------------------------------------------------------------------*/
172 /* PRIVATE. Parse XML Object Node */
173 /*--------------------------------------------------------------------------*/
175 xml04_parse_object (xmlNodePtr object_node,
176 glLabelObject *object)
180 gl_debug (DEBUG_XML, "START");
182 x = lgl_xml_get_prop_double (object_node, "x", 0);
183 y = lgl_xml_get_prop_double (object_node, "y", 0);
185 gl_label_object_set_position (object, x, y, FALSE);
187 gl_debug (DEBUG_XML, "END");
191 /*--------------------------------------------------------------------------*/
192 /* PRIVATE. Parse XML Label->Text Node Properties */
193 /*--------------------------------------------------------------------------*/
195 xml04_parse_text_props (xmlNodePtr object_node,
198 xmlChar *font_family;
200 PangoWeight font_weight;
201 gboolean font_italic_flag;
202 glColorNode *color_node;
203 PangoAlignment align;
204 xmlNodePtr line_node, text_node;
205 glTextNode *node_text;
206 GList *nodes, *lines;
210 gl_debug (DEBUG_XML, "START");
212 font_family = xmlGetProp (object_node, (xmlChar *)"font_family");
213 font_size = lgl_xml_get_prop_double (object_node, "font_size", 0);
214 string = xmlGetProp (object_node, (xmlChar *)"font_weight");
215 font_weight = gl_str_util_string_to_weight ((gchar *)string);
217 font_italic_flag = lgl_xml_get_prop_boolean (object_node, "font_italic", FALSE);
219 string = xmlGetProp (object_node, (xmlChar *)"justify");
220 align = gl_str_util_string_to_align ((gchar *)string);
223 color_node = gl_color_node_new_default ();
224 color_node->color = lgl_xml_get_prop_uint (object_node, "color", 0);
226 gl_label_object_set_font_family (GL_LABEL_OBJECT(object), (gchar *)font_family, FALSE);
227 gl_label_object_set_font_size (GL_LABEL_OBJECT(object), font_size, FALSE);
228 gl_label_object_set_font_weight (GL_LABEL_OBJECT(object), font_weight, FALSE);
229 gl_label_object_set_font_italic_flag (GL_LABEL_OBJECT(object), font_italic_flag, FALSE);
230 gl_label_object_set_text_color (GL_LABEL_OBJECT(object), color_node, FALSE);
231 gl_label_object_set_text_alignment (GL_LABEL_OBJECT(object), align, FALSE);
233 gl_color_node_free (&color_node);
236 for (line_node = object_node->xmlChildrenNode; line_node != NULL;
237 line_node = line_node->next) {
239 if (xmlStrEqual (line_node->name, (xmlChar *)"Line")) {
241 gl_debug (DEBUG_XML, "->Line node");
244 for (text_node = line_node->xmlChildrenNode;
245 text_node != NULL; text_node = text_node->next) {
247 if (xmlStrEqual (text_node->name, (xmlChar *)"Field")) {
248 gl_debug (DEBUG_XML, "->Line->Field node");
249 node_text = g_new0 (glTextNode, 1);
250 node_text->field_flag = TRUE;
252 (gchar *)xmlGetProp (text_node, (xmlChar *)"name");
254 g_list_append (nodes, node_text);
255 } else if (xmlNodeIsText (text_node)) {
256 gl_debug (DEBUG_XML, "->Line->\"literal\" node");
257 node_text = g_new0 (glTextNode, 1);
258 node_text->field_flag = FALSE;
260 (gchar *)xmlNodeGetContent (text_node);
261 gl_debug (DEBUG_XML, "text = \"%s\"",
264 g_list_append (nodes, node_text);
266 g_message ("Unexpected Text Line child: \"%s\"",
271 lines = g_list_append (lines, nodes);
273 } else if (!xmlNodeIsText (line_node)) {
274 g_message ("Unexpected Text child: \"%s\"",
280 gl_label_text_set_lines (object, lines, FALSE);
282 gl_text_node_lines_free (&lines);
283 xmlFree (font_family);
285 /* Adjust location. In 0.4.x, text was anchored at x,y */
286 gl_label_object_get_position (GL_LABEL_OBJECT(object), &x, &y);
287 gl_label_object_get_size (GL_LABEL_OBJECT(object), &w, &h);
289 case PANGO_ALIGN_LEFT:
292 case PANGO_ALIGN_CENTER:
294 gl_label_object_set_position (GL_LABEL_OBJECT(object), x, y, FALSE);
296 case PANGO_ALIGN_RIGHT:
298 gl_label_object_set_position (GL_LABEL_OBJECT(object), x, y, FALSE);
301 /* should not happen */
305 gl_debug (DEBUG_XML, "END");
309 /*--------------------------------------------------------------------------*/
310 /* PRIVATE. Parse XML Label->Box Node Properties */
311 /*--------------------------------------------------------------------------*/
313 xml04_parse_box_props (xmlNodePtr node,
316 gdouble w, h, line_width;
317 glColorNode *line_color_node;
318 glColorNode *fill_color_node;
320 gl_debug (DEBUG_XML, "START");
322 w = lgl_xml_get_prop_double (node, "w", 0);
323 h = lgl_xml_get_prop_double (node, "h", 0);
325 line_width = lgl_xml_get_prop_double (node, "line_width", 0);
327 line_color_node = gl_color_node_new_default ();
328 line_color_node->color = lgl_xml_get_prop_uint (node, "line_color", 0);
330 fill_color_node = gl_color_node_new_default ();
331 fill_color_node->color = lgl_xml_get_prop_uint (node, "fill_color", 0);
333 gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h, FALSE);
334 gl_label_object_set_line_width (GL_LABEL_OBJECT(object), line_width, FALSE);
335 gl_label_object_set_line_color (GL_LABEL_OBJECT(object), line_color_node, FALSE);
336 gl_label_object_set_fill_color (GL_LABEL_OBJECT(object), fill_color_node, FALSE);
338 gl_color_node_free (&line_color_node);
339 gl_color_node_free (&fill_color_node);
340 gl_debug (DEBUG_XML, "END");
344 /*--------------------------------------------------------------------------*/
345 /* PRIVATE. Parse XML Label->Line Node Properties */
346 /*--------------------------------------------------------------------------*/
348 xml04_parse_line_props (xmlNodePtr node,
351 gdouble w, h, line_width;
352 glColorNode *line_color_node;
354 gl_debug (DEBUG_XML, "START");
356 w = lgl_xml_get_prop_double (node, "dx", 0);
357 h = lgl_xml_get_prop_double (node, "dy", 0);
359 line_width = lgl_xml_get_prop_double (node, "line_width", 0);
361 line_color_node = gl_color_node_new_default ();
362 line_color_node->color = lgl_xml_get_prop_uint (node, "line_color", 0);
364 gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h, FALSE);
365 gl_label_object_set_line_width (GL_LABEL_OBJECT(object), line_width, FALSE);
366 gl_label_object_set_line_color (GL_LABEL_OBJECT(object), line_color_node, FALSE);
368 gl_color_node_free (&line_color_node);
370 gl_debug (DEBUG_XML, "END");
374 /*--------------------------------------------------------------------------*/
375 /* PRIVATE. Parse XML Label->Ellipse Node Properties */
376 /*--------------------------------------------------------------------------*/
378 xml04_parse_ellipse_props (xmlNodePtr node,
379 glLabelEllipse *object)
381 gdouble w, h, line_width;
382 glColorNode *line_color_node;
383 glColorNode *fill_color_node;
385 gl_debug (DEBUG_XML, "START");
387 w = lgl_xml_get_prop_double (node, "w", 0);
388 h = lgl_xml_get_prop_double (node, "h", 0);
390 line_width = lgl_xml_get_prop_double (node, "line_width", 0);
392 line_color_node = gl_color_node_new_default ();
393 line_color_node->color = lgl_xml_get_prop_uint (node, "line_color", 0);
395 fill_color_node = gl_color_node_new_default ();
396 fill_color_node->color = lgl_xml_get_prop_uint (node, "fill_color", 0);
398 gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h, FALSE);
399 gl_label_object_set_line_width (GL_LABEL_OBJECT(object), line_width, FALSE);
400 gl_label_object_set_line_color (GL_LABEL_OBJECT(object), line_color_node, FALSE);
401 gl_label_object_set_fill_color (GL_LABEL_OBJECT(object), fill_color_node, FALSE);
403 gl_color_node_free (&line_color_node);
404 gl_color_node_free (&fill_color_node);
405 gl_debug (DEBUG_XML, "END");
409 /*--------------------------------------------------------------------------*/
410 /* PRIVATE. Parse XML Label->Image Node Properties */
411 /*--------------------------------------------------------------------------*/
413 xml04_parse_image_props (xmlNodePtr node,
414 glLabelImage *object)
417 glTextNode *filename;
419 gl_debug (DEBUG_XML, "START");
421 filename = g_new0 (glTextNode, 1);
422 filename->field_flag = FALSE;
423 filename->data = (gchar *)xmlGetProp (node, (xmlChar *)"filename");
424 gl_label_image_set_filename (object, filename, FALSE);
425 gl_text_node_free (&filename);
427 w = lgl_xml_get_prop_double (node, "w", 0);
428 h = lgl_xml_get_prop_double (node, "h", 0);
429 gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h, FALSE);
431 gl_debug (DEBUG_XML, "END");
435 /*--------------------------------------------------------------------------*/
436 /* PRIVATE. Parse XML Label->Barcode Node Properties */
437 /*--------------------------------------------------------------------------*/
439 xml04_parse_barcode_props (xmlNodePtr node,
440 glLabelBarcode *object)
443 const gchar *backend_id;
444 glLabelBarcodeStyle *style;
445 glColorNode *color_node;
447 glTextNode *text_node;
449 gl_debug (DEBUG_XML, "START");
451 color_node = gl_color_node_new_default ();
452 color_node->color = lgl_xml_get_prop_uint (node, "color", 0);
454 style = gl_label_barcode_style_new ();
456 id = lgl_xml_get_prop_string (node, "style", "POSTNET");
457 backend_id = gl_barcode_backends_guess_backend_id (id);
458 gl_label_barcode_style_set_backend_id (style, backend_id);
459 gl_label_barcode_style_set_style_id (style, id);
461 style->text_flag = lgl_xml_get_prop_boolean (node, "text", FALSE);
462 style->checksum_flag = TRUE;
463 style->format_digits = 0;
465 gl_label_barcode_set_style (object, style, FALSE);
467 gl_label_object_set_line_color (GL_LABEL_OBJECT(object), color_node, FALSE);
469 child = node->xmlChildrenNode;
470 text_node = g_new0 (glTextNode, 1);
471 if (xmlStrEqual (child->name, (xmlChar *)"Field")) {
472 text_node->field_flag = TRUE;
473 text_node->data = (gchar *)xmlGetProp (child, (xmlChar *)"name");
474 } else if (xmlNodeIsText (child)) {
475 text_node->field_flag = FALSE;
476 text_node->data = (gchar *)xmlNodeGetContent (child);
478 g_message ("Unexpected Barcode child: \"%s\"", child->name);
480 gl_label_barcode_set_data (object, text_node, FALSE);
482 gl_color_node_free (&color_node);
483 gl_text_node_free (&text_node);
485 gl_label_barcode_style_free (style);
487 gl_debug (DEBUG_XML, "END");
491 /*--------------------------------------------------------------------------*/
492 /* PRIVATE. Parse XML merge properties tag. */
493 /*--------------------------------------------------------------------------*/
495 xml04_parse_merge_properties (xmlNodePtr node,
501 gl_debug (DEBUG_XML, "START");
503 string = xmlGetProp (node, (xmlChar *)"type");
504 merge = gl_merge_new ((gchar *)string);
507 string = xmlGetProp (node, (xmlChar *)"src");
508 gl_merge_set_src (merge, (gchar *)string);
511 gl_label_set_merge (label, merge, FALSE);
513 g_object_unref (G_OBJECT(merge));
515 gl_debug (DEBUG_XML, "END");
521 * Local Variables: -- emacs
523 * c-basic-offset: 8 -- emacs
524 * tab-width: 8 -- emacs
525 * indent-tabs-mode: nil -- emacs