Code

WIP commit
[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"
49 #include <vector>
51 #include "dom/dom.h"
52 #include "dom/util/ziptool.h"
55 //# Shorthand notation
56 typedef org::w3c::dom::DOMString DOMString;
59 namespace Inkscape
60 {
61 namespace Extension
62 {
63 namespace Internal
64 {
69 class ImageInfo
70 {
71 public:
73     ImageInfo(const DOMString &nameArg,
74               const DOMString &newNameArg,
75               const std::vector<unsigned char> &bufArg)
76         {
77         name    = nameArg;
78         newName = newNameArg;
79         buf     = bufArg;
80         }
82     virtual ~ImageInfo()
83         {}
85     DOMString getName()
86         {
87         return name;
88         }
90     DOMString getNewName()
91         {
92         return newName;
93         }
96     std::vector<unsigned char> getBuf()
97         {
98         return buf;
99         }
101     DOMString name;
102     DOMString newName;
103     std::vector<unsigned char>buf;
105 };
108 class StyleInfo
110 public:
112     StyleInfo(const DOMString &nameArg, const DOMString &styleArg)
113         {
114         name   = nameArg;
115         style  = styleArg;
116         fill   = "none";
117         stroke = "none";
118         }
120     virtual ~StyleInfo()
121         {}
123     DOMString getName()
124         {
125         return name;
126         }
128     DOMString getCssStyle()
129         {
130         return cssStyle;
131         }
133     DOMString getStroke()
134         {
135         return stroke;
136         }
138     DOMString getStrokeColor()
139         {
140         return strokeColor;
141         }
143     DOMString getStrokeWidth()
144         {
145         return strokeWidth;
146         }
149     DOMString getFill()
150         {
151         return fill;
152         }
154     DOMString getFillColor()
155         {
156         return fillColor;
157         }
159     DOMString name;
160     DOMString style;
161     DOMString cssStyle;
162     DOMString stroke;
163     DOMString strokeColor;
164     DOMString strokeWidth;
165     DOMString fill;
166     DOMString fillColor;
168 };
171 //########################################################################
172 //# O U T P U T
173 //########################################################################
175 void OdfOutput::po(char *str)
177     if (str)
178         while (*str)
179             outs.put(*str++);
185 /**
186  * Make sure that we are in the database
187  */
188 bool
189 OdfOutput::check (Inkscape::Extension::Extension *module)
191     /* We don't need a Key
192     if (NULL == Inkscape::Extension::db.get(SP_MODULE_KEY_OUTPUT_POV))
193         return FALSE;
194     */
196     return TRUE;
201 /**
202  * This function searches the Repr tree recursively from the given node,
203  * and adds refs to all nodes with the given name, to the result vector
204  */
205 void
206 OdfOutput::preprocess(Inkscape::XML::Node *node)
208     //Look for style values in the svg element
209     Inkscape::Util::List<Inkscape::XML::AttributeRecord const> attr =
210         node->attributeList();
211     for ( ; attr ; ++attr)
212         {
213         DOMString attrName  = (const char *)attr->key;
214         DOMString attrValue = (const char *)attr->value;
215         StyleInfo si(attrName, attrValue);
216         /*
217         if (styleTable.find(styleValue) != styleTable.end())
218             {
219             g_message("duplicate style");
220             }
221         else
222             {
223             char buf[16];
224             snprintf(buf, 15, "style%d", styleIndex++);
225             std::string styleName  = buf;
226             //Map from value-->name .   Looks backwards, i know
227             styleTable[styleValue] = styleName;
228             g_message("mapping '%s' to '%s'",
229                 styleValue.c_str(), styleName.c_str());
230             }
231         */
232         }
236     for (Inkscape::XML::Node *child = node->firstChild() ;
237             child ; child = child->next())
238         preprocess(child);
242 /**
243  * This function searches the Repr tree recursively from the given node,
244  * and adds refs to all nodes with the given name, to the result vector
245  */
246 void
247 OdfOutput::preprocess(SPDocument *doc)
249     styleTable.clear();
250     styleIndex = 0;
251     preprocess(doc->rroot);
253     outs.clear();
255     po("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
256     po("<office:document-content\n");
257     po("    xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"\n");
258     po("    xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\"\n");
259     po("    xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"\n");
260     po("    xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"\n");
261     po("    xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\"\n");
262     po("    xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\"\n");
263     po("    xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n");
264     po("    xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n");
265     po("    xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"\n");
266     po("    xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\"\n");
267     po("    xmlns:presentation=\"urn:oasis:names:tc:opendocument:xmlns:presentation:1.0\"\n");
268     po("    xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\"\n");
269     po("    xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\"\n");
270     po("    xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\"\n");
271     po("    xmlns:math=\"http://www.w3.org/1998/Math/MathML\"\n");
272     po("    xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\"\n");
273     po("    xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\"\n");
274     po("    xmlns:ooo=\"http://openoffice.org/2004/office\"\n");
275     po("    xmlns:ooow=\"http://openoffice.org/2004/writer\"\n");
276     po("    xmlns:oooc=\"http://openoffice.org/2004/calc\"\n");
277     po("    xmlns:dom=\"http://www.w3.org/2001/xml-events\"\n");
278     po("    xmlns:xforms=\"http://www.w3.org/2002/xforms\"\n");
279     po("    xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n");
280     po("    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
281     po("    xmlns:smil=\"urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0\"\n");
282     po("    xmlns:anim=\"urn:oasis:names:tc:opendocument:xmlns:animation:1.0\"\n");
283     po("    office:version=\"1.0\">\n");
284     po("\n");
285     po("\n");
286     po("<office:scripts/>\n");
287     po("<office:automatic-styles>\n");
288     po("<style:style style:name=\"dp1\" style:family=\"drawing-page\"/>\n");
289     po("<style:style style:name=\"grx1\" style:family=\"graphic\" style:parent-style-name=\"standard\">\n");
290     po("  <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");
291     po("</style:style>\n");
292     po("<style:style style:name=\"P1\" style:family=\"paragraph\">\n");
293     po("  <style:paragraph-properties fo:text-align=\"center\"/>\n");
294     po("</style:style>\n");
296     //##  Dump our style table
297     /*
298     std::map<std::string, std::string>::iterator iter;
299     for (iter = styleTable.begin() ; iter != styleTable.end() ; iter++)
300         {
301         po("<style:style style:name=\"%s\"", iter->second);
302         po(" style:family=\"graphic\" style:parent-style-name=\"standard\">\n");
303         po("  <style:graphic-properties");
304         po(" draw:fill=\"" + s.getFill() + "\"");
305         if (!s.getFill().equals("none"))
306             po(" draw:fill-color=\"" + s.getFillColor() + "\"");
307         po(" draw:stroke=\"" + s.getStroke() + "\"");
308         if (!s.getStroke().equals("none"))
309             {
310             po(" svg:stroke-width=\"" + s.getStrokeWidth() + "\"");
311             po(" svg:stroke-color=\"" + s.getStrokeColor() + "\"");
312             }
313         po("/>\n");
314         po("</style:style>\n");
315         }
316     */
317     po("</office:automatic-styles>\n");
318     po("\n");
325 /**
326  * This function searches the Repr tree recursively from the given node,
327  * and adds refs to all nodes with the given name, to the result vector
328  */
329 static void
330 findElementsByTagName(std::vector<Inkscape::XML::Node *> &results,
331                       Inkscape::XML::Node *node,
332                       char const *name)
334     if ( !name || strcmp(node->name(), name) == 0 )
335         {
336         results.push_back(node);
337         }
339     for (Inkscape::XML::Node *child = node->firstChild() ; child ; child = child->next())
340         findElementsByTagName( results, child, name );
345 /**
346  * Descends into the SVG tree, mapping things to ODF when appropriate
347  */
348 void
349 OdfOutput::save(Inkscape::Extension::Output *mod, SPDocument *doc, gchar const *uri)
351     //# Preprocess the style entries.  ODF does not put styles
352     //# directly on elements.  Rather, it uses class IDs.
353     preprocess(doc);
354     ZipFile zipFile;
355     zipFile.writeFile(uri);
359 /**
360  * This is the definition of PovRay output.  This function just
361  * calls the extension system with the memory allocated XML that
362  * describes the data.
363 */
364 void
365 OdfOutput::init()
367     Inkscape::Extension::build_from_mem(
368         "<inkscape-extension>\n"
369             "<name>" N_("OpenDocument Drawing Output") "</name>\n"
370             "<id>org.inkscape.output.odf</id>\n"
371             "<output>\n"
372                 "<extension>.odg</extension>\n"
373                 "<mimetype>text/x-povray-script</mimetype>\n"
374                 "<filetypename>" N_("OpenDocument drawing (*.odg)(placeholder)") "</filetypename>\n"
375                 "<filetypetooltip>" N_("OpenDocument drawing file") "</filetypetooltip>\n"
376             "</output>\n"
377         "</inkscape-extension>",
378         new OdfOutput());
381 //########################################################################
382 //# I N P U T
383 //########################################################################
388 }  //namespace Internal
389 }  //namespace Extension
390 }  //namespace Inkscape
393 /*
394   Local Variables:
395   mode:c++
396   c-file-style:"stroustrup"
397   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
398   indent-tabs-mode:nil
399   fill-column:99
400   End:
401 */
402 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :