/** * Fetches document from URI, or creates new, if NULL; public document * appears in document list. */ SPDocument * sp_document_new(gchar const *uri, unsigned int keepalive, bool make_new) { SPDocument *doc; Inkscape::XML::Document *rdoc; gchar *base = NULL; gchar *name = NULL; if (uri) { Inkscape::XML::Node *rroot; gchar *s, *p; /* Try to fetch repr from file */ rdoc = sp_repr_read_file(uri, SP_SVG_NS_URI); /* If file cannot be loaded, return NULL without warning */ if (rdoc == NULL) return NULL; rroot = rdoc->root(); /* If xml file is not svg, return NULL without warning */ /* fixme: destroy document */ if (strcmp(rroot->name(), "svg:svg") != 0) return NULL; s = g_strdup(uri); p = strrchr(s, '/'); if (p) { name = g_strdup(p + 1); p[1] = '\0'; base = g_strdup(s); } else { base = NULL; name = g_strdup(uri); } g_free(s); } else { rdoc = sp_repr_document_new("svg:svg"); } if (make_new) { base = NULL; uri = NULL; name = g_strdup_printf(_("New document %d"), ++doc_count); } //# These should be set by now g_assert(name); doc = sp_document_create(rdoc, uri, base, name, keepalive); g_free(base); g_free(name); return doc; }
SPDocument * XSLT::open(Inkscape::Extension::Input */*module*/, gchar const *filename) { xmlDocPtr filein = xmlParseFile(filename); if (filein == NULL) { return NULL; } const char * params[1]; params[0] = NULL; xmlDocPtr result = xsltApplyStylesheet(_stylesheet, filein, params); xmlFreeDoc(filein); Inkscape::XML::Document * rdoc = sp_repr_do_read( result, SP_SVG_NS_URI); xmlFreeDoc(result); if (rdoc == NULL) { return NULL; } if (strcmp(rdoc->root()->name(), "svg:svg") != 0) { return NULL; } gchar * base = NULL; gchar * name = NULL; gchar * s = NULL, * p = NULL; s = g_strdup(filename); p = strrchr(s, '/'); if (p) { name = g_strdup(p + 1); p[1] = '\0'; base = g_strdup(s); } else { base = NULL; name = g_strdup(filename); } g_free(s); SPDocument * doc = SPDocument::createDoc(rdoc, filename, base, name, true, NULL); g_free(base); g_free(name); return doc; }
/** \return None \brief This is the function that does all of the SVG saves in Inkscape. It detects whether it should do a Inkscape namespace save internally. \param mod Extension to use. \param doc Document to save. \param uri The filename to save the file to. This function first checks it's parameters, and makes sure that we're getting good data. It also checks the module ID of the incoming module to figure out if this is save should include the Inkscape namespace stuff or not. The result of that comparison is stored in the spns variable. If there is not to be Inkscape name spaces a new document is created without. (I think, I'm not sure on this code) All of the internally referenced imageins are also set to relative paths in the file. And the file is saved. This really needs to be fleshed out more, but I don't quite understand all of this code. I just stole it. */ void Svg::save (Inkscape::Extension::Output *mod, SPDocument *doc, const gchar *uri) { g_return_if_fail(doc != NULL); g_return_if_fail(uri != NULL); gchar *save_path = g_path_get_dirname (uri); gboolean const spns = (!mod->get_id() || !strcmp (mod->get_id(), SP_MODULE_KEY_OUTPUT_SVG_INKSCAPE) || !strcmp (mod->get_id(), SP_MODULE_KEY_OUTPUT_SVGZ_INKSCAPE)); Inkscape::XML::Document *rdoc = NULL; Inkscape::XML::Node *repr = NULL; if (spns) { repr = sp_document_repr_root (doc); } else { rdoc = sp_repr_document_new ("svg:svg"); repr = rdoc->root(); repr = sp_document_root (doc)->updateRepr(rdoc, repr, SP_OBJECT_WRITE_BUILD); } Inkscape::IO::fixupHrefs( doc, save_path, spns ); gboolean const s = sp_repr_save_file (repr->document(), uri, SP_SVG_NS_URI); if (s == FALSE) { throw Inkscape::Extension::Output::save_failed(); } if (!spns) { Inkscape::GC::release(rdoc); } g_free(save_path); return; }
SPDocument * sp_document_new_from_mem(gchar const *buffer, gint length, unsigned int keepalive) { SPDocument *doc; Inkscape::XML::Document *rdoc; Inkscape::XML::Node *rroot; gchar *name; rdoc = sp_repr_read_mem(buffer, length, SP_SVG_NS_URI); /* If it cannot be loaded, return NULL without warning */ if (rdoc == NULL) return NULL; rroot = rdoc->root(); /* If xml file is not svg, return NULL without warning */ /* fixme: destroy document */ if (strcmp(rroot->name(), "svg:svg") != 0) return NULL; name = g_strdup_printf(_("Memory document %d"), ++doc_count); doc = sp_document_create(rdoc, NULL, NULL, name, keepalive); return doc; }
/// Returns new document having as first child a node named rootname. Inkscape::XML::Document * sp_repr_document_new(char const *rootname) { Inkscape::XML::Document *doc = new Inkscape::XML::SimpleDocument(); if (!strcmp(rootname, "svg:svg")) { doc->setAttribute("version", "1.0"); doc->setAttribute("standalone", "no"); Inkscape::XML::Node *comment = doc->createComment(" Created with Inkscape (http://www.inkscape.org/) "); doc->appendChild(comment); Inkscape::GC::release(comment); } Inkscape::XML::Node *root = doc->createElement(rootname); doc->appendChild(root); Inkscape::GC::release(root); return doc; }