summaryrefslogtreecommitdiffstats
path: root/src/extension/internal/odf.cpp
blob: 97e58600d93f3b8c096feea594aafe7382896f10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/**
 * OpenDocument <drawing> input and output
 *
 * This is an an entry in the extensions mechanism to begin to enable
 * the inputting and outputting of OpenDocument Format (ODF) files from
 * within Inkscape.  Although the initial implementations will be very lossy
 * do to the differences in the models of SVG and ODF, they will hopefully
 * improve greatly with time.
 *
 * http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/idl-definitions.html
 *
 * Authors:
 *   Bob Jamison
 *
 * Copyright (C) 2006 Bob Jamison
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */



#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "odf.h"
#include "clear-n_.h"
#include "inkscape.h"
#include "sp-path.h"
#include <style.h>
#include "display/curve.h"
#include "libnr/n-art-bpath.h"
#include "extension/system.h"

#include "xml/repr.h"
#include "xml/attribute-record.h"

#include <vector>

#include "dom/dom.h"
#include "dom/util/ziptool.h"


//# Shorthand notation
typedef org::w3c::dom::DOMString DOMString;


namespace Inkscape
{
namespace Extension
{
namespace Internal
{




class ImageInfo
{
public:

    ImageInfo(const DOMString &nameArg,
              const DOMString &newNameArg,
              const std::vector<unsigned char> &bufArg)
        {
        name    = nameArg;
        newName = newNameArg;
        buf     = bufArg;
        }

    virtual ~ImageInfo()
        {}

    DOMString getName()
        {
        return name;
        }

    DOMString getNewName()
        {
        return newName;
        }


    std::vector<unsigned char> getBuf()
        {
        return buf;
        }

    DOMString name;
    DOMString newName;
    std::vector<unsigned char>buf;

};


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++);
}




/**
 * Make sure that we are in the database
 */
bool
OdfOutput::check (Inkscape::Extension::Extension *module)
{
    /* We don't need a Key
    if (NULL == Inkscape::Extension::db.get(SP_MODULE_KEY_OUTPUT_POV))
        return FALSE;
    */

    return TRUE;
}



/**
 * 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<Inkscape::XML::AttributeRecord const> attr =
        node->attributeList();
    for ( ; attr ; ++attr)
        {
        DOMString attrName  = (const char *)attr->key;
        DOMString attrValue = (const char *)attr->value;
        StyleInfo si(attrName, attrValue);
        /*
        if (styleTable.find(styleValue) != styleTable.end())
            {
            g_message("duplicate style");
            }
        else
            {
            char buf[16];
            snprintf(buf, 15, "style%d", styleIndex++);
            std::string styleName  = buf;
            //Map from value-->name .   Looks backwards, i know
            styleTable[styleValue] = styleName;
            g_message("mapping '%s' to '%s'",
                styleValue.c_str(), styleName.c_str());
            }
        */
        }



    for (Inkscape::XML::Node *child = node->firstChild() ;
            child ; child = child->next())
        preprocess(child);
}


/**
 * 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(SPDocument *doc)
{
    styleTable.clear();
    styleIndex = 0;
    preprocess(doc->rroot);

    outs.clear();

    po("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    po("<office:document-content\n");
    po("    xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"\n");
    po("    xmlns:style=\"urn:oasis:names:tc:opendocument:xmlns:style:1.0\"\n");
    po("    xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"\n");
    po("    xmlns:table=\"urn:oasis:names:tc:opendocument:xmlns:table:1.0\"\n");
    po("    xmlns:draw=\"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0\"\n");
    po("    xmlns:fo=\"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0\"\n");
    po("    xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n");
    po("    xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n");
    po("    xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"\n");
    po("    xmlns:number=\"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0\"\n");
    po("    xmlns:presentation=\"urn:oasis:names:tc:opendocument:xmlns:presentation:1.0\"\n");
    po("    xmlns:svg=\"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0\"\n");
    po("    xmlns:chart=\"urn:oasis:names:tc:opendocument:xmlns:chart:1.0\"\n");
    po("    xmlns:dr3d=\"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0\"\n");
    po("    xmlns:math=\"http://www.w3.org/1998/Math/MathML\"\n");
    po("    xmlns:form=\"urn:oasis:names:tc:opendocument:xmlns:form:1.0\"\n");
    po("    xmlns:script=\"urn:oasis:names:tc:opendocument:xmlns:script:1.0\"\n");
    po("    xmlns:ooo=\"http://openoffice.org/2004/office\"\n");
    po("    xmlns:ooow=\"http://openoffice.org/2004/writer\"\n");
    po("    xmlns:oooc=\"http://openoffice.org/2004/calc\"\n");
    po("    xmlns:dom=\"http://www.w3.org/2001/xml-events\"\n");
    po("    xmlns:xforms=\"http://www.w3.org/2002/xforms\"\n");
    po("    xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n");
    po("    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
    po("    xmlns:smil=\"urn:oasis:names:tc:opendocument:xmlns:smil-compatible:1.0\"\n");
    po("    xmlns:anim=\"urn:oasis:names:tc:opendocument:xmlns:animation:1.0\"\n");
    po("    office:version=\"1.0\">\n");
    po("\n");
    po("\n");
    po("<office:scripts/>\n");
    po("<office:automatic-styles>\n");
    po("<style:style style:name=\"dp1\" style:family=\"drawing-page\"/>\n");
    po("<style:style style:name=\"grx1\" style:family=\"graphic\" style:parent-style-name=\"standard\">\n");
    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");
    po("</style:style>\n");
    po("<style:style style:name=\"P1\" style:family=\"paragraph\">\n");
    po("  <style:paragraph-properties fo:text-align=\"center\"/>\n");
    po("</style:style>\n");

    //##  Dump our style table
    /*
    std::map<std::string, std::string>::iterator iter;
    for (iter = styleTable.begin() ; iter != styleTable.end() ; iter++)
        {
        po("<style:style style:name=\"%s\"", iter->second);
        po(" style:family=\"graphic\" style:parent-style-name=\"standard\">\n");
        po("  <style:graphic-properties");
        po(" draw:fill=\"" + s.getFill() + "\"");
        if (!s.getFill().equals("none"))
            po(" draw:fill-color=\"" + s.getFillColor() + "\"");
        po(" draw:stroke=\"" + s.getStroke() + "\"");
        if (!s.getStroke().equals("none"))
            {
            po(" svg:stroke-width=\"" + s.getStrokeWidth() + "\"");
            po(" svg:stroke-color=\"" + s.getStrokeColor() + "\"");
            }
        po("/>\n");
        po("</style:style>\n");
        }
    */
    po("</office:automatic-styles>\n");
    po("\n");

}




/**
 * 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<Inkscape::XML::Node *> &results,
                      Inkscape::XML::Node *node,
                      char const *name)
{
    if ( !name || strcmp(node->name(), name) == 0 )
        {
        results.push_back(node);
        }

    for (Inkscape::XML::Node *child = node->firstChild() ; child ; child = child->next())
        findElementsByTagName( results, child, name );

}


/**
 * 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);
}


/**
 * This is the definition of PovRay output.  This function just
 * calls the extension system with the memory allocated XML that
 * describes the data.
*/
void
OdfOutput::init()
{
    Inkscape::Extension::build_from_mem(
        "<inkscape-extension>\n"
            "<name>" N_("OpenDocument Drawing Output") "</name>\n"
            "<id>org.inkscape.output.odf</id>\n"
            "<output>\n"
                "<extension>.odg</extension>\n"
                "<mimetype>text/x-povray-script</mimetype>\n"
                "<filetypename>" N_("OpenDocument drawing (*.odg)(placeholder)") "</filetypename>\n"
                "<filetypetooltip>" N_("OpenDocument drawing file") "</filetypetooltip>\n"
            "</output>\n"
        "</inkscape-extension>",
        new OdfOutput());
}

//########################################################################
//# I N P U T
//########################################################################




}  //namespace Internal
}  //namespace Extension
}  //namespace Inkscape


/*
  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:encoding=utf-8:textwidth=99 :