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/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"
39 static gboolean xml04_parse_media_description (xmlNodePtr node,
41 static void xml04_parse_object (xmlNodePtr node,
42 glLabelObject *object);
43 static void xml04_parse_text_props (xmlNodePtr node,
45 static void xml04_parse_box_props (xmlNodePtr node,
47 static void xml04_parse_line_props (xmlNodePtr node,
49 static void xml04_parse_ellipse_props (xmlNodePtr node,
50 glLabelEllipse *object);
51 static void xml04_parse_image_props (xmlNodePtr node,
52 glLabelImage *object);
53 static void xml04_parse_barcode_props (xmlNodePtr node,
54 glLabelBarcode *object);
55 static void xml04_parse_merge_properties (xmlNodePtr node,
59 /****************************************************************************/
60 /* PRIVATE. Parse xml doc structure and create label. */
61 /****************************************************************************/
62 glLabel *gl_xml_label_04_parse (xmlNodePtr root,
63 glXMLLabelStatus *status)
70 gl_debug (DEBUG_XML, "START");
72 *status = XML_LABEL_OK;
74 if (!xmlStrEqual (root->name, (xmlChar *)"Label")) {
75 g_message (_("Bad root node = \"%s\""), root->name);
76 *status = XML_LABEL_ERROR_OPEN_PARSE;
80 label = GL_LABEL (gl_label_new ());
82 rotate_flag = lgl_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, (xmlChar *)"Media_Type")) {
91 if (!xml04_parse_media_description (node, label)) {
92 *status = XML_LABEL_UNKNOWN_MEDIA;
94 } else if (xmlStrEqual (node->name, (xmlChar *)"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, (xmlChar *)"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, (xmlChar *)"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, (xmlChar *)"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, (xmlChar *)"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, (xmlChar *)"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, (xmlChar *)"Merge_Properties")) {
121 xml04_parse_merge_properties (node, label);
123 g_message (_("bad node = \"%s\""), node->name);
128 gl_debug (DEBUG_XML, "END");
134 /*--------------------------------------------------------------------------*/
135 /* PRIVATE. Parse Media Description node. */
136 /*--------------------------------------------------------------------------*/
138 xml04_parse_media_description (xmlNodePtr node,
141 xmlChar *template_name;
142 lglTemplate *template;
145 gl_debug (DEBUG_XML, "START");
147 template_name = xmlNodeGetContent (node);
149 template = lgl_db_lookup_template_from_name ((gchar *)template_name);
150 if (template == NULL) {
151 g_message ("Undefined template \"%s\"", template_name);
153 template = lgl_db_lookup_template_from_name (NULL);
159 gl_label_set_template (label, template);
161 lgl_template_free (template);
162 xmlFree (template_name);
164 gl_debug (DEBUG_XML, "END");
170 /*--------------------------------------------------------------------------*/
171 /* PRIVATE. Parse XML Object Node */
172 /*--------------------------------------------------------------------------*/
174 xml04_parse_object (xmlNodePtr object_node,
175 glLabelObject *object)
179 gl_debug (DEBUG_XML, "START");
181 x = lgl_xml_get_prop_double (object_node, "x", 0);
182 y = lgl_xml_get_prop_double (object_node, "y", 0);
184 gl_label_object_set_position (object, x, y);
186 gl_debug (DEBUG_XML, "END");
190 /*--------------------------------------------------------------------------*/
191 /* PRIVATE. Parse XML Label->Text Node Properties */
192 /*--------------------------------------------------------------------------*/
194 xml04_parse_text_props (xmlNodePtr object_node,
197 xmlChar *font_family;
199 PangoWeight font_weight;
200 gboolean font_italic_flag;
201 glColorNode *color_node;
202 PangoAlignment align;
203 xmlNodePtr line_node, text_node;
204 glTextNode *node_text;
205 GList *nodes, *lines;
209 gl_debug (DEBUG_XML, "START");
211 font_family = xmlGetProp (object_node, (xmlChar *)"font_family");
212 font_size = lgl_xml_get_prop_double (object_node, "font_size", 0);
213 string = xmlGetProp (object_node, (xmlChar *)"font_weight");
214 font_weight = gl_str_util_string_to_weight ((gchar *)string);
216 font_italic_flag = lgl_xml_get_prop_boolean (object_node, "font_italic", FALSE);
218 string = xmlGetProp (object_node, (xmlChar *)"justify");
219 align = gl_str_util_string_to_align ((gchar *)string);
222 color_node = gl_color_node_new_default ();
223 color_node->color = lgl_xml_get_prop_uint (object_node, "color", 0);
225 gl_label_object_set_font_family (GL_LABEL_OBJECT(object), (gchar *)font_family);
226 gl_label_object_set_font_size (GL_LABEL_OBJECT(object), font_size);
227 gl_label_object_set_font_weight (GL_LABEL_OBJECT(object), font_weight);
228 gl_label_object_set_font_italic_flag (GL_LABEL_OBJECT(object), font_italic_flag);
229 gl_label_object_set_text_color (GL_LABEL_OBJECT(object), color_node);
230 gl_label_object_set_text_alignment (GL_LABEL_OBJECT(object), align);
232 gl_color_node_free (&color_node);
235 for (line_node = object_node->xmlChildrenNode; line_node != NULL;
236 line_node = line_node->next) {
238 if (xmlStrEqual (line_node->name, (xmlChar *)"Line")) {
240 gl_debug (DEBUG_XML, "->Line node");
243 for (text_node = line_node->xmlChildrenNode;
244 text_node != NULL; text_node = text_node->next) {
246 if (xmlStrEqual (text_node->name, (xmlChar *)"Field")) {
247 gl_debug (DEBUG_XML, "->Line->Field node");
248 node_text = g_new0 (glTextNode, 1);
249 node_text->field_flag = TRUE;
251 (gchar *)xmlGetProp (text_node, (xmlChar *)"name");
253 g_list_append (nodes, node_text);
254 } else if (xmlNodeIsText (text_node)) {
255 gl_debug (DEBUG_XML, "->Line->\"literal\" node");
256 node_text = g_new0 (glTextNode, 1);
257 node_text->field_flag = FALSE;
259 (gchar *)xmlNodeGetContent (text_node);
260 gl_debug (DEBUG_XML, "text = \"%s\"",
263 g_list_append (nodes, node_text);
265 g_message ("Unexpected Text Line child: \"%s\"",
270 lines = g_list_append (lines, nodes);
272 } else if (!xmlNodeIsText (line_node)) {
273 g_message ("Unexpected Text child: \"%s\"",
279 gl_label_text_set_lines (object, lines);
281 gl_text_node_lines_free (&lines);
282 xmlFree (font_family);
284 /* Adjust location. In 0.4.x, text was anchored at x,y */
285 gl_label_object_get_position (GL_LABEL_OBJECT(object), &x, &y);
286 gl_label_object_get_size (GL_LABEL_OBJECT(object), &w, &h);
288 case PANGO_ALIGN_LEFT:
291 case PANGO_ALIGN_CENTER:
293 gl_label_object_set_position (GL_LABEL_OBJECT(object), x, y);
295 case PANGO_ALIGN_RIGHT:
297 gl_label_object_set_position (GL_LABEL_OBJECT(object), x, y);
300 /* should not happen */
304 gl_debug (DEBUG_XML, "END");
308 /*--------------------------------------------------------------------------*/
309 /* PRIVATE. Parse XML Label->Box Node Properties */
310 /*--------------------------------------------------------------------------*/
312 xml04_parse_box_props (xmlNodePtr node,
315 gdouble w, h, line_width;
316 glColorNode *line_color_node;
317 glColorNode *fill_color_node;
319 gl_debug (DEBUG_XML, "START");
321 w = lgl_xml_get_prop_double (node, "w", 0);
322 h = lgl_xml_get_prop_double (node, "h", 0);
324 line_width = lgl_xml_get_prop_double (node, "line_width", 0);
326 line_color_node = gl_color_node_new_default ();
327 line_color_node->color = lgl_xml_get_prop_uint (node, "line_color", 0);
329 fill_color_node = gl_color_node_new_default ();
330 fill_color_node->color = lgl_xml_get_prop_uint (node, "fill_color", 0);
332 gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
333 gl_label_object_set_line_width (GL_LABEL_OBJECT(object), line_width);
334 gl_label_object_set_line_color (GL_LABEL_OBJECT(object), line_color_node);
335 gl_label_object_set_fill_color (GL_LABEL_OBJECT(object), fill_color_node);
337 gl_color_node_free (&line_color_node);
338 gl_color_node_free (&fill_color_node);
339 gl_debug (DEBUG_XML, "END");
343 /*--------------------------------------------------------------------------*/
344 /* PRIVATE. Parse XML Label->Line Node Properties */
345 /*--------------------------------------------------------------------------*/
347 xml04_parse_line_props (xmlNodePtr node,
350 gdouble w, h, line_width;
351 glColorNode *line_color_node;
353 gl_debug (DEBUG_XML, "START");
355 w = lgl_xml_get_prop_double (node, "dx", 0);
356 h = lgl_xml_get_prop_double (node, "dy", 0);
358 line_width = lgl_xml_get_prop_double (node, "line_width", 0);
360 line_color_node = gl_color_node_new_default ();
361 line_color_node->color = lgl_xml_get_prop_uint (node, "line_color", 0);
363 gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
364 gl_label_object_set_line_width (GL_LABEL_OBJECT(object), line_width);
365 gl_label_object_set_line_color (GL_LABEL_OBJECT(object), line_color_node);
367 gl_color_node_free (&line_color_node);
369 gl_debug (DEBUG_XML, "END");
373 /*--------------------------------------------------------------------------*/
374 /* PRIVATE. Parse XML Label->Ellipse Node Properties */
375 /*--------------------------------------------------------------------------*/
377 xml04_parse_ellipse_props (xmlNodePtr node,
378 glLabelEllipse *object)
380 gdouble w, h, line_width;
381 glColorNode *line_color_node;
382 glColorNode *fill_color_node;
384 gl_debug (DEBUG_XML, "START");
386 w = lgl_xml_get_prop_double (node, "w", 0);
387 h = lgl_xml_get_prop_double (node, "h", 0);
389 line_width = lgl_xml_get_prop_double (node, "line_width", 0);
391 line_color_node = gl_color_node_new_default ();
392 line_color_node->color = lgl_xml_get_prop_uint (node, "line_color", 0);
394 fill_color_node = gl_color_node_new_default ();
395 fill_color_node->color = lgl_xml_get_prop_uint (node, "fill_color", 0);
397 gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
398 gl_label_object_set_line_width (GL_LABEL_OBJECT(object), line_width);
399 gl_label_object_set_line_color (GL_LABEL_OBJECT(object), line_color_node);
400 gl_label_object_set_fill_color (GL_LABEL_OBJECT(object), fill_color_node);
402 gl_color_node_free (&line_color_node);
403 gl_color_node_free (&fill_color_node);
404 gl_debug (DEBUG_XML, "END");
408 /*--------------------------------------------------------------------------*/
409 /* PRIVATE. Parse XML Label->Image Node Properties */
410 /*--------------------------------------------------------------------------*/
412 xml04_parse_image_props (xmlNodePtr node,
413 glLabelImage *object)
416 glTextNode *filename;
418 gl_debug (DEBUG_XML, "START");
420 filename = g_new0 (glTextNode, 1);
421 filename->field_flag = FALSE;
422 filename->data = (gchar *)xmlGetProp (node, (xmlChar *)"filename");
423 gl_label_image_set_filename (object, filename);
424 gl_text_node_free (&filename);
426 w = lgl_xml_get_prop_double (node, "w", 0);
427 h = lgl_xml_get_prop_double (node, "h", 0);
428 gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
430 gl_debug (DEBUG_XML, "END");
434 /*--------------------------------------------------------------------------*/
435 /* PRIVATE. Parse XML Label->Barcode Node Properties */
436 /*--------------------------------------------------------------------------*/
438 xml04_parse_barcode_props (xmlNodePtr node,
439 glLabelBarcode *object)
443 glColorNode *color_node;
446 glTextNode *text_node;
448 gl_debug (DEBUG_XML, "START");
450 color_node = gl_color_node_new_default ();
451 color_node->color = lgl_xml_get_prop_uint (node, "color", 0);
453 id = xmlGetProp (node, (xmlChar *)"style");
455 text_flag = lgl_xml_get_prop_boolean (node, "text", FALSE);
456 scale = lgl_xml_get_prop_double (node, "scale", 1.0);
458 scale = 0.5; /* Set to a valid value */
460 gl_label_barcode_set_props (object, (gchar *)id, text_flag, TRUE, 0);
461 gl_label_object_set_line_color (GL_LABEL_OBJECT(object), color_node);
463 child = node->xmlChildrenNode;
464 text_node = g_new0 (glTextNode, 1);
465 if (xmlStrEqual (child->name, (xmlChar *)"Field")) {
466 text_node->field_flag = TRUE;
467 text_node->data = (gchar *)xmlGetProp (child, (xmlChar *)"name");
468 } else if (xmlNodeIsText (child)) {
469 text_node->field_flag = FALSE;
470 text_node->data = (gchar *)xmlNodeGetContent (child);
472 g_message ("Unexpected Barcode child: \"%s\"", child->name);
474 gl_label_barcode_set_data (object, text_node);
476 gl_color_node_free (&color_node);
477 gl_text_node_free (&text_node);
480 gl_debug (DEBUG_XML, "END");
484 /*--------------------------------------------------------------------------*/
485 /* PRIVATE. Parse XML merge properties tag. */
486 /*--------------------------------------------------------------------------*/
488 xml04_parse_merge_properties (xmlNodePtr node,
494 gl_debug (DEBUG_XML, "START");
496 string = xmlGetProp (node, (xmlChar *)"type");
497 merge = gl_merge_new ((gchar *)string);
500 string = xmlGetProp (node, (xmlChar *)"src");
501 gl_merge_set_src (merge, (gchar *)string);
504 gl_label_set_merge (label, merge);
506 g_object_unref (G_OBJECT(merge));
508 gl_debug (DEBUG_XML, "END");
514 * Local Variables: -- emacs
516 * c-basic-offset: 8 -- emacs
517 * tab-width: 8 -- emacs
518 * indent-tabs-mode: nil -- emacs