~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to filters/libmso/ODrawToOdf.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-21 15:36:35 UTC
  • mfrom: (1.4.1 upstream) (60.2.11 maverick)
  • Revision ID: james.westby@ubuntu.com-20100921153635-6tejqkiro2u21ydi
Tags: 1:2.2.2-0ubuntu3
Add kubuntu_03_fix-crash-on-closing-sqlite-connection-2.2.2.diff and
kubuntu_04_support-large-memo-values-for-msaccess-2.2.2.diff as
recommended by upstream http://kexi-
project.org/wiki/wikiview/index.php@Kexi2.2_Patches.html#sqlite_stab
ility

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the KDE project
 
2
   Copyright (C) 2010 KO GmbH <jos.van.den.oever@kogmbh.com>
 
3
 
 
4
   This library is free software; you can redistribute it and/or
 
5
   modify it under the terms of the GNU Library General Public
 
6
   License as published by the Free Software Foundation; either
 
7
   version 2 of the License, or (at your option) any later version.
 
8
 
 
9
   This library is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
   Library General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU Library General Public License
 
15
   along with this library; see the file COPYING.LIB.  If not, write to
 
16
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
 * Boston, MA 02110-1301, USA.
 
18
*/
 
19
 
 
20
#include "ODrawToOdf.h"
 
21
#include "drawstyle.h"
 
22
#include <KoXmlWriter.h>
 
23
#include <QtCore/QtDebug>
 
24
#include <QtGui/QColor>
 
25
 
 
26
using namespace MSO;
 
27
 
 
28
/**
 
29
 * Return the bounding rectangle for this object.
 
30
 **/
 
31
QRect
 
32
ODrawToOdf::getRect(const OfficeArtFSPGR &r)
 
33
{
 
34
    return QRect(r.xLeft, r.yTop, r.xRight - r.xLeft, r.yBottom - r.yTop);
 
35
}
 
36
 
 
37
void ODrawToOdf::processDrawing(const OfficeArtSpgrContainerFileBlock& of,
 
38
                                Writer& out)
 
39
{
 
40
    if (of.anon.is<OfficeArtSpgrContainer>()) {
 
41
        processGroup(*of.anon.get<OfficeArtSpgrContainer>(), out);
 
42
    } else { // OfficeArtSpContainer
 
43
        processDrawingObject(*of.anon.get<OfficeArtSpContainer>(), out);
 
44
    }
 
45
}
 
46
void ODrawToOdf::processGroup(const MSO::OfficeArtSpgrContainer& o, Writer& out)
 
47
{
 
48
    if (o.rgfb.size() < 2) return;
 
49
    out.xml.startElement("draw:g");
 
50
    /* if the first OfficeArtSpContainer has a clientAnchor,
 
51
       a new coordinate system is introduced.
 
52
       */
 
53
    const OfficeArtSpContainer* first
 
54
        = o.rgfb[0].anon.get<OfficeArtSpContainer>();
 
55
    QRect oldCoords;
 
56
    if (first && first->shapeGroup && first->clientAnchor) {
 
57
        oldCoords = client->getRect(*first->clientAnchor);
 
58
    }
 
59
    if (oldCoords.isValid()) {
 
60
        QRect newCoords = getRect(*first->shapeGroup);
 
61
        Writer transformedOut = out.transform(oldCoords, newCoords);
 
62
        for (int i = 1; i < o.rgfb.size(); ++i) {
 
63
            processDrawing(o.rgfb[i], transformedOut);
 
64
        }
 
65
    } else {
 
66
        for (int i = 1; i < o.rgfb.size(); ++i) {
 
67
            processDrawing(o.rgfb[i], out);
 
68
        }
 
69
    }
 
70
    out.xml.endElement(); // draw:g
 
71
}
 
72
void ODrawToOdf::addGraphicStyleToDrawElement(Writer& out,
 
73
                                            const OfficeArtSpContainer& o)
 
74
{
 
75
    KoGenStyle style;
 
76
    const OfficeArtDggContainer* drawingGroup = 0;
 
77
    if (client) {
 
78
        style = client->createGraphicStyle(o.clientTextbox.data(),
 
79
                                           o.clientData.data(), out);
 
80
        drawingGroup = client->getOfficeArtDggContainer();
 
81
    }
 
82
    if (!drawingGroup) return;
 
83
 
 
84
    const DrawStyle ds(*drawingGroup, &o);
 
85
    defineGraphicProperties(style, ds);
 
86
 
 
87
    client->addTextStyles(o.clientTextbox.data(),
 
88
                          o.clientData.data(), out, style);
 
89
}
 
90
 
 
91
namespace
 
92
{
 
93
const char* dashses[11] = {
 
94
    "", "Dash_20_2", "Dash_20_3", "Dash_20_2", "Dash_20_2", "Dash_20_2",
 
95
    "Dash_20_4", "Dash_20_6", "Dash_20_5", "Dash_20_7", "Dash_20_8"
 
96
};
 
97
const char* arrowHeads[6] = {
 
98
    "", "msArrowEnd_20_5", "msArrowStealthEnd_20_5", "msArrowDiamondEnd_20_5",
 
99
    "msArrowOvalEnd_20_5", "msArrowOpenEnd_20_5"
 
100
};
 
101
QString format(double v) {
 
102
    static const QString f("%1");
 
103
    static const QString e("");
 
104
    static const QRegExp r("\\.?0+$");
 
105
    return f.arg(v, 0, 'f').replace(r, e);
 
106
}
 
107
QString pt(double v) {
 
108
    static const QString pt("pt");
 
109
    return format(v) + pt;
 
110
}
 
111
QString percent(double v) {
 
112
    return format(v) + '%';
 
113
}
 
114
}
 
115
void ODrawToOdf::defineGraphicProperties(KoGenStyle& style, const DrawStyle& ds,
 
116
                                       const QString& listStyle)
 
117
{
 
118
    const KoGenStyle::PropertyType gt = KoGenStyle::GraphicType;
 
119
    // dr3d:ambient-color
 
120
    // dr3d:back-scale
 
121
    // dr3d:backface-culling
 
122
    // dr3d:close-back
 
123
    // dr3d:close-front
 
124
    // dr3d:depth
 
125
    // dr3d:diffuse-color
 
126
    // dr3d:edge-rounding
 
127
    // dr3d:edge-rounding-mode
 
128
    // dr3d:emissive-color
 
129
    // dr3d:end-angle
 
130
    // dr3d:horizontal-segments
 
131
    // dr3d:lighting-mode
 
132
    // dr3d:normals-direction
 
133
    // dr3d:normals-kind
 
134
    // dr3d:shadow
 
135
    // dr3d:shininess
 
136
    // dr3d:specular-color
 
137
    // dr3d:texture-filter
 
138
    // dr3d:texture-generation-mode-x
 
139
    // dr3d:texture-generation-mode-y
 
140
    // dr3d:texture-kind
 
141
    // dr3d:texture-mode
 
142
    // dr3d:vertical-segments
 
143
    // draw:auto-grow-height
 
144
    // draw:auto-grow-width
 
145
    // draw:blue
 
146
    // draw:caption-angle
 
147
    // draw:caption-angle-type
 
148
    // draw:caption-escape
 
149
    // draw:caption-escape-direction
 
150
    // draw:caption-fit-line-length
 
151
    // draw:caption-gap
 
152
    // draw:caption-line-length
 
153
    // draw:caption-type
 
154
    // draw:color-inversion
 
155
    // draw:color-mode
 
156
    // draw:contrast
 
157
    // draw:decimal-places
 
158
    // draw:end-guide
 
159
    // draw:end-line-spacing-horizontal
 
160
    // draw:end-line-spacing-vertical
 
161
    // draw:fill ("bitmap", "gradient", "hatch", "none" or "solid")
 
162
    qint32 fillType = ds.fillType();
 
163
    if (ds.fFilled()) {
 
164
        style.addProperty("draw:fill", getFillType(fillType), gt);
 
165
    } else {
 
166
        style.addProperty("draw:fill", "none", gt);
 
167
    }
 
168
    // draw:fill-color
 
169
    // only set the color if the fill type is 'solid' because OOo ignores
 
170
    // fill='none' if the color is set
 
171
    if (fillType == 0 && client) {
 
172
        style.addProperty("draw:fill-color",
 
173
                          client->toQColor(ds.fillColor()).name(), gt);
 
174
    }
 
175
    // draw:fill-gradient-name
 
176
    // draw:fill-hatch-name
 
177
    // draw:fill-hatch-solid
 
178
    // draw:fill-image-height
 
179
    // draw:fill-image-name
 
180
    quint32 fillBlip = ds.fillBlip();
 
181
    QString fillImagePath;
 
182
    if (client) {
 
183
        fillImagePath = client->getPicturePath(fillBlip);
 
184
    }
 
185
    if (!fillImagePath.isEmpty()) {
 
186
        style.addProperty("draw:fill-image-name",
 
187
                          "fillImage" + QString::number(fillBlip), gt);
 
188
    }
 
189
    // draw:fill-image-ref-point
 
190
    // draw:fill-image-ref-point-x
 
191
    // draw:fill-image-ref-point-y
 
192
    // draw:fill-image-width
 
193
    // draw:fit-to-contour
 
194
    // draw:fit-to-size
 
195
    // draw:frame-display-border
 
196
    // draw:frame-display-scrollbar
 
197
    // draw:frame-margin-horizontal
 
198
    // draw:frame-margin-vertical
 
199
    // draw:gamma
 
200
    // draw:gradient-step-count
 
201
    // draw:green
 
202
    // draw:guide-distance
 
203
    // draw:guide-overhang
 
204
    // draw:image-opacity
 
205
    // draw:line-distance
 
206
    // draw:luminance
 
207
    // draw:marker-end
 
208
    quint32 lineEndArrowhead = ds.lineEndArrowhead();
 
209
    if (lineEndArrowhead > 0 && lineEndArrowhead < 6) {
 
210
        style.addProperty("draw:marker-end", arrowHeads[lineEndArrowhead], gt);
 
211
    }
 
212
    // draw:marker-end-center
 
213
    // draw:marker-end-width
 
214
    qreal lineWidthPt = ds.lineWidth() / 12700.;
 
215
    style.addProperty("draw:marker-end-width",
 
216
                      pt(lineWidthPt*4*(1+ds.lineEndArrowWidth())), gt);
 
217
    // draw:marker-start
 
218
    quint32 lineStartArrowhead = ds.lineStartArrowhead();
 
219
    if (lineStartArrowhead > 0 && lineStartArrowhead < 6) {
 
220
        style.addProperty("draw:marker-start", arrowHeads[lineStartArrowhead],
 
221
                          gt);
 
222
    }
 
223
    // draw:marker-start-center
 
224
    // draw:marker-start-width
 
225
    style.addProperty("draw:marker-start-width",
 
226
                      pt(lineWidthPt*4*(1+ds.lineStartArrowWidth())), gt);
 
227
    // draw:measure-align
 
228
    // draw:measure-vertical-align
 
229
    // draw:ole-draw-aspect
 
230
    // draw:opacity
 
231
    // draw:opacity-name
 
232
    // draw:parallel
 
233
    // draw:placing
 
234
    // draw:red
 
235
    // draw:secondary-fill-color
 
236
    // draw:shadow
 
237
    // draw:shadow-color
 
238
    // draw:shadow-offset-x
 
239
    style.addProperty("draw:shadow-offset-x", pt(ds.shadowOffsetX()/12700.),gt);
 
240
    // draw:shadow-offset-y
 
241
    style.addProperty("draw:shadow-offset-y", pt(ds.shadowOffsetY()/12700.),gt);
 
242
    // draw:shadow-opacity
 
243
    float shadowOpacity = toQReal(ds.shadowOpacity());
 
244
    style.addProperty("draw:shadow-opacity", percent(100*shadowOpacity), gt);
 
245
    // draw:show-unit
 
246
    // draw:start-guide
 
247
    // draw:start-line-spacing-horizontal
 
248
    // draw:start-line-spacing-vertical
 
249
    // draw:stroke ('dash', 'none' or 'solid')
 
250
    quint32 lineDashing = ds.lineDashing();
 
251
    // OOo interprets solid line with with 0 as hairline, so if
 
252
    // width == 0, stroke *must* be none to avoid OOo from
 
253
    // displaying a line
 
254
    if (lineWidthPt == 0) {
 
255
        style.addProperty("draw:stroke", "none", gt);
 
256
    } else if (ds.fLine() || ds.fNoLineDrawDash()) {
 
257
        if (lineDashing > 0 && lineDashing < 11) {
 
258
            style.addProperty("draw:stroke", "dash", gt);
 
259
        } else {
 
260
            style.addProperty("draw:stroke", "solid", gt);
 
261
        }
 
262
    } else {
 
263
        style.addProperty("draw:stroke", "none", gt);
 
264
    }
 
265
    // draw:stroke-dash from 2.3.8.17 lineDashing
 
266
    if (lineDashing > 0 && lineDashing < 11) {
 
267
        style.addProperty("draw:stroke-dash", dashses[lineDashing], gt);
 
268
    }
 
269
    // draw:stroke-dash-names
 
270
    // draw:stroke-linejoin
 
271
    // draw:symbol-color
 
272
    // draw:textarea-horizontal-align
 
273
    // draw:textarea-vertical-align
 
274
    // draw:tile-repeat-offset
 
275
    // draw:unit
 
276
    // draw:visible-area-height
 
277
    // draw:visible-area-left
 
278
    // draw:visible-area-top
 
279
    // draw:visible-area-width
 
280
    // draw:wrap-influence-on-position
 
281
    // fo:background-color
 
282
    // fo:border
 
283
    // fo:border-bottom
 
284
    // fo:border-left
 
285
    // fo:border-right
 
286
    // fo:border-top
 
287
    // fo:clip
 
288
    // fo:margin
 
289
    // fo:margin-bottom
 
290
    // fo:margin-left
 
291
    // fo:margin-right
 
292
    // fo:margin-top
 
293
    // fo:max-height
 
294
    // fo:max-width
 
295
    // fo:min-height
 
296
    // fo:min-width
 
297
    // fo:padding
 
298
    // fo:padding-bottom
 
299
    // fo:padding-left
 
300
    // fo:padding-right
 
301
    // fo:padding-top
 
302
    // fo:wrap-option
 
303
    // style:border-line-width
 
304
    // style:border-line-width-bottom
 
305
    // style:border-line-width-left
 
306
    // style:border-line-width-right
 
307
    // style:border-line-width-top
 
308
    // style:editable
 
309
    // style:flow-with-text
 
310
    // style:horizontal-pos
 
311
    // style:horizontal-rel
 
312
    // style:mirror
 
313
    // style:number-wrapped-paragraphs
 
314
    // style:overflow-behavior
 
315
    // style:print-content
 
316
    // style:protect
 
317
    // style:rel-height
 
318
    // style:rel-width
 
319
    // style:repeat
 
320
    // style:run-through
 
321
    // style:shadow
 
322
    // style:vertical-pos
 
323
    // style:vertical-rel
 
324
    // style:wrap
 
325
    // style:wrap-contour
 
326
    // style:wrap-contour-mode
 
327
    // style:wrap-dynamic-treshold
 
328
    // svg:fill-rule
 
329
    // svg:height
 
330
    // svg:stroke-color from 2.3.8.1 lineColor
 
331
    if (client) {
 
332
        style.addProperty("svg:stroke-color",
 
333
                          client->toQColor(ds.lineColor()).name(), gt);
 
334
    }
 
335
    // svg:stroke-opacity from 2.3.8.2 lineOpacity
 
336
    style.addProperty("svg:stroke-opacity",
 
337
                      percent(100.0 * ds.lineOpacity() / 0x10000), gt);
 
338
    // svg:stroke-width from 2.3.8.14 lineWidth
 
339
    style.addProperty("svg:stroke-width", pt(lineWidthPt), gt);
 
340
    // svg:width
 
341
    // svg:x
 
342
    // svg:y
 
343
    // text:anchor-page-number
 
344
    // text:anchor-type
 
345
    // text:animation
 
346
    // text:animation-delay
 
347
    // text:animation-direction
 
348
    // text:animation-repeat
 
349
    // text:animation-start-inside
 
350
    // text:animation-steps
 
351
    // text:animation-stop-inside
 
352
 
 
353
    /* associate with a text:list-style element */
 
354
    if (!listStyle.isNull()) {
 
355
        style.addAttribute("style:list-style-name", listStyle);
 
356
    }
 
357
}
 
358
const char* getFillType(quint32 fillType)
 
359
{
 
360
    switch (fillType) {
 
361
    case 2: // msofillTexture
 
362
    case 3: // msofillPicture
 
363
        return "bitmap";
 
364
    case 4: // msofillShade
 
365
    case 5: // msofillShadeCenter
 
366
    case 6: // msofillShadeShape
 
367
    case 7: // msofillShadeScale
 
368
    case 8: // msofillShadeTitle
 
369
        return "gradient";
 
370
    case 1: // msofillPattern
 
371
        return "hatch";
 
372
    case 9: // msofillBackground
 
373
        return "none";
 
374
    case 0: // msofillSolid
 
375
    default:
 
376
        return "solid";
 
377
    }
 
378
}