1 #define __SP_REPR_IO_C__
3 /*
4 * Dirty DOM-like tree
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * bulia byak <buliabyak@users.sf.net>
9 *
10 * Copyright (C) 1999-2002 Lauris Kaplinski
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #ifdef HAVE_CONFIG_H
16 # include <config.h>
17 #endif
19 #include <stdexcept>
21 #include "xml/repr.h"
22 #include "xml/attribute-record.h"
23 #include "xml/simple-document.h"
25 #include "io/sys.h"
26 #include "io/uristream.h"
27 #include "io/gzipstream.h"
29 #include "prefs-utils.h"
31 using Inkscape::IO::Writer;
32 using Inkscape::Util::List;
33 using Inkscape::Util::cons;
34 using Inkscape::XML::Document;
35 using Inkscape::XML::SimpleDocument;
36 using Inkscape::XML::Node;
37 using Inkscape::XML::AttributeRecord;
39 static Document *sp_repr_do_read (xmlDocPtr doc, const gchar *default_ns);
40 static Node *sp_repr_svg_read_node (Document *xml_doc, xmlNodePtr node, const gchar *default_ns, GHashTable *prefix_map);
41 static gint sp_repr_qualified_name (gchar *p, gint len, xmlNsPtr ns, const xmlChar *name, const gchar *default_ns, GHashTable *prefix_map);
42 static void sp_repr_write_stream_root_element (Node *repr, Writer &out, bool add_whitespace, gchar const *default_ns, int inlineattrs, int indent);
43 static void sp_repr_write_stream (Node *repr, Writer &out, gint indent_level, bool add_whitespace, Glib::QueryQuark elide_prefix, int inlineattrs, int indent);
44 static void sp_repr_write_stream_element (Node *repr, Writer &out, gint indent_level, bool add_whitespace, Glib::QueryQuark elide_prefix, List<AttributeRecord const> attributes, int inlineattrs, int indent);
46 #ifdef HAVE_LIBWMF
47 static xmlDocPtr sp_wmf_convert (const char * file_name);
48 static char * sp_wmf_image_name (void * context);
49 #endif /* HAVE_LIBWMF */
52 class XmlSource
53 {
54 public:
55 XmlSource()
56 : filename(0),
57 encoding(0),
58 fp(0),
59 firstFewLen(0),
60 dummy("x"),
61 instr(0),
62 gzin(0)
63 {
64 }
65 virtual ~XmlSource()
66 {
67 close();
68 if ( encoding ) {
69 g_free(encoding);
70 encoding = 0;
71 }
72 }
74 int setFile( char const * filename );
76 static int readCb( void * context, char * buffer, int len );
77 static int closeCb( void * context );
79 char const* getEncoding() const { return encoding; }
80 int read( char * buffer, int len );
81 int close();
82 private:
83 const char* filename;
84 char* encoding;
85 FILE* fp;
86 unsigned char firstFew[4];
87 int firstFewLen;
88 Inkscape::URI dummy;
89 Inkscape::IO::UriInputStream* instr;
90 Inkscape::IO::GzipInputStream* gzin;
91 };
93 int XmlSource::setFile(char const *filename)
94 {
95 int retVal = -1;
97 this->filename = filename;
99 fp = Inkscape::IO::fopen_utf8name(filename, "r");
100 if ( fp ) {
101 // First peek in the file to see what it is
102 memset( firstFew, 0, sizeof(firstFew) );
104 size_t some = fread( firstFew, 1, 4, fp );
105 if ( fp ) {
106 // first check for compression
107 if ( (some >= 2) && (firstFew[0] == 0x1f) && (firstFew[1] == 0x8b) ) {
108 //g_message(" the file being read is gzip'd. extract it");
109 fclose(fp);
110 fp = 0;
111 fp = Inkscape::IO::fopen_utf8name(filename, "r");
112 instr = new Inkscape::IO::UriInputStream(fp, dummy);
113 gzin = new Inkscape::IO::GzipInputStream(*instr);
115 memset( firstFew, 0, sizeof(firstFew) );
116 some = 0;
117 int single = 0;
118 while ( some < 4 && single >= 0 )
119 {
120 single = gzin->get();
121 if ( single >= 0 ) {
122 firstFew[some++] = 0x0ff & single;
123 } else {
124 break;
125 }
126 }
127 }
129 int encSkip = 0;
130 if ( (some >= 2) &&(firstFew[0] == 0xfe) && (firstFew[1] == 0xff) ) {
131 encoding = g_strdup("UTF-16BE");
132 encSkip = 2;
133 } else if ( (some >= 2) && (firstFew[0] == 0xff) && (firstFew[1] == 0xfe) ) {
134 encoding = g_strdup("UTF-16LE");
135 encSkip = 2;
136 } else if ( (some >= 3) && (firstFew[0] == 0xef) && (firstFew[1] == 0xbb) && (firstFew[2] == 0xbf) ) {
137 encoding = g_strdup("UTF-8");
138 encSkip = 3;
139 }
141 if ( encSkip ) {
142 memmove( firstFew, firstFew + encSkip, (some - encSkip) );
143 some -= encSkip;
144 }
146 firstFewLen = some;
147 retVal = 0; // no error
148 }
149 }
151 return retVal;
152 }
155 int XmlSource::readCb( void * context, char * buffer, int len )
156 {
157 int retVal = -1;
158 if ( context ) {
159 XmlSource* self = static_cast<XmlSource*>(context);
160 retVal = self->read( buffer, len );
161 }
162 return retVal;
163 }
165 int XmlSource::closeCb(void * context)
166 {
167 if ( context ) {
168 XmlSource* self = static_cast<XmlSource*>(context);
169 self->close();
170 }
171 return 0;
172 }
174 int XmlSource::read( char *buffer, int len )
175 {
176 int retVal = 0;
177 size_t got = 0;
179 if ( firstFewLen > 0 ) {
180 int some = (len < firstFewLen) ? len : firstFewLen;
181 memcpy( buffer, firstFew, some );
182 if ( len < firstFewLen ) {
183 memmove( firstFew, firstFew + some, (firstFewLen - some) );
184 }
185 firstFewLen -= some;
186 got = some;
187 } else if ( gzin ) {
188 int single = 0;
189 while ( (int)got < len && single >= 0 )
190 {
191 single = gzin->get();
192 if ( single >= 0 ) {
193 buffer[got++] = 0x0ff & single;
194 } else {
195 break;
196 }
197 }
198 } else {
199 got = fread( buffer, 1, len, fp );
200 }
202 if ( feof(fp) ) {
203 retVal = got;
204 } else if ( ferror(fp) ) {
205 retVal = -1;
206 } else {
207 retVal = got;
208 }
210 return retVal;
211 }
213 int XmlSource::close()
214 {
215 if ( gzin ) {
216 gzin->close();
217 delete gzin;
218 gzin = 0;
219 }
220 if ( instr ) {
221 instr->close();
222 fp = 0;
223 delete instr;
224 instr = 0;
225 }
226 if ( fp ) {
227 fclose(fp);
228 fp = 0;
229 }
230 return 0;
231 }
233 /**
234 * Reads XML from a file, including WMF files, and returns the Document.
235 * The default namespace can also be specified, if desired.
236 */
237 Document *
238 sp_repr_read_file (const gchar * filename, const gchar *default_ns)
239 {
240 xmlDocPtr doc = 0;
241 Document * rdoc = 0;
243 xmlSubstituteEntitiesDefault(1);
245 g_return_val_if_fail (filename != NULL, NULL);
246 g_return_val_if_fail (Inkscape::IO::file_test( filename, G_FILE_TEST_EXISTS ), NULL);
248 // TODO: bulia, please look over
249 gsize bytesRead = 0;
250 gsize bytesWritten = 0;
251 GError* error = NULL;
252 // TODO: need to replace with our own fopen and reading
253 gchar* localFilename = g_filename_from_utf8 ( filename,
254 -1, &bytesRead, &bytesWritten, &error);
255 g_return_val_if_fail( localFilename != NULL, NULL );
257 Inkscape::IO::dump_fopen_call( filename, "N" );
259 #ifdef HAVE_LIBWMF
260 if (strlen (localFilename) > 4) {
261 if ( (strcmp (localFilename + strlen (localFilename) - 4,".wmf") == 0)
262 || (strcmp (localFilename + strlen (localFilename) - 4,".WMF") == 0)) {
263 doc = sp_wmf_convert (localFilename);
264 }
265 }
266 #endif // !HAVE_LIBWMF
268 if ( !doc ) {
269 XmlSource src;
271 if ( (src.setFile(filename) == 0) ) {
272 doc = xmlReadIO( XmlSource::readCb,
273 XmlSource::closeCb,
274 &src,
275 localFilename,
276 src.getEncoding(),
277 XML_PARSE_NOENT );
278 }
279 }
281 rdoc = sp_repr_do_read( doc, default_ns );
282 if ( doc ) {
283 xmlFreeDoc( doc );
284 }
286 if ( localFilename ) {
287 g_free( localFilename );
288 }
290 return rdoc;
291 }
293 /**
294 * Reads and parses XML from a buffer, returning it as an Document
295 */
296 Document *
297 sp_repr_read_mem (const gchar * buffer, gint length, const gchar *default_ns)
298 {
299 xmlDocPtr doc;
300 Document * rdoc;
302 xmlSubstituteEntitiesDefault(1);
304 g_return_val_if_fail (buffer != NULL, NULL);
306 doc = xmlParseMemory ((gchar *) buffer, length);
308 rdoc = sp_repr_do_read (doc, default_ns);
309 if (doc)
310 xmlFreeDoc (doc);
311 return rdoc;
312 }
314 namespace Inkscape {
316 struct compare_quark_ids {
317 bool operator()(Glib::QueryQuark const &a, Glib::QueryQuark const &b) const {
318 return a.id() < b.id();
319 }
320 };
322 }
324 namespace {
326 typedef std::map<Glib::QueryQuark, Glib::QueryQuark, Inkscape::compare_quark_ids> PrefixMap;
328 Glib::QueryQuark qname_prefix(Glib::QueryQuark qname) {
329 static PrefixMap prefix_map;
330 PrefixMap::iterator iter = prefix_map.find(qname);
331 if ( iter != prefix_map.end() ) {
332 return (*iter).second;
333 } else {
334 gchar const *name_string=g_quark_to_string(qname);
335 gchar const *prefix_end=strchr(name_string, ':');
336 if (prefix_end) {
337 Glib::Quark prefix=Glib::ustring(name_string, prefix_end);
338 prefix_map.insert(PrefixMap::value_type(qname, prefix));
339 return prefix;
340 } else {
341 return GQuark(0);
342 }
343 }
344 }
346 }
348 namespace {
350 void promote_to_svg_namespace(Node *repr) {
351 if ( repr->type() == Inkscape::XML::ELEMENT_NODE ) {
352 GQuark code = repr->code();
353 if (!qname_prefix(code).id()) {
354 gchar *svg_name = g_strconcat("svg:", g_quark_to_string(code), NULL);
355 repr->setCodeUnsafe(g_quark_from_string(svg_name));
356 g_free(svg_name);
357 }
358 for ( Node *child = sp_repr_children(repr) ; child ; child = sp_repr_next(child) ) {
359 promote_to_svg_namespace(child);
360 }
361 }
362 }
364 }
366 /**
367 * Reads in a XML file to create a Document
368 */
369 Document *
370 sp_repr_do_read (xmlDocPtr doc, const gchar *default_ns)
371 {
372 if (doc == NULL) return NULL;
373 xmlNodePtr node=xmlDocGetRootElement (doc);
374 if (node == NULL) return NULL;
376 GHashTable * prefix_map;
377 prefix_map = g_hash_table_new (g_str_hash, g_str_equal);
379 Document *rdoc = new Inkscape::XML::SimpleDocument();
381 Node *root=NULL;
382 for ( node = doc->children ; node != NULL ; node = node->next ) {
383 if (node->type == XML_ELEMENT_NODE) {
384 Node *repr=sp_repr_svg_read_node(rdoc, node, default_ns, prefix_map);
385 rdoc->appendChild(repr);
386 Inkscape::GC::release(repr);
388 if (!root) {
389 root = repr;
390 } else {
391 root = NULL;
392 break;
393 }
394 } else if ( node->type == XML_COMMENT_NODE ) {
395 Node *comment=sp_repr_svg_read_node(rdoc, node, default_ns, prefix_map);
396 rdoc->appendChild(comment);
397 Inkscape::GC::release(comment);
398 }
399 }
401 if (root != NULL) {
402 /* promote elements of SVG documents that don't use namespaces
403 * into the SVG namespace */
404 if ( default_ns && !strcmp(default_ns, SP_SVG_NS_URI)
405 && !strcmp(root->name(), "svg") )
406 {
407 promote_to_svg_namespace(root);
408 }
409 }
411 g_hash_table_destroy (prefix_map);
413 return rdoc;
414 }
416 gint
417 sp_repr_qualified_name (gchar *p, gint len, xmlNsPtr ns, const xmlChar *name, const gchar *default_ns, GHashTable *prefix_map)
418 {
419 const xmlChar *prefix;
420 if ( ns && ns->href ) {
421 prefix = (xmlChar*)sp_xml_ns_uri_prefix ((gchar*)ns->href, (char*)ns->prefix);
422 g_hash_table_insert (prefix_map, (gpointer)prefix, (gpointer)ns->href);
423 } else {
424 prefix = NULL;
425 }
427 if (prefix)
428 return g_snprintf (p, len, "%s:%s", (gchar*)prefix, name);
429 else
430 return g_snprintf (p, len, "%s", name);
431 }
433 static Node *
434 sp_repr_svg_read_node (Document *xml_doc, xmlNodePtr node, const gchar *default_ns, GHashTable *prefix_map)
435 {
436 Node *repr, *crepr;
437 xmlAttrPtr prop;
438 xmlNodePtr child;
439 gchar c[256];
441 if (node->type == XML_TEXT_NODE || node->type == XML_CDATA_SECTION_NODE) {
443 if (node->content == NULL || *(node->content) == '\0')
444 return NULL; // empty text node
446 bool preserve = (xmlNodeGetSpacePreserve (node) == 1);
448 xmlChar *p;
449 for (p = node->content; *p && g_ascii_isspace (*p) && !preserve; p++)
450 ; // skip all whitespace
452 if (!(*p)) { // this is an all-whitespace node, and preserve == default
453 return NULL; // we do not preserve all-whitespace nodes unless we are asked to
454 }
456 return xml_doc->createTextNode((const gchar *)node->content);
457 }
459 if (node->type == XML_COMMENT_NODE)
460 return xml_doc->createComment((const gchar *)node->content);
462 if (node->type == XML_ENTITY_DECL) return NULL;
464 sp_repr_qualified_name (c, 256, node->ns, node->name, default_ns, prefix_map);
465 repr = xml_doc->createElement(c);
466 /* TODO remember node->ns->prefix if node->ns != NULL */
468 for (prop = node->properties; prop != NULL; prop = prop->next) {
469 if (prop->children) {
470 sp_repr_qualified_name (c, 256, prop->ns, prop->name, default_ns, prefix_map);
471 repr->setAttribute(c, (gchar*)prop->children->content);
472 /* TODO remember prop->ns->prefix if prop->ns != NULL */
473 }
474 }
476 if (node->content)
477 repr->setContent((gchar*)node->content);
479 child = node->xmlChildrenNode;
480 for (child = node->xmlChildrenNode; child != NULL; child = child->next) {
481 crepr = sp_repr_svg_read_node (xml_doc, child, default_ns, prefix_map);
482 if (crepr) {
483 repr->appendChild(crepr);
484 Inkscape::GC::release(crepr);
485 }
486 }
488 return repr;
489 }
491 void
492 sp_repr_save_stream (Document *doc, FILE *fp, gchar const *default_ns, bool compress)
493 {
494 Node *repr;
495 const gchar *str;
497 Inkscape::URI dummy("x");
498 Inkscape::IO::UriOutputStream bout(fp, dummy);
499 Inkscape::IO::GzipOutputStream *gout = compress ? new Inkscape::IO::GzipOutputStream(bout) : NULL;
500 Inkscape::IO::OutputStreamWriter *out = compress ? new Inkscape::IO::OutputStreamWriter( *gout ) : new Inkscape::IO::OutputStreamWriter( bout );
502 int inlineattrs = prefs_get_int_attribute("options.svgoutput", "inlineattrs", 0);
503 int indent = prefs_get_int_attribute("options.svgoutput", "indent", 2);
505 /* fixme: do this The Right Way */
506 out->writeString( "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" );
508 str = ((Node *)doc)->attribute("doctype");
509 if (str) {
510 out->writeString( str );
511 }
513 repr = sp_repr_document_first_child(doc);
514 for ( repr = sp_repr_document_first_child(doc) ;
515 repr ; repr = sp_repr_next(repr) )
516 {
517 if ( repr->type() == Inkscape::XML::ELEMENT_NODE ) {
518 sp_repr_write_stream_root_element(repr, *out, TRUE, default_ns, inlineattrs, indent);
519 } else if ( repr->type() == Inkscape::XML::COMMENT_NODE ) {
520 sp_repr_write_stream(repr, *out, 0, TRUE, GQuark(0), inlineattrs, indent);
521 out->writeChar( '\n' );
522 } else {
523 sp_repr_write_stream(repr, *out, 0, TRUE, GQuark(0), inlineattrs, indent);
524 }
525 }
526 if ( out ) {
527 delete out;
528 out = NULL;
529 }
530 if ( gout ) {
531 delete gout;
532 gout = NULL;
533 }
534 }
536 /* Returns TRUE if file successfully saved; FALSE if not
537 */
538 bool
539 sp_repr_save_file (Document *doc, const gchar *filename,
540 gchar const *default_ns)
541 {
542 if (filename == NULL) {
543 return FALSE;
544 }
545 bool compress = false;
546 {
547 if (strlen (filename) > 5) {
548 gchar tmp[] = {0,0,0,0,0,0};
549 strncpy( tmp, filename + strlen (filename) - 5, 6 );
550 tmp[5] = 0;
551 if ( strcasecmp(".svgz", tmp ) == 0 )
552 {
553 //g_message("TIME TO COMPRESS THE OUTPUT FOR SVGZ");
554 compress = true;
555 }
556 }
557 }
559 Inkscape::IO::dump_fopen_call( filename, "B" );
560 FILE *file = Inkscape::IO::fopen_utf8name(filename, "w");
561 if (file == NULL) {
562 return FALSE;
563 }
565 sp_repr_save_stream (doc, file, default_ns, compress);
567 if (fclose (file) != 0) {
568 return FALSE;
569 }
571 return TRUE;
572 }
574 void
575 sp_repr_print (Node * repr)
576 {
577 Inkscape::IO::StdOutputStream bout;
578 Inkscape::IO::OutputStreamWriter out(bout);
580 sp_repr_write_stream (repr, out, 0, TRUE, GQuark(0), 0, 2);
582 return;
583 }
585 /* (No doubt this function already exists elsewhere.) */
586 static void
587 repr_quote_write (Writer &out, const gchar * val)
588 {
589 if (!val) return;
591 for (; *val != '\0'; val++) {
592 switch (*val) {
593 case '"': out.writeString( """ ); break;
594 case '&': out.writeString( "&" ); break;
595 case '<': out.writeString( "<" ); break;
596 case '>': out.writeString( ">" ); break;
597 default: out.writeChar( *val ); break;
598 }
599 }
600 }
602 namespace {
604 typedef std::map<Glib::QueryQuark, gchar const *, Inkscape::compare_quark_ids> LocalNameMap;
605 typedef std::map<Glib::QueryQuark, Inkscape::Util::ptr_shared<char>, Inkscape::compare_quark_ids> NSMap;
607 gchar const *qname_local_name(Glib::QueryQuark qname) {
608 static LocalNameMap local_name_map;
609 LocalNameMap::iterator iter = local_name_map.find(qname);
610 if ( iter != local_name_map.end() ) {
611 return (*iter).second;
612 } else {
613 gchar const *name_string=g_quark_to_string(qname);
614 gchar const *prefix_end=strchr(name_string, ':');
615 if (prefix_end) {
616 return prefix_end + 1;
617 } else {
618 return name_string;
619 }
620 }
621 }
623 void add_ns_map_entry(NSMap &ns_map, Glib::QueryQuark prefix) {
624 using Inkscape::Util::ptr_shared;
625 using Inkscape::Util::share_unsafe;
627 static const Glib::QueryQuark xml_prefix("xml");
629 NSMap::iterator iter=ns_map.find(prefix);
630 if ( iter == ns_map.end() ) {
631 if (prefix.id()) {
632 gchar const *uri=sp_xml_ns_prefix_uri(g_quark_to_string(prefix));
633 if (uri) {
634 ns_map.insert(NSMap::value_type(prefix, share_unsafe(uri)));
635 } else if ( prefix != xml_prefix ) {
636 g_warning("No namespace known for normalized prefix %s", g_quark_to_string(prefix));
637 }
638 } else {
639 ns_map.insert(NSMap::value_type(prefix, ptr_shared<char>()));
640 }
641 }
642 }
644 void populate_ns_map(NSMap &ns_map, Node &repr) {
645 if ( repr.type() == Inkscape::XML::ELEMENT_NODE ) {
646 add_ns_map_entry(ns_map, qname_prefix(repr.code()));
647 for ( List<AttributeRecord const> iter=repr.attributeList() ;
648 iter ; ++iter )
649 {
650 Glib::QueryQuark prefix=qname_prefix(iter->key);
651 if (prefix.id()) {
652 add_ns_map_entry(ns_map, prefix);
653 }
654 }
655 for ( Node *child=sp_repr_children(&repr) ;
656 child ; child = sp_repr_next(child) )
657 {
658 populate_ns_map(ns_map, *child);
659 }
660 }
661 }
663 }
665 void
666 sp_repr_write_stream_root_element (Node *repr, Writer &out, bool add_whitespace, gchar const *default_ns,
667 int inlineattrs, int indent)
668 {
669 using Inkscape::Util::ptr_shared;
670 g_assert(repr != NULL);
671 Glib::QueryQuark xml_prefix=g_quark_from_static_string("xml");
673 NSMap ns_map;
674 populate_ns_map(ns_map, *repr);
676 Glib::QueryQuark elide_prefix=GQuark(0);
677 if ( default_ns && ns_map.find(GQuark(0)) == ns_map.end() ) {
678 elide_prefix = g_quark_from_string(sp_xml_ns_uri_prefix(default_ns, NULL));
679 }
681 List<AttributeRecord const> attributes=repr->attributeList();
682 for ( NSMap::iterator iter=ns_map.begin() ; iter != ns_map.end() ; ++iter )
683 {
684 Glib::QueryQuark prefix=(*iter).first;
685 ptr_shared<char> ns_uri=(*iter).second;
687 if (prefix.id()) {
688 if ( prefix != xml_prefix ) {
689 if ( elide_prefix == prefix ) {
690 attributes = cons(AttributeRecord(g_quark_from_static_string("xmlns"), ns_uri), attributes);
691 }
693 Glib::ustring attr_name="xmlns:";
694 attr_name.append(g_quark_to_string(prefix));
695 GQuark key = g_quark_from_string(attr_name.c_str());
696 attributes = cons(AttributeRecord(key, ns_uri), attributes);
697 }
698 } else {
699 // if there are non-namespaced elements, we can't globally
700 // use a default namespace
701 elide_prefix = GQuark(0);
702 }
703 }
705 return sp_repr_write_stream_element(repr, out, 0, add_whitespace, elide_prefix, attributes, inlineattrs, indent);
706 }
708 void
709 sp_repr_write_stream (Node *repr, Writer &out, gint indent_level,
710 bool add_whitespace, Glib::QueryQuark elide_prefix, int inlineattrs, int indent)
711 {
712 if (repr->type() == Inkscape::XML::TEXT_NODE) {
713 repr_quote_write (out, repr->content());
714 } else if (repr->type() == Inkscape::XML::COMMENT_NODE) {
715 out.printf( "<!--%s-->", repr->content() );
716 } else if (repr->type() == Inkscape::XML::ELEMENT_NODE) {
717 sp_repr_write_stream_element(repr, out, indent_level, add_whitespace, elide_prefix, repr->attributeList(), inlineattrs, indent);
718 } else {
719 g_assert_not_reached();
720 }
721 }
723 void
724 sp_repr_write_stream_element (Node * repr, Writer & out, gint indent_level,
725 bool add_whitespace,
726 Glib::QueryQuark elide_prefix,
727 List<AttributeRecord const> attributes,
728 int inlineattrs, int indent)
729 {
730 Node *child;
731 bool loose;
733 g_return_if_fail (repr != NULL);
735 if ( indent_level > 16 )
736 indent_level = 16;
738 if (add_whitespace && indent) {
739 for (gint i = 0; i < indent_level; i++) {
740 for (gint j = 0; j < indent; j++) {
741 out.writeString(" ");
742 }
743 }
744 }
746 GQuark code = repr->code();
747 gchar const *element_name;
748 if ( elide_prefix == qname_prefix(code) ) {
749 element_name = qname_local_name(code);
750 } else {
751 element_name = g_quark_to_string(code);
752 }
753 out.printf( "<%s", element_name );
755 // if this is a <text> element, suppress formatting whitespace
756 // for its content and children:
757 gchar const *xml_space_attr = repr->attribute("xml:space");
758 if (xml_space_attr != NULL && !strcmp(xml_space_attr, "preserve")) {
759 add_whitespace = FALSE;
760 }
762 for ( List<AttributeRecord const> iter = attributes ;
763 iter ; ++iter )
764 {
765 if (!inlineattrs) {
766 out.writeString("\n");
767 if (indent) {
768 for ( gint i = 0 ; i < indent_level + 1 ; i++ ) {
769 for ( gint j = 0 ; j < indent ; j++ ) {
770 out.writeString(" ");
771 }
772 }
773 }
774 }
775 out.printf(" %s=\"", g_quark_to_string(iter->key));
776 repr_quote_write(out, iter->value);
777 out.writeChar('"');
778 }
780 loose = TRUE;
781 for (child = repr->firstChild() ; child != NULL; child = child->next()) {
782 if (child->type() == Inkscape::XML::TEXT_NODE) {
783 loose = FALSE;
784 break;
785 }
786 }
787 if (repr->firstChild()) {
788 out.writeString( ">" );
789 if (loose && add_whitespace) {
790 out.writeString( "\n" );
791 }
792 for (child = repr->firstChild(); child != NULL; child = child->next()) {
793 sp_repr_write_stream (child, out, (loose) ? (indent_level + 1) : 0, add_whitespace, elide_prefix, inlineattrs, indent);
794 }
796 if (loose && add_whitespace && indent) {
797 for (gint i = 0; i < indent_level; i++) {
798 for ( gint j = 0 ; j < indent ; j++ ) {
799 out.writeString(" ");
800 }
801 }
802 }
803 out.printf( "</%s>", element_name );
804 } else {
805 out.writeString( " />" );
806 }
808 // text elements cannot nest, so we can output newline
809 // after closing text
811 if (add_whitespace || !strcmp (repr->name(), "svg:text")) {
812 out.writeString( "\n" );
813 }
814 }
817 /*
818 Local Variables:
819 mode:c++
820 c-file-style:"stroustrup"
821 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
822 indent-tabs-mode:nil
823 fill-column:99
824 End:
825 */
826 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :