Code

wip
[inkscape.git] / src / extension / internal / odf.cpp
1 /**
2  * OpenDocument <drawing> input and output
3  *
4  * This is an an entry in the extensions mechanism to begin to enable
5  * the inputting and outputting of OpenDocument Format (ODF) files from
6  * within Inkscape.  Although the initial implementations will be very lossy
7  * do to the differences in the models of SVG and ODF, they will hopefully
8  * improve greatly with time.
9  *
10  * http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/idl-definitions.html
11  *
12  * Authors:
13  *   Bob Jamison
14  *
15  * Copyright (C) 2006 Bob Jamison
16  *
17  *  This library is free software; you can redistribute it and/or
18  *  modify it under the terms of the GNU Lesser General Public
19  *  License as published by the Free Software Foundation; either
20  *  version 2.1 of the License, or (at your option) any later version.
21  *
22  *  This library is distributed in the hope that it will be useful,
23  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
24  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25  *  Lesser General Public License for more details.
26  *
27  *  You should have received a copy of the GNU Lesser General Public
28  *  License along with this library; if not, write to the Free Software
29  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
30  */
34 #ifdef HAVE_CONFIG_H
35 # include <config.h>
36 #endif
37 #include "odf.h"
38 #include "clear-n_.h"
39 #include "inkscape.h"
40 #include "sp-path.h"
41 #include <style.h>
42 #include "display/curve.h"
43 #include "libnr/n-art-bpath.h"
44 #include "extension/system.h"
46 #include "xml/repr.h"
47 #include "xml/attribute-record.h"
48 #include "sp-text.h"
49 #include "sp-flowtext.h"
50 #include "svg/svg.h"
51 #include "text-editing.h"
53 #include <vector>
55 #include "dom/dom.h"
56 #include "dom/util/ziptool.h"
57 #include "dom/io/domstream.h"
58 #include "dom/io/bufferstream.h"
61 //# Shorthand notation
62 typedef org::w3c::dom::DOMString DOMString;
63 typedef org::w3c::dom::io::OutputStreamWriter OutputStreamWriter;
64 typedef org::w3c::dom::io::BufferOutputStream BufferOutputStream;
69 namespace Inkscape
70 {
71 namespace Extension
72 {
73 namespace Internal
74 {
80 //########################################################################
81 //# O U T P U T
82 //########################################################################
87 /**
88  * This function searches the Repr tree recursively from the given node,
89  * and adds refs to all nodes with the given name, to the result vector
90  */
91 void
92 OdfOutput::preprocess(ZipFile &zf, Inkscape::XML::Node *node)
93 {
95     std::string nodeName = node->name();
97     if (nodeName == "image")
98         {
99         char *hrefs = (char *)node->attribute("xlink:href");
100         if (hrefs)
101             {
102             std::string oldName = hrefs;
103             if (imageTable.find(oldName) == imageTable.end())
104                 {
105                 char buf[16];
106                 snprintf(buf, 15, "Pictures/image%d", imageTable.size());
107                 std::string newName = buf;
108                 imageTable[oldName] = newName;
109                 std::string comment = "old name was: ";
110                 comment.append(oldName);
111                 ZipEntry *ze = zf.addFile(oldName, comment);
112                 if (ze)
113                     {
114                     ze->setFileName(newName);
115                     }
116                 else
117                     {
118                     g_warning("Could not load image file '%s'", oldName.c_str());
119                     }
120                 }
121             }
122         }
124 #if 0
126     //Look for style values in the svg element
127     Inkscape::Util::List<Inkscape::XML::AttributeRecord const> attr =
128         node->attributeList();
129     for ( ; attr ; ++attr)
130         {
131         std::string attrName  = (const char *)attr->key;
132         std::string attrValue = (const char *)attr->value;
133         StyleInfo si(attrName, attrValue);
134         /*
135         if (styleTable.find(styleValue) != styleTable.end())
136             {
137             g_message("duplicate style");
138             }
139         else
140             {
141             char buf[16];
142             snprintf(buf, 15, "style%d", styleIndex++);
143             std::string styleName  = buf;
144             //Map from value-->name .   Looks backwards, i know
145             styleTable[styleValue] = styleName;
146             g_message("mapping '%s' to '%s'",
147                 styleValue.c_str(), styleName.c_str());
148             }
149         */
150         }
152 #endif
154     for (Inkscape::XML::Node *child = node->firstChild() ;
155             child ; child = child->next())
156         preprocess(zf, child);
160 /**
161  * This function searches the Repr tree recursively from the given node,
162  * and adds refs to all nodes with the given name, to the result vector
163  */
164 void
165 OdfOutput::preprocess(ZipFile &zf, SPDocument *doc)
167     styleTable.clear();
168     preprocess(zf, doc->rroot);
173 bool OdfOutput::writeManifest(ZipFile &zf)
175     BufferOutputStream bouts;
176     OutputStreamWriter outs(bouts);
178     outs.printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
179     outs.printf("<!DOCTYPE manifest:manifest PUBLIC \"-//OpenOffice.org//DTD Manifest 1.0//EN\" \"Manifest.dtd\">\n");
180     outs.printf("<manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\">\n");
181     outs.printf("    <manifest:file-entry manifest:media-type=\"application/vnd.oasis.opendocument.graphics\" manifest:full-path=\"/\"/>\n");
182     outs.printf("    <manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"content.xml\"/>\n");
183     outs.printf("    <manifest:file-entry manifest:media-type=\"text/xml\" manifest:full-path=\"meta.xml\"/>\n");
184     outs.printf("    <!--List our images here-->\n");
185     std::map<std::string, std::string>::iterator iter;
186     for (iter = imageTable.begin() ; iter!=imageTable.end() ; iter++)
187         {
188         std::string oldName = iter->first;
189         std::string newName = iter->second;
190         if (oldName.size() < 5)
191             {
192             g_warning("image file name too short:%s", oldName.c_str());
193             return false;
194             }
195         std::string ext = oldName.substr(oldName.size() - 4);
196         outs.printf("    <manifest:file-entry manifest:media-type=\"");
197         if (ext == ".gif")
198             outs.printf("image/gif");
199         else if (ext == ".png")
200             outs.printf("image/png");
201         else if (ext == ".jpg" || ext == ".jpeg")
202             outs.printf("image/jpeg");
203         outs.printf("\" manifest:full-path=\"");
204         outs.printf((char *)newName.c_str());
205         outs.printf("\"/>\n");
206         }
207     outs.printf("</manifest:manifest>\n");
209     outs.close();
211     //Make our entry
212     ZipEntry *ze = zf.newEntry("META-INF/manifest.xml", "ODF file manifest");
213     ze->setUncompressedData(bouts.getBuffer());
214     ze->finish();
216     return true;
223 bool OdfOutput::writeStyle(Writer &outs)
225     outs.printf("<office:automatic-styles>\n");
226     outs.printf("<style:style style:name=\"dp1\" style:family=\"drawing-page\"/>\n");
227     outs.printf("<style:style style:name=\"grx1\" style:family=\"graphic\" style:parent-style-name=\"standard\">\n");
228     outs.printf("  <style:graphic-properties draw:stroke=\"none\" draw:fill=\"solid\" draw:textarea-horizontal-align=\"center\" draw:textarea-vertical-align=\"middle\" draw:color-mode=\"standard\" draw:luminance=\"0%\" draw:contrast=\"0%\" draw:gamma=\"100%\" draw:red=\"0%\" draw:green=\"0%\" draw:blue=\"0%\" fo:clip=\"rect(0cm 0cm 0cm 0cm)\" draw:image-opacity=\"100%\" style:mirror=\"none\"/>\n");
229     outs.printf("</style:style>\n");
230     outs.printf("<style:style style:name=\"P1\" style:family=\"paragraph\">\n");
231     outs.printf("  <style:paragraph-properties fo:text-align=\"center\"/>\n");
232     outs.printf("</style:style>\n");
234     //##  Dump our style table
235     /*
236     std::map<std::string, std::string>::iterator iter;
237     for (iter = styleTable.begin() ; iter != styleTable.end() ; iter++)
238         {
239         outs.printf("<style:style style:name=\"%s\"", iter->second);
240         outs.printf(" style:family=\"graphic\" style:parent-style-name=\"standard\">\n");
241         outs.printf("  <style:graphic-properties");
242         outs.printf(" draw:fill=\"" + s.getFill() + "\"");
243         if (!s.getFill().equals("none"))
244             outs.printf(" draw:fill-color=\"" + s.getFillColor() + "\"");
245         outs.printf(" draw:stroke=\"" + s.getStroke() + "\"");
246         if (!s.getStroke().equals("none"))
247             {
248             outs.printf(" svg:stroke-width=\"" + s.getStrokeWidth() + "\"");
249             outs.printf(" svg:stroke-color=\"" + s.getStrokeColor() + "\"");
250             }
251         outs.printf("/>\n");
252         outs.printf("</style:style>\n");
253         }
254     */
255     outs.printf("</office:automatic-styles>\n");
256     outs.printf("\n");
258     return true;
264 bool OdfOutput::writeTree(Writer &outs, Inkscape::XML::Node *node)
266     //# Get the SPItem, if applicable
267     SPObject *reprobj = SP_ACTIVE_DOCUMENT->getObjectByRepr(node);
268     if (!reprobj)
269         return true;
270     if (!SP_IS_ITEM(reprobj))
271         {
272         return true;
273         }
274     SPItem *item = SP_ITEM(reprobj);
276     //# Do our stuff
277     SPCurve *curve = NULL;
278     if (SP_IS_SHAPE(item))
279         {
280         curve = sp_shape_get_curve(SP_SHAPE(item));
281         }
282     else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item))
283         {
284         curve = te_get_layout(item)->convertToCurves();
285         }
287     if (!curve)
288         return false;
290     //Inkscape::XML::Node *repr = sp_repr_new("svg:path");
291     /* Transformation */
292     //repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
294     /* Rotation center */
295     //sp_repr_set_attr(repr, "inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"));
296     //sp_repr_set_attr(repr, "inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"));
298     /* Definition */
299     gchar *def_str = sp_svg_write_path(curve->bpath);
301     g_free(def_str);
302     sp_curve_unref(curve);
305     //# Iterate through the children
306     for (Inkscape::XML::Node *child = node->firstChild() ; child ; child = child->next())
307         {
308         if (!writeTree(outs, child))
309             return false;
310         }
311     return true;
317 bool OdfOutput::writeContent(ZipFile &zf, Inkscape::XML::Node *node)
319     BufferOutputStream bouts;
320     OutputStreamWriter outs(bouts);
322     outs.printf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
323     outs.printf("<office:document-content\n");
324     outs.printf("    xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"\n");
325     outs.printf("    xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\"\n");
326     outs.printf("    xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"\n");
327     outs.printf("    xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"\n");
328     outs.printf("    xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\"\n");
329     outs.printf("    xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\"\n");
330     outs.printf("    xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n");
331     outs.printf("    xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n");
332     outs.printf("    xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"\n");
333     outs.printf("    xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\"\n");
334     outs.printf("    xmlns:presentation=\"urn:oasis:names:tc:opendocument:xmlns:presentation:1.0\"\n");
335     outs.printf("    xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\"\n");
336     outs.printf("    xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\"\n");
337     outs.printf("    xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\"\n");
338     outs.printf("    xmlns:math=\"http://www.w3.org/1998/Math/MathML\"\n");
339     outs.printf("    xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\"\n");
340     outs.printf("    xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\"\n");
341     outs.printf("    xmlns:ooo=\"http://openoffice.org/2004/office\"\n");
342     outs.printf("    xmlns:ooow=\"http://openoffice.org/2004/writer\"\n");
343     outs.printf("    xmlns:oooc=\"http://openoffice.org/2004/calc\"\n");
344     outs.printf("    xmlns:dom=\"http://www.w3.org/2001/xml-events\"\n");
345     outs.printf("    xmlns:xforms=\"http://www.w3.org/2002/xforms\"\n");
346     outs.printf("    xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n");
347     outs.printf("    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
348     outs.printf("    xmlns:smil=\"urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0\"\n");
349     outs.printf("    xmlns:anim=\"urn:oasis:names:tc:opendocument:xmlns:animation:1.0\"\n");
350     outs.printf("    office:version=\"1.0\">\n");
351     outs.printf("\n");
352     outs.printf("\n");
353     outs.printf("<office:scripts/>\n");
355     //if (!writeStyle(outs))
356     //    return false;
358     //if (!writeTree(outs, node))
359     //    return false;
361     //Make our entry
362     ZipEntry *ze = zf.newEntry("content.xml", "ODF master content file");
363     ze->setUncompressedData(bouts.getBuffer());
364     ze->finish();
366     return true;
372 /**
373  * Descends into the SVG tree, mapping things to ODF when appropriate
374  */
375 void
376 OdfOutput::save(Inkscape::Extension::Output *mod, SPDocument *doc, gchar const *uri)
378     ZipFile zf;
379     preprocess(zf, doc);
380     if (!writeManifest(zf))
381         {
382         g_warning("Failed to write manifest");
383         return;
384         }
385     if (!writeContent(zf, doc->rroot))
386         {
387         g_warning("Failed to write content");
388         return;
389         }
390     if (!zf.writeFile(uri))
391         {
392         return;
393         }
397 /**
398  * This is the definition of PovRay output.  This function just
399  * calls the extension system with the memory allocated XML that
400  * describes the data.
401 */
402 void
403 OdfOutput::init()
405     Inkscape::Extension::build_from_mem(
406         "<inkscape-extension>\n"
407             "<name>" N_("OpenDocument Drawing Output") "</name>\n"
408             "<id>org.inkscape.output.odf</id>\n"
409             "<output>\n"
410                 "<extension>.odg</extension>\n"
411                 "<mimetype>text/x-povray-script</mimetype>\n"
412                 "<filetypename>" N_("OpenDocument drawing (*.odg)") "</filetypename>\n"
413                 "<filetypetooltip>" N_("OpenDocument drawing file") "</filetypetooltip>\n"
414             "</output>\n"
415         "</inkscape-extension>",
416         new OdfOutput());
419 /**
420  * Make sure that we are in the database
421  */
422 bool
423 OdfOutput::check (Inkscape::Extension::Extension *module)
425     /* We don't need a Key
426     if (NULL == Inkscape::Extension::db.get(SP_MODULE_KEY_OUTPUT_POV))
427         return FALSE;
428     */
430     return TRUE;
433 //########################################################################
434 //# I N P U T
435 //########################################################################
440 }  //namespace Internal
441 }  //namespace Extension
442 }  //namespace Inkscape
445 /*
446   Local Variables:
447   mode:c++
448   c-file-style:"stroustrup"
449   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
450   indent-tabs-mode:nil
451   fill-column:99
452   End:
453 */
454 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :