From: ishmal Date: Thu, 13 Apr 2006 10:50:59 +0000 (+0000) Subject: wip X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=bf855a45a363791d2820b6b3a8ed7b1363bbd55c;p=inkscape.git wip --- diff --git a/src/extension/internal/odf.cpp b/src/extension/internal/odf.cpp index 2b78bede8..bc98be79b 100644 --- a/src/extension/internal/odf.cpp +++ b/src/extension/internal/odf.cpp @@ -54,10 +54,16 @@ #include "dom/dom.h" #include "dom/util/ziptool.h" +#include "dom/io/domstream.h" +#include "dom/io/bufferstream.h" //# Shorthand notation typedef org::w3c::dom::DOMString DOMString; +typedef org::w3c::dom::io::OutputStreamWriter OutputStreamWriter; +typedef org::w3c::dom::io::BufferOutputStream BufferOutputStream; + + namespace Inkscape @@ -70,121 +76,11 @@ namespace Internal -class ImageInfo -{ -public: - - ImageInfo(const DOMString &nameArg, - const DOMString &newNameArg, - const std::vector &bufArg) - { - name = nameArg; - newName = newNameArg; - buf = bufArg; - } - - virtual ~ImageInfo() - {} - - DOMString getName() - { - return name; - } - - DOMString getNewName() - { - return newName; - } - - - std::vector getBuf() - { - return buf; - } - - DOMString name; - DOMString newName; - std::vectorbuf; - -}; - - -class StyleInfo -{ -public: - - StyleInfo(const DOMString &nameArg, const DOMString &styleArg) - { - name = nameArg; - style = styleArg; - fill = "none"; - stroke = "none"; - } - - virtual ~StyleInfo() - {} - - DOMString getName() - { - return name; - } - - DOMString getCssStyle() - { - return cssStyle; - } - - DOMString getStroke() - { - return stroke; - } - - DOMString getStrokeColor() - { - return strokeColor; - } - - DOMString getStrokeWidth() - { - return strokeWidth; - } - - - DOMString getFill() - { - return fill; - } - - DOMString getFillColor() - { - return fillColor; - } - - DOMString name; - DOMString style; - DOMString cssStyle; - DOMString stroke; - DOMString strokeColor; - DOMString strokeWidth; - DOMString fill; - DOMString fillColor; - -}; - //######################################################################## //# O U T P U T //######################################################################## -void OdfOutput::po(char *str) -{ - if (str) - while (*str) - outs.put(*str++); -} - - - @@ -192,35 +88,48 @@ void OdfOutput::po(char *str) * This function searches the Repr tree recursively from the given node, * and adds refs to all nodes with the given name, to the result vector */ -static void -findElementsByTagName(std::vector &results, - Inkscape::XML::Node *node, - char const *name) +void +OdfOutput::preprocess(ZipFile &zf, Inkscape::XML::Node *node) { - if ( !name || strcmp(node->name(), name) == 0 ) + + std::string nodeName = node->name(); + + if (nodeName == "image") { - results.push_back(node); + char *hrefs = (char *)node->attribute("xlink:href"); + if (hrefs) + { + std::string oldName = hrefs; + if (imageTable.find(oldName) == imageTable.end()) + { + char buf[16]; + snprintf(buf, 15, "Pictures/image%d", imageTable.size()); + std::string newName = buf; + imageTable[oldName] = newName; + std::string comment = "old name was: "; + comment.append(oldName); + ZipEntry *ze = zf.addFile(oldName, comment); + if (ze) + { + ze->setFileName(newName); + } + else + { + g_warning("Could not load image file '%s'", oldName.c_str()); + } + } + } } - for (Inkscape::XML::Node *child = node->firstChild() ; child ; child = child->next()) - findElementsByTagName( results, child, name ); - -} +#if 0 -/** - * This function searches the Repr tree recursively from the given node, - * and adds refs to all nodes with the given name, to the result vector - */ -void -OdfOutput::preprocess(Inkscape::XML::Node *node) -{ //Look for style values in the svg element Inkscape::Util::List attr = node->attributeList(); for ( ; attr ; ++attr) { - DOMString attrName = (const char *)attr->key; - DOMString attrValue = (const char *)attr->value; + std::string attrName = (const char *)attr->key; + std::string attrValue = (const char *)attr->value; StyleInfo si(attrName, attrValue); /* if (styleTable.find(styleValue) != styleTable.end()) @@ -240,11 +149,11 @@ OdfOutput::preprocess(Inkscape::XML::Node *node) */ } - +#endif for (Inkscape::XML::Node *child = node->firstChild() ; child ; child = child->next()) - preprocess(child); + preprocess(zf, child); } @@ -253,83 +162,106 @@ OdfOutput::preprocess(Inkscape::XML::Node *node) * and adds refs to all nodes with the given name, to the result vector */ void -OdfOutput::preprocess(SPDocument *doc) +OdfOutput::preprocess(ZipFile &zf, SPDocument *doc) { styleTable.clear(); - styleIndex = 0; - preprocess(doc->rroot); - - outs.clear(); - - po("\n"); - po("\n"); - po("\n"); - po("\n"); - po("\n"); - po("\n"); - po("\n"); - po("\n"); - po(" \n"); - po("\n"); - po("\n"); - po(" \n"); - po("\n"); + preprocess(zf, doc->rroot); + + +} + +bool OdfOutput::writeManifest(ZipFile &zf) +{ + BufferOutputStream bouts; + OutputStreamWriter outs(bouts); + + outs.printf("\n"); + outs.printf("\n"); + outs.printf("\n"); + outs.printf(" \n"); + outs.printf(" \n"); + outs.printf(" \n"); + outs.printf(" \n"); + std::map::iterator iter; + for (iter = imageTable.begin() ; iter!=imageTable.end() ; iter++) + { + std::string oldName = iter->first; + std::string newName = iter->second; + if (oldName.size() < 5) + { + g_warning("image file name too short:%s", oldName.c_str()); + return false; + } + std::string ext = oldName.substr(oldName.size() - 4); + outs.printf(" \n"); + } + outs.printf("\n"); + + outs.close(); + + //Make our entry + ZipEntry *ze = zf.newEntry("META-INF/manifest.xml", "ODF file manifest"); + ze->setUncompressedData(bouts.getBuffer()); + ze->finish(); + + return true; +} + + + + + +bool OdfOutput::writeStyle(Writer &outs) +{ + outs.printf("\n"); + outs.printf("\n"); + outs.printf("\n"); + outs.printf(" \n"); + outs.printf("\n"); + outs.printf("\n"); + outs.printf(" \n"); + outs.printf("\n"); //## Dump our style table /* std::map::iterator iter; for (iter = styleTable.begin() ; iter != styleTable.end() ; iter++) { - po("second); - po(" style:family=\"graphic\" style:parent-style-name=\"standard\">\n"); - po(" second); + outs.printf(" style:family=\"graphic\" style:parent-style-name=\"standard\">\n"); + outs.printf(" \n"); - po("\n"); + outs.printf("/>\n"); + outs.printf("\n"); } */ - po("\n"); - po("\n"); + outs.printf("\n"); + outs.printf("\n"); + return true; } -bool OdfOutput::writeTree(Inkscape::XML::Node *node) + + +bool OdfOutput::writeTree(Writer &outs, Inkscape::XML::Node *node) { //# Get the SPItem, if applicable SPObject *reprobj = SP_ACTIVE_DOCUMENT->getObjectByRepr(node); @@ -373,7 +305,7 @@ bool OdfOutput::writeTree(Inkscape::XML::Node *node) //# Iterate through the children for (Inkscape::XML::Node *child = node->firstChild() ; child ; child = child->next()) { - if (!writeTree(child)) + if (!writeTree(outs, child)) return false; } return true; @@ -381,17 +313,84 @@ bool OdfOutput::writeTree(Inkscape::XML::Node *node) + +bool OdfOutput::writeContent(ZipFile &zf, Inkscape::XML::Node *node) +{ + BufferOutputStream bouts; + OutputStreamWriter outs(bouts); + + outs.printf("\n"); + outs.printf("\n"); + outs.printf("\n"); + outs.printf("\n"); + outs.printf("\n"); + + //if (!writeStyle(outs)) + // return false; + + //if (!writeTree(outs, node)) + // return false; + + //Make our entry + ZipEntry *ze = zf.newEntry("content.xml", "ODF master content file"); + ze->setUncompressedData(bouts.getBuffer()); + ze->finish(); + + return true; +} + + + + /** * Descends into the SVG tree, mapping things to ODF when appropriate */ void OdfOutput::save(Inkscape::Extension::Output *mod, SPDocument *doc, gchar const *uri) { - //# Preprocess the style entries. ODF does not put styles - //# directly on elements. Rather, it uses class IDs. - preprocess(doc); - ZipFile zipFile; - zipFile.writeFile(uri); + ZipFile zf; + preprocess(zf, doc); + if (!writeManifest(zf)) + { + g_warning("Failed to write manifest"); + return; + } + if (!writeContent(zf, doc->rroot)) + { + g_warning("Failed to write content"); + return; + } + if (!zf.writeFile(uri)) + { + return; + } } diff --git a/src/extension/internal/odf.h b/src/extension/internal/odf.h index 4d4421b6b..083f571d6 100644 --- a/src/extension/internal/odf.h +++ b/src/extension/internal/odf.h @@ -44,6 +44,10 @@ #include #include +#include +#include + +typedef org::w3c::dom::io::Writer Writer; namespace Inkscape { @@ -53,6 +57,71 @@ namespace Internal { + +class StyleInfo +{ +public: + + StyleInfo(const std::string &nameArg, const std::string &styleArg) + { + name = nameArg; + style = styleArg; + fill = "none"; + stroke = "none"; + } + + virtual ~StyleInfo() + {} + + std::string getName() + { + return name; + } + + std::string getCssStyle() + { + return cssStyle; + } + + std::string getStroke() + { + return stroke; + } + + std::string getStrokeColor() + { + return strokeColor; + } + + std::string getStrokeWidth() + { + return strokeWidth; + } + + + std::string getFill() + { + return fill; + } + + std::string getFillColor() + { + return fillColor; + } + + std::string name; + std::string style; + std::string cssStyle; + std::string stroke; + std::string strokeColor; + std::string strokeWidth; + std::string fill; + std::string fillColor; + +}; + + + class OdfOutput : public Inkscape::Extension::Implementation::Implementation { @@ -68,15 +137,21 @@ public: private: - int styleIndex; - std::map styleTable; + std::map styleTable; + + //for renaming image file names + std::map imageTable; + + void preprocess(ZipFile &zf, SPDocument *doc); + void preprocess(ZipFile &zf, Inkscape::XML::Node *node); + + bool writeManifest(ZipFile &zf); + + bool writeStyle(Writer &outs); - void preprocess(SPDocument *doc); - void preprocess(Inkscape::XML::Node *node); - bool writeTree(Inkscape::XML::Node *node); + bool writeTree(Writer &outs, Inkscape::XML::Node *node); - void po(char *str); - org::w3c::dom::io::StringOutputStream outs; + bool writeContent(ZipFile &zf, Inkscape::XML::Node *node); };