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"
59 //# Shorthand notation
60 typedef org::w3c::dom::DOMString DOMString;
63 namespace Inkscape
64 {
65 namespace Extension
66 {
67 namespace Internal
68 {
73 class ImageInfo
74 {
75 public:
77 ImageInfo(const DOMString &nameArg,
78 const DOMString &newNameArg,
79 const std::vector<unsigned char> &bufArg)
80 {
81 name = nameArg;
82 newName = newNameArg;
83 buf = bufArg;
84 }
86 virtual ~ImageInfo()
87 {}
89 DOMString getName()
90 {
91 return name;
92 }
94 DOMString getNewName()
95 {
96 return newName;
97 }
100 std::vector<unsigned char> getBuf()
101 {
102 return buf;
103 }
105 DOMString name;
106 DOMString newName;
107 std::vector<unsigned char>buf;
109 };
112 class StyleInfo
113 {
114 public:
116 StyleInfo(const DOMString &nameArg, const DOMString &styleArg)
117 {
118 name = nameArg;
119 style = styleArg;
120 fill = "none";
121 stroke = "none";
122 }
124 virtual ~StyleInfo()
125 {}
127 DOMString getName()
128 {
129 return name;
130 }
132 DOMString getCssStyle()
133 {
134 return cssStyle;
135 }
137 DOMString getStroke()
138 {
139 return stroke;
140 }
142 DOMString getStrokeColor()
143 {
144 return strokeColor;
145 }
147 DOMString getStrokeWidth()
148 {
149 return strokeWidth;
150 }
153 DOMString getFill()
154 {
155 return fill;
156 }
158 DOMString getFillColor()
159 {
160 return fillColor;
161 }
163 DOMString name;
164 DOMString style;
165 DOMString cssStyle;
166 DOMString stroke;
167 DOMString strokeColor;
168 DOMString strokeWidth;
169 DOMString fill;
170 DOMString fillColor;
172 };
175 //########################################################################
176 //# O U T P U T
177 //########################################################################
179 void OdfOutput::po(char *str)
180 {
181 if (str)
182 while (*str)
183 outs.put(*str++);
184 }
191 /**
192 * This function searches the Repr tree recursively from the given node,
193 * and adds refs to all nodes with the given name, to the result vector
194 */
195 static void
196 findElementsByTagName(std::vector<Inkscape::XML::Node *> &results,
197 Inkscape::XML::Node *node,
198 char const *name)
199 {
200 if ( !name || strcmp(node->name(), name) == 0 )
201 {
202 results.push_back(node);
203 }
205 for (Inkscape::XML::Node *child = node->firstChild() ; child ; child = child->next())
206 findElementsByTagName( results, child, name );
208 }
210 /**
211 * This function searches the Repr tree recursively from the given node,
212 * and adds refs to all nodes with the given name, to the result vector
213 */
214 void
215 OdfOutput::preprocess(Inkscape::XML::Node *node)
216 {
217 //Look for style values in the svg element
218 Inkscape::Util::List<Inkscape::XML::AttributeRecord const> attr =
219 node->attributeList();
220 for ( ; attr ; ++attr)
221 {
222 DOMString attrName = (const char *)attr->key;
223 DOMString attrValue = (const char *)attr->value;
224 StyleInfo si(attrName, attrValue);
225 /*
226 if (styleTable.find(styleValue) != styleTable.end())
227 {
228 g_message("duplicate style");
229 }
230 else
231 {
232 char buf[16];
233 snprintf(buf, 15, "style%d", styleIndex++);
234 std::string styleName = buf;
235 //Map from value-->name . Looks backwards, i know
236 styleTable[styleValue] = styleName;
237 g_message("mapping '%s' to '%s'",
238 styleValue.c_str(), styleName.c_str());
239 }
240 */
241 }
245 for (Inkscape::XML::Node *child = node->firstChild() ;
246 child ; child = child->next())
247 preprocess(child);
248 }
251 /**
252 * This function searches the Repr tree recursively from the given node,
253 * and adds refs to all nodes with the given name, to the result vector
254 */
255 void
256 OdfOutput::preprocess(SPDocument *doc)
257 {
258 styleTable.clear();
259 styleIndex = 0;
260 preprocess(doc->rroot);
262 outs.clear();
264 po("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
265 po("<office:document-content\n");
266 po(" xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"\n");
267 po(" xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\"\n");
268 po(" xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"\n");
269 po(" xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"\n");
270 po(" xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\"\n");
271 po(" xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\"\n");
272 po(" xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n");
273 po(" xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n");
274 po(" xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"\n");
275 po(" xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\"\n");
276 po(" xmlns:presentation=\"urn:oasis:names:tc:opendocument:xmlns:presentation:1.0\"\n");
277 po(" xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\"\n");
278 po(" xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\"\n");
279 po(" xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\"\n");
280 po(" xmlns:math=\"http://www.w3.org/1998/Math/MathML\"\n");
281 po(" xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\"\n");
282 po(" xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\"\n");
283 po(" xmlns:ooo=\"http://openoffice.org/2004/office\"\n");
284 po(" xmlns:ooow=\"http://openoffice.org/2004/writer\"\n");
285 po(" xmlns:oooc=\"http://openoffice.org/2004/calc\"\n");
286 po(" xmlns:dom=\"http://www.w3.org/2001/xml-events\"\n");
287 po(" xmlns:xforms=\"http://www.w3.org/2002/xforms\"\n");
288 po(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n");
289 po(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
290 po(" xmlns:smil=\"urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0\"\n");
291 po(" xmlns:anim=\"urn:oasis:names:tc:opendocument:xmlns:animation:1.0\"\n");
292 po(" office:version=\"1.0\">\n");
293 po("\n");
294 po("\n");
295 po("<office:scripts/>\n");
296 po("<office:automatic-styles>\n");
297 po("<style:style style:name=\"dp1\" style:family=\"drawing-page\"/>\n");
298 po("<style:style style:name=\"grx1\" style:family=\"graphic\" style:parent-style-name=\"standard\">\n");
299 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");
300 po("</style:style>\n");
301 po("<style:style style:name=\"P1\" style:family=\"paragraph\">\n");
302 po(" <style:paragraph-properties fo:text-align=\"center\"/>\n");
303 po("</style:style>\n");
305 //## Dump our style table
306 /*
307 std::map<std::string, std::string>::iterator iter;
308 for (iter = styleTable.begin() ; iter != styleTable.end() ; iter++)
309 {
310 po("<style:style style:name=\"%s\"", iter->second);
311 po(" style:family=\"graphic\" style:parent-style-name=\"standard\">\n");
312 po(" <style:graphic-properties");
313 po(" draw:fill=\"" + s.getFill() + "\"");
314 if (!s.getFill().equals("none"))
315 po(" draw:fill-color=\"" + s.getFillColor() + "\"");
316 po(" draw:stroke=\"" + s.getStroke() + "\"");
317 if (!s.getStroke().equals("none"))
318 {
319 po(" svg:stroke-width=\"" + s.getStrokeWidth() + "\"");
320 po(" svg:stroke-color=\"" + s.getStrokeColor() + "\"");
321 }
322 po("/>\n");
323 po("</style:style>\n");
324 }
325 */
326 po("</office:automatic-styles>\n");
327 po("\n");
329 }
332 bool OdfOutput::writeTree(Inkscape::XML::Node *node)
333 {
334 //# Get the SPItem, if applicable
335 SPObject *reprobj = SP_ACTIVE_DOCUMENT->getObjectByRepr(node);
336 if (!reprobj)
337 return true;
338 if (!SP_IS_ITEM(reprobj))
339 {
340 return true;
341 }
342 SPItem *item = SP_ITEM(reprobj);
344 //# Do our stuff
345 SPCurve *curve = NULL;
346 if (SP_IS_SHAPE(item))
347 {
348 curve = sp_shape_get_curve(SP_SHAPE(item));
349 }
350 else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item))
351 {
352 curve = te_get_layout(item)->convertToCurves();
353 }
355 if (!curve)
356 return false;
358 //Inkscape::XML::Node *repr = sp_repr_new("svg:path");
359 /* Transformation */
360 //repr->setAttribute("transform", SP_OBJECT_REPR(item)->attribute("transform"));
362 /* Rotation center */
363 //sp_repr_set_attr(repr, "inkscape:transform-center-x", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-x"));
364 //sp_repr_set_attr(repr, "inkscape:transform-center-y", SP_OBJECT_REPR(item)->attribute("inkscape:transform-center-y"));
366 /* Definition */
367 gchar *def_str = sp_svg_write_path(curve->bpath);
369 g_free(def_str);
370 sp_curve_unref(curve);
373 //# Iterate through the children
374 for (Inkscape::XML::Node *child = node->firstChild() ; child ; child = child->next())
375 {
376 if (!writeTree(child))
377 return false;
378 }
379 return true;
380 }
384 /**
385 * Descends into the SVG tree, mapping things to ODF when appropriate
386 */
387 void
388 OdfOutput::save(Inkscape::Extension::Output *mod, SPDocument *doc, gchar const *uri)
389 {
390 //# Preprocess the style entries. ODF does not put styles
391 //# directly on elements. Rather, it uses class IDs.
392 preprocess(doc);
393 ZipFile zipFile;
394 zipFile.writeFile(uri);
395 }
398 /**
399 * This is the definition of PovRay output. This function just
400 * calls the extension system with the memory allocated XML that
401 * describes the data.
402 */
403 void
404 OdfOutput::init()
405 {
406 Inkscape::Extension::build_from_mem(
407 "<inkscape-extension>\n"
408 "<name>" N_("OpenDocument Drawing Output") "</name>\n"
409 "<id>org.inkscape.output.odf</id>\n"
410 "<output>\n"
411 "<extension>.odg</extension>\n"
412 "<mimetype>text/x-povray-script</mimetype>\n"
413 "<filetypename>" N_("OpenDocument drawing (*.odg)") "</filetypename>\n"
414 "<filetypetooltip>" N_("OpenDocument drawing file") "</filetypetooltip>\n"
415 "</output>\n"
416 "</inkscape-extension>",
417 new OdfOutput());
418 }
420 /**
421 * Make sure that we are in the database
422 */
423 bool
424 OdfOutput::check (Inkscape::Extension::Extension *module)
425 {
426 /* We don't need a Key
427 if (NULL == Inkscape::Extension::db.get(SP_MODULE_KEY_OUTPUT_POV))
428 return FALSE;
429 */
431 return TRUE;
432 }
434 //########################################################################
435 //# I N P U T
436 //########################################################################
441 } //namespace Internal
442 } //namespace Extension
443 } //namespace Inkscape
446 /*
447 Local Variables:
448 mode:c++
449 c-file-style:"stroustrup"
450 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
451 indent-tabs-mode:nil
452 fill-column:99
453 End:
454 */
455 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :