1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
4 * (GLABELS) Label and Business Card Creation program for GNOME
6 * xml-label-04.c: GLabels xml label compat module
8 * Copyright (C) 2001-2002 Jim Evins <evins@snaught.com>.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program 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
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "xml-label-04.h"
29 #include <glib/gi18n.h>
31 #include "label-text.h"
32 #include "label-box.h"
33 #include "label-line.h"
34 #include "label-ellipse.h"
35 #include "label-image.h"
36 #include "label-barcode.h"
37 #include <libglabels/db.h>
38 #include <libglabels/xml.h>
43 static gboolean xml04_parse_media_description (xmlNodePtr node,
45 static void xml04_parse_object (xmlNodePtr node,
46 glLabelObject *object);
47 static void xml04_parse_text_props (xmlNodePtr node,
49 static void xml04_parse_box_props (xmlNodePtr node,
51 static void xml04_parse_line_props (xmlNodePtr node,
53 static void xml04_parse_ellipse_props (xmlNodePtr node,
54 glLabelEllipse *object);
55 static void xml04_parse_image_props (xmlNodePtr node,
56 glLabelImage *object);
57 static void xml04_parse_barcode_props (xmlNodePtr node,
58 glLabelBarcode *object);
59 static void xml04_parse_merge_properties (xmlNodePtr node,
63 /****************************************************************************/
64 /* PRIVATE. Parse xml doc structure and create label. */
65 /****************************************************************************/
66 glLabel *gl_xml_label_04_parse (xmlNodePtr root,
67 glXMLLabelStatus *status)
74 gl_debug (DEBUG_XML, "START");
76 *status = XML_LABEL_OK;
78 if (!xmlStrEqual (root->name, (xmlChar *)"Label")) {
79 g_message (_("Bad root node = \"%s\""), root->name);
80 *status = XML_LABEL_ERROR_OPEN_PARSE;
84 label = GL_LABEL (gl_label_new ());
86 rotate_flag = lgl_xml_get_prop_boolean (root, "rotate", FALSE);
87 gl_label_set_rotate_flag (label, rotate_flag);
89 for (node = root->xmlChildrenNode; node != NULL; node = node->next) {
91 gl_debug (DEBUG_XML, "node name = \"%s\"", node->name);
93 if (!xmlNodeIsText (node)) {
94 if (xmlStrEqual (node->name, (xmlChar *)"Media_Type")) {
95 if (!xml04_parse_media_description (node, label)) {
96 *status = XML_LABEL_UNKNOWN_MEDIA;
98 } else if (xmlStrEqual (node->name, (xmlChar *)"Text")) {
99 object = gl_label_text_new (label);
100 xml04_parse_object (node, GL_LABEL_OBJECT(object));
101 xml04_parse_text_props (node, GL_LABEL_TEXT(object));
102 } else if (xmlStrEqual (node->name, (xmlChar *)"Box")) {
103 object = gl_label_box_new (label);
104 xml04_parse_object (node, GL_LABEL_OBJECT(object));
105 xml04_parse_box_props (node, GL_LABEL_BOX(object));
106 } else if (xmlStrEqual (node->name, (xmlChar *)"Line")) {
107 object = gl_label_line_new (label);
108 xml04_parse_object (node, GL_LABEL_OBJECT(object));
109 xml04_parse_line_props (node, GL_LABEL_LINE(object));
110 } else if (xmlStrEqual (node->name, (xmlChar *)"Ellipse")) {
111 object = gl_label_ellipse_new (label);
112 xml04_parse_object (node, GL_LABEL_OBJECT(object));
113 xml04_parse_ellipse_props (node,
114 GL_LABEL_ELLIPSE(object));
115 } else if (xmlStrEqual (node->name, (xmlChar *)"Image")) {
116 object = gl_label_image_new (label);
117 xml04_parse_object (node, GL_LABEL_OBJECT(object));
118 xml04_parse_image_props (node, GL_LABEL_IMAGE(object));
119 } else if (xmlStrEqual (node->name, (xmlChar *)"Barcode")) {
120 object = gl_label_barcode_new (label);
121 xml04_parse_object (node, GL_LABEL_OBJECT(object));
122 xml04_parse_barcode_props (node,
123 GL_LABEL_BARCODE(object));
124 } else if (xmlStrEqual (node->name, (xmlChar *)"Merge_Properties")) {
125 xml04_parse_merge_properties (node, label);
127 g_message (_("bad node = \"%s\""), node->name);
132 gl_debug (DEBUG_XML, "END");
137 /*--------------------------------------------------------------------------*/
138 /* PRIVATE. Parse Media Description node. */
139 /*--------------------------------------------------------------------------*/
141 xml04_parse_media_description (xmlNodePtr node,
144 xmlChar *template_name;
145 lglTemplate *template;
148 gl_debug (DEBUG_XML, "START");
150 template_name = xmlNodeGetContent (node);
152 template = lgl_db_lookup_template_from_name ((gchar *)template_name);
153 if (template == NULL) {
154 g_message ("Undefined template \"%s\"", template_name);
156 template = lgl_db_lookup_template_from_name (NULL);
162 gl_label_set_template (label, template);
164 lgl_template_free (template);
165 xmlFree (template_name);
167 gl_debug (DEBUG_XML, "END");
172 /*--------------------------------------------------------------------------*/
173 /* PRIVATE. Parse XML Object Node */
174 /*--------------------------------------------------------------------------*/
176 xml04_parse_object (xmlNodePtr object_node,
177 glLabelObject *object)
181 gl_debug (DEBUG_XML, "START");
183 x = lgl_xml_get_prop_double (object_node, "x", 0);
184 y = lgl_xml_get_prop_double (object_node, "y", 0);
186 gl_label_object_set_position (object, x, y);
188 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_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_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);
227 gl_label_object_set_font_size (GL_LABEL_OBJECT(object), font_size);
228 gl_label_object_set_font_weight (GL_LABEL_OBJECT(object), font_weight);
229 gl_label_object_set_font_italic_flag (GL_LABEL_OBJECT(object), font_italic_flag);
230 gl_label_object_set_text_color (GL_LABEL_OBJECT(object), color_node);
231 gl_label_object_set_text_alignment (GL_LABEL_OBJECT(object), align);
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);
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);
296 case PANGO_ALIGN_RIGHT:
298 gl_label_object_set_position (GL_LABEL_OBJECT(object), x, y);
301 /* should not happen */
305 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");
342 /*--------------------------------------------------------------------------*/
343 /* PRIVATE. Parse XML Label->Line Node Properties */
344 /*--------------------------------------------------------------------------*/
346 xml04_parse_line_props (xmlNodePtr node,
349 gdouble w, h, line_width;
350 glColorNode *line_color_node;
352 gl_debug (DEBUG_XML, "START");
354 w = lgl_xml_get_prop_double (node, "dx", 0);
355 h = lgl_xml_get_prop_double (node, "dy", 0);
357 line_width = lgl_xml_get_prop_double (node, "line_width", 0);
359 line_color_node = gl_color_node_new_default ();
360 line_color_node->color = lgl_xml_get_prop_uint (node, "line_color", 0);
362 gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
363 gl_label_object_set_line_width (GL_LABEL_OBJECT(object), line_width);
364 gl_label_object_set_line_color (GL_LABEL_OBJECT(object), line_color_node);
366 gl_color_node_free (&line_color_node);
368 gl_debug (DEBUG_XML, "END");
371 /*--------------------------------------------------------------------------*/
372 /* PRIVATE. Parse XML Label->Ellipse Node Properties */
373 /*--------------------------------------------------------------------------*/
375 xml04_parse_ellipse_props (xmlNodePtr node,
376 glLabelEllipse *object)
378 gdouble w, h, line_width;
379 glColorNode *line_color_node;
380 glColorNode *fill_color_node;
382 gl_debug (DEBUG_XML, "START");
384 w = lgl_xml_get_prop_double (node, "w", 0);
385 h = lgl_xml_get_prop_double (node, "h", 0);
387 line_width = lgl_xml_get_prop_double (node, "line_width", 0);
389 line_color_node = gl_color_node_new_default ();
390 line_color_node->color = lgl_xml_get_prop_uint (node, "line_color", 0);
392 fill_color_node = gl_color_node_new_default ();
393 fill_color_node->color = lgl_xml_get_prop_uint (node, "fill_color", 0);
395 gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
396 gl_label_object_set_line_width (GL_LABEL_OBJECT(object), line_width);
397 gl_label_object_set_line_color (GL_LABEL_OBJECT(object), line_color_node);
398 gl_label_object_set_fill_color (GL_LABEL_OBJECT(object), fill_color_node);
400 gl_color_node_free (&line_color_node);
401 gl_color_node_free (&fill_color_node);
402 gl_debug (DEBUG_XML, "END");
405 /*--------------------------------------------------------------------------*/
406 /* PRIVATE. Parse XML Label->Image Node Properties */
407 /*--------------------------------------------------------------------------*/
409 xml04_parse_image_props (xmlNodePtr node,
410 glLabelImage *object)
413 glTextNode *filename;
415 gl_debug (DEBUG_XML, "START");
417 filename = g_new0 (glTextNode, 1);
418 filename->field_flag = FALSE;
419 filename->data = (gchar *)xmlGetProp (node, (xmlChar *)"filename");
420 gl_label_image_set_filename (object, filename);
421 gl_text_node_free (&filename);
423 w = lgl_xml_get_prop_double (node, "w", 0);
424 h = lgl_xml_get_prop_double (node, "h", 0);
425 gl_label_object_set_size (GL_LABEL_OBJECT(object), w, h);
427 gl_debug (DEBUG_XML, "END");
430 /*--------------------------------------------------------------------------*/
431 /* PRIVATE. Parse XML Label->Barcode Node Properties */
432 /*--------------------------------------------------------------------------*/
434 xml04_parse_barcode_props (xmlNodePtr node,
435 glLabelBarcode *object)
439 glColorNode *color_node;
442 glTextNode *text_node;
444 gl_debug (DEBUG_XML, "START");
446 color_node = gl_color_node_new_default ();
447 color_node->color = lgl_xml_get_prop_uint (node, "color", 0);
449 id = xmlGetProp (node, (xmlChar *)"style");
451 text_flag = lgl_xml_get_prop_boolean (node, "text", FALSE);
452 scale = lgl_xml_get_prop_double (node, "scale", 1.0);
454 scale = 0.5; /* Set to a valid value */
456 gl_label_barcode_set_props (object, (gchar *)id, text_flag, TRUE, 0);
457 gl_label_object_set_line_color (GL_LABEL_OBJECT(object), color_node);
459 child = node->xmlChildrenNode;
460 text_node = g_new0 (glTextNode, 1);
461 if (xmlStrEqual (child->name, (xmlChar *)"Field")) {
462 text_node->field_flag = TRUE;
463 text_node->data = (gchar *)xmlGetProp (child, (xmlChar *)"name");
464 } else if (xmlNodeIsText (child)) {
465 text_node->field_flag = FALSE;
466 text_node->data = (gchar *)xmlNodeGetContent (child);
468 g_message ("Unexpected Barcode child: \"%s\"", child->name);
470 gl_label_barcode_set_data (object, text_node);
472 gl_color_node_free (&color_node);
473 gl_text_node_free (&text_node);
476 gl_debug (DEBUG_XML, "END");
479 /*--------------------------------------------------------------------------*/
480 /* PRIVATE. Parse XML merge properties tag. */
481 /*--------------------------------------------------------------------------*/
483 xml04_parse_merge_properties (xmlNodePtr node,
489 gl_debug (DEBUG_XML, "START");
491 string = xmlGetProp (node, (xmlChar *)"type");
492 merge = gl_merge_new ((gchar *)string);
495 string = xmlGetProp (node, (xmlChar *)"src");
496 gl_merge_set_src (merge, (gchar *)string);
499 gl_label_set_merge (label, merge);
501 g_object_unref (G_OBJECT(merge));
503 gl_debug (DEBUG_XML, "END");