svg.cpp revision 7e3c59e50c57dd75009d244e1493d54a72c805b5
163N/A/*
163N/A * This is the code that moves all of the SVG loading and saving into
163N/A * the module format. Really Inkscape is built to handle these formats
163N/A * internally, so this is just calling those internal functions.
163N/A *
163N/A * Authors:
163N/A * Lauris Kaplinski <lauris@kaplinski.com>
163N/A * Ted Gould <ted@gould.cx>
163N/A * Jon A. Cruz <jon@joncruz.org>
163N/A * Abhishek Sharma
163N/A *
163N/A * Copyright (C) 2002-2003 Authors
163N/A *
163N/A * Released under GNU GPL, read the file 'COPYING' for more information
163N/A */
163N/A
163N/A#ifdef HAVE_CONFIG_H
163N/A# include <config.h>
163N/A#endif
163N/A#include "sp-object.h"
163N/A#include "svg.h"
163N/A#include "file.h"
163N/A#include "extension/system.h"
163N/A#include "extension/output.h"
163N/A#include <vector>
163N/A#include "xml/attribute-record.h"
163N/A#include "sp-root.h"
163N/A#include "document.h"
163N/A
211N/A#ifdef WITH_GNOME_VFS
211N/A# include <libgnomevfs/gnome-vfs.h>
211N/A#endif
211N/A
211N/Anamespace Inkscape {
211N/Anamespace Extension {
163N/Anamespace Internal {
163N/A
163N/A#include "clear-n_.h"
163N/A
163N/A
211N/Ausing Inkscape::Util::List;
163N/Ausing Inkscape::XML::AttributeRecord;
163N/Ausing Inkscape::XML::Node;
163N/A
163N/A
163N/A
163N/Avoid pruneExtendedAttributes( Inkscape::XML::Node *repr )
163N/A{
163N/A if (repr) {
163N/A if ( repr->type() == Inkscape::XML::ELEMENT_NODE ) {
163N/A std::vector<gchar const*> toBeRemoved;
163N/A for ( List<AttributeRecord const> it = repr->attributeList(); it; ++it ) {
163N/A const gchar* attrName = g_quark_to_string(it->key);
163N/A if ((strncmp("inkscape:", attrName, 9) == 0) || (strncmp("sodipodi:", attrName, 9) == 0)) {
163N/A toBeRemoved.push_back(attrName);
163N/A }
163N/A }
163N/A // Can't change the set we're interating over while we are iterating.
163N/A for ( std::vector<gchar const*>::iterator it = toBeRemoved.begin(); it != toBeRemoved.end(); ++it ) {
163N/A repr->setAttribute(*it, 0);
163N/A }
163N/A }
163N/A
163N/A for ( Node *child = repr->firstChild(); child; child = child->next() ) {
163N/A pruneExtendedAttributes(child);
163N/A }
163N/A }
163N/A}
163N/A
163N/A
163N/A/**
211N/A \return None
163N/A \brief What would an SVG editor be without loading/saving SVG
163N/A files. This function sets that up.
163N/A
163N/A For each module there is a call to Inkscape::Extension::build_from_mem
163N/A with a rather large XML file passed in. This is a constant string
211N/A that describes the module. At the end of this call a module is
163N/A returned that is basically filled out. The one thing that it doesn't
163N/A have is the key function for the operation. And that is linked at
163N/A the end of each call.
163N/A*/
163N/Avoid
163N/ASvg::init(void)
163N/A{
163N/A /* SVG in */
163N/A Inkscape::Extension::build_from_mem(
211N/A "<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
211N/A "<name>" N_("SVG Input") "</name>\n"
163N/A "<id>" SP_MODULE_KEY_INPUT_SVG "</id>\n"
163N/A "<input>\n"
163N/A "<extension>.svg</extension>\n"
163N/A "<mimetype>image/svg+xml</mimetype>\n"
163N/A "<filetypename>" N_("Scalable Vector Graphic (*.svg)") "</filetypename>\n"
163N/A "<filetypetooltip>" N_("Inkscape native file format and W3C standard") "</filetypetooltip>\n"
181N/A "<output_extension>" SP_MODULE_KEY_OUTPUT_SVG_INKSCAPE "</output_extension>\n"
163N/A "</input>\n"
163N/A "</inkscape-extension>", new Svg());
163N/A
163N/A /* SVG out Inkscape */
Inkscape::Extension::build_from_mem(
"<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
"<name>" N_("SVG Output Inkscape") "</name>\n"
"<id>" SP_MODULE_KEY_OUTPUT_SVG_INKSCAPE "</id>\n"
"<output>\n"
"<extension>.svg</extension>\n"
"<mimetype>image/x-inkscape-svg</mimetype>\n"
"<filetypename>" N_("Inkscape SVG (*.svg)") "</filetypename>\n"
"<filetypetooltip>" N_("SVG format with Inkscape extensions") "</filetypetooltip>\n"
"<dataloss>false</dataloss>\n"
"</output>\n"
"</inkscape-extension>", new Svg());
/* SVG out */
Inkscape::Extension::build_from_mem(
"<inkscape-extension xmlns=\"" INKSCAPE_EXTENSION_URI "\">\n"
"<name>" N_("SVG Output") "</name>\n"
"<id>" SP_MODULE_KEY_OUTPUT_SVG "</id>\n"
"<output>\n"
"<extension>.svg</extension>\n"
"<mimetype>image/svg+xml</mimetype>\n"
"<filetypename>" N_("Plain SVG (*.svg)") "</filetypename>\n"
"<filetypetooltip>" N_("Scalable Vector Graphics format as defined by the W3C") "</filetypetooltip>\n"
"</output>\n"
"</inkscape-extension>", new Svg());
#ifdef WITH_GNOME_VFS
gnome_vfs_init();
#endif
return;
}
#ifdef WITH_GNOME_VFS
#define BUF_SIZE 8192
static gchar *
_load_uri (const gchar *uri)
{
GnomeVFSHandle *handle = NULL;
GnomeVFSFileSize bytes_read;
gsize bytesRead = 0;
gsize bytesWritten = 0;
GError* error = NULL;
gchar* uri_local = g_filename_from_utf8( uri, -1, &bytesRead, &bytesWritten, &error);
if ( uri_local == NULL ) {
g_warning( "Error converting filename to locale encoding.");
}
GnomeVFSResult result = gnome_vfs_open (&handle, uri_local, GNOME_VFS_OPEN_READ);
if (result != GNOME_VFS_OK) {
g_warning("%s", gnome_vfs_result_to_string(result));
}
std::vector<gchar> doc;
while (result == GNOME_VFS_OK) {
gchar buffer[BUF_SIZE];
result = gnome_vfs_read (handle, buffer, BUF_SIZE, &bytes_read);
doc.insert(doc.end(), buffer, buffer+bytes_read);
}
return g_strndup(&doc[0], doc.size());
}
#endif
/**
\return A new document just for you!
\brief This function takes in a filename of a SVG document and
turns it into a SPDocument.
\param mod Module to use
\param uri The path to the file (UTF-8)
This function is really simple, it just calls sp_document_new...
*/
SPDocument *
Svg::open (Inkscape::Extension::Input */*mod*/, const gchar *uri)
{
#ifdef WITH_GNOME_VFS
if (!gnome_vfs_initialized() || gnome_vfs_uri_is_local(gnome_vfs_uri_new(uri))) {
// Use built-in loader instead of VFS for this
return SPDocument::createNewDoc(uri, TRUE);
}
gchar * buffer = _load_uri(uri);
if (buffer == NULL) {
g_warning("Error: Could not open file '%s' with VFS\n", uri);
return NULL;
}
SPDocument * doc = SPDocument::createNewDocFromMem(buffer, strlen(buffer), 1);
g_free(buffer);
return doc;
#else
return SPDocument::createNewDoc(uri, TRUE);
#endif
}
/**
\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 its parameters, and makes sure that
we're getting good data. It also checks the module ID of the
incoming module to figure out whether this save should include
the Inkscape namespace stuff or not. The result of that comparison
is stored in the exportExtensions 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, gchar const *filename)
{
g_return_if_fail(doc != NULL);
g_return_if_fail(filename != NULL);
bool const exportExtensions = ( !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 (exportExtensions) {
repr = doc->getReprRoot();
} else {
rdoc = sp_repr_document_new ("svg:svg");
repr = rdoc->root();
repr = doc->getRoot()->updateRepr(rdoc, repr, SP_OBJECT_WRITE_BUILD);
pruneExtendedAttributes(repr);
}
if (!sp_repr_save_rebased_file(repr->document(), filename, SP_SVG_NS_URI,
doc->getBase(), filename)) {
throw Inkscape::Extension::Output::save_failed();
}
if (!exportExtensions) {
Inkscape::GC::release(rdoc);
}
return;
}
} } } /* namespace inkscape, module, implementation */
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :