summaryrefslogtreecommitdiff
path: root/vcl/source/gdi/TypeSerializer.cxx
blob: 0b1b533720305ef8f44a62a90b00de9cfe841d4b (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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <TypeSerializer.hxx>
#include <tools/vcompat.hxx>
#include <sal/log.hxx>
#include <comphelper/fileformat.h>
#include <vcl/gdimtf.hxx>
#include <vcl/dibtools.hxx>

TypeSerializer::TypeSerializer(SvStream& rStream)
    : GenericTypeSerializer(rStream)
{
}

void TypeSerializer::readGradient(Gradient& rGradient)
{
    VersionCompat aCompat(mrStream, StreamMode::READ);

    sal_uInt16 nStyle;
    Color aStartColor;
    Color aEndColor;
    sal_uInt16 nAngle;
    sal_uInt16 nBorder;
    sal_uInt16 nOffsetX;
    sal_uInt16 nOffsetY;
    sal_uInt16 nIntensityStart;
    sal_uInt16 nIntensityEnd;
    sal_uInt16 nStepCount;

    mrStream.ReadUInt16(nStyle);
    readColor(aStartColor);
    readColor(aEndColor);
    mrStream.ReadUInt16(nAngle);
    mrStream.ReadUInt16(nBorder);
    mrStream.ReadUInt16(nOffsetX);
    mrStream.ReadUInt16(nOffsetY);
    mrStream.ReadUInt16(nIntensityStart);
    mrStream.ReadUInt16(nIntensityEnd);
    mrStream.ReadUInt16(nStepCount);

    rGradient.SetStyle(static_cast<GradientStyle>(nStyle));
    rGradient.SetStartColor(aStartColor);
    rGradient.SetEndColor(aEndColor);
    rGradient.SetAngle(Degree10(nAngle));
    rGradient.SetBorder(nBorder);
    rGradient.SetOfsX(nOffsetX);
    rGradient.SetOfsY(nOffsetY);
    rGradient.SetStartIntensity(nIntensityStart);
    rGradient.SetEndIntensity(nIntensityEnd);
    rGradient.SetSteps(nStepCount);
}

void TypeSerializer::writeGradient(const Gradient& rGradient)
{
    VersionCompat aCompat(mrStream, StreamMode::WRITE, 1);

    mrStream.WriteUInt16(static_cast<sal_uInt16>(rGradient.GetStyle()));
    writeColor(rGradient.GetStartColor());
    writeColor(rGradient.GetEndColor());
    mrStream.WriteUInt16(rGradient.GetAngle().get());
    mrStream.WriteUInt16(rGradient.GetBorder());
    mrStream.WriteUInt16(rGradient.GetOfsX());
    mrStream.WriteUInt16(rGradient.GetOfsY());
    mrStream.WriteUInt16(rGradient.GetStartIntensity());
    mrStream.WriteUInt16(rGradient.GetEndIntensity());
    mrStream.WriteUInt16(rGradient.GetSteps());
}

void TypeSerializer::readGfxLink(GfxLink& rGfxLink)
{
    sal_uInt16 nType = 0;
    sal_uInt32 nDataSize = 0;
    sal_uInt32 nUserId = 0;

    Size aSize;
    MapMode aMapMode;
    bool bMapAndSizeValid = false;

    {
        VersionCompat aCompat(mrStream, StreamMode::READ);

        // Version 1
        mrStream.ReadUInt16(nType);
        mrStream.ReadUInt32(nDataSize);
        mrStream.ReadUInt32(nUserId);

        if (aCompat.GetVersion() >= 2)
        {
            readSize(aSize);
            ReadMapMode(mrStream, aMapMode);
            bMapAndSizeValid = true;
        }
    }

    auto nRemainingData = mrStream.remainingSize();
    if (nDataSize > nRemainingData)
    {
        SAL_WARN("vcl", "graphic link stream is smaller than requested size");
        nDataSize = nRemainingData;
    }

    std::unique_ptr<sal_uInt8[]> pBuffer(new sal_uInt8[nDataSize]);
    mrStream.ReadBytes(pBuffer.get(), nDataSize);

    rGfxLink = GfxLink(std::move(pBuffer), nDataSize, static_cast<GfxLinkType>(nType));
    rGfxLink.SetUserId(nUserId);

    if (bMapAndSizeValid)
    {
        rGfxLink.SetPrefSize(aSize);
        rGfxLink.SetPrefMapMode(aMapMode);
    }
}

void TypeSerializer::writeGfxLink(const GfxLink& rGfxLink)
{
    {
        VersionCompat aCompat(mrStream, StreamMode::WRITE, 2);

        // Version 1
        mrStream.WriteUInt16(sal_uInt16(rGfxLink.GetType()));
        mrStream.WriteUInt32(rGfxLink.GetDataSize());
        mrStream.WriteUInt32(rGfxLink.GetUserId());

        // Version 2
        writeSize(rGfxLink.GetPrefSize());
        WriteMapMode(mrStream, rGfxLink.GetPrefMapMode());
    }

    if (rGfxLink.GetDataSize())
    {
        if (rGfxLink.GetData())
            mrStream.WriteBytes(rGfxLink.GetData(), rGfxLink.GetDataSize());
    }
}

namespace
{
#define NATIVE_FORMAT_50 COMPAT_FORMAT('N', 'A', 'T', '5')

constexpr sal_uInt32 constSvgMagic = createMagic('s', 'v', 'g', '0');
constexpr sal_uInt32 constWmfMagic = createMagic('w', 'm', 'f', '0');
constexpr sal_uInt32 constEmfMagic = createMagic('e', 'm', 'f', '0');
constexpr sal_uInt32 constPdfMagic = createMagic('p', 'd', 'f', '0');

} // end anonymous namespace

void TypeSerializer::readGraphic(Graphic& rGraphic)
{
    if (mrStream.GetError())
        return;

    const sal_uLong nInitialStreamPosition = mrStream.Tell();
    sal_uInt32 nType;

    // read Id
    mrStream.ReadUInt32(nType);

    // if there is no more data, avoid further expensive
    // reading which will create VDevs and other stuff, just to
    // read nothing. CAUTION: Eof is only true AFTER reading another
    // byte, a speciality of SvMemoryStream (!)
    if (!mrStream.good())
        return;

    if (NATIVE_FORMAT_50 == nType)
    {
        Graphic aGraphic;
        GfxLink aLink;

        // read compat info, destructor writes stuff into the header
        {
            VersionCompat aCompat(mrStream, StreamMode::READ);
        }

        readGfxLink(aLink);

        if (!mrStream.GetError() && aLink.LoadNative(aGraphic))
        {
            if (aLink.IsPrefMapModeValid())
                aGraphic.SetPrefMapMode(aLink.GetPrefMapMode());

            if (aLink.IsPrefSizeValid())
                aGraphic.SetPrefSize(aLink.GetPrefSize());
        }
        else
        {
            mrStream.Seek(nInitialStreamPosition);
            mrStream.SetError(ERRCODE_IO_WRONGFORMAT);
        }
        rGraphic = aGraphic;
    }
    else
    {
        BitmapEx aBitmapEx;
        const SvStreamEndian nOldFormat = mrStream.GetEndian();

        mrStream.SeekRel(-4);
        mrStream.SetEndian(SvStreamEndian::LITTLE);
        ReadDIBBitmapEx(aBitmapEx, mrStream);

        if (!mrStream.GetError())
        {
            sal_uInt32 nMagic1 = 0;
            sal_uInt32 nMagic2 = 0;
            sal_uInt64 nBeginPosition = mrStream.Tell();

            mrStream.ReadUInt32(nMagic1);
            mrStream.ReadUInt32(nMagic2);
            mrStream.Seek(nBeginPosition);

            if (!mrStream.GetError())
            {
                if (nMagic1 == 0x5344414e && nMagic2 == 0x494d4931)
                {
                    Animation aAnimation;
                    ReadAnimation(mrStream, aAnimation);

                    // #108077# manually set loaded BmpEx to Animation
                    // (which skips loading its BmpEx if already done)
                    aAnimation.SetBitmapEx(aBitmapEx);
                    rGraphic = Graphic(aAnimation);
                }
                else
                {
                    rGraphic = Graphic(aBitmapEx);
                }
            }
            else
            {
                mrStream.ResetError();
            }
        }
        else
        {
            GDIMetaFile aMetaFile;

            mrStream.Seek(nInitialStreamPosition);
            mrStream.ResetError();
            ReadGDIMetaFile(mrStream, aMetaFile);

            if (!mrStream.GetError())
            {
                rGraphic = Graphic(aMetaFile);
            }
            else
            {
                ErrCode nOriginalError = mrStream.GetErrorCode();
                // try to stream in Svg defining data (length, byte array and evtl. path)
                // See below (operator<<) for more information
                sal_uInt32 nMagic;
                mrStream.Seek(nInitialStreamPosition);
                mrStream.ResetError();
                mrStream.ReadUInt32(nMagic);

                if (constSvgMagic == nMagic || constWmfMagic == nMagic || constEmfMagic == nMagic
                    || constPdfMagic == nMagic)
                {
                    sal_uInt32 nLength = 0;
                    mrStream.ReadUInt32(nLength);

                    if (nLength)
                    {
                        VectorGraphicDataArray aData(nLength);

                        mrStream.ReadBytes(aData.getArray(), nLength);
                        OUString aPath = mrStream.ReadUniOrByteString(mrStream.GetStreamCharSet());

                        if (!mrStream.GetError())
                        {
                            VectorGraphicDataType aDataType(VectorGraphicDataType::Svg);

                            switch (nMagic)
                            {
                                case constWmfMagic:
                                    aDataType = VectorGraphicDataType::Wmf;
                                    break;
                                case constEmfMagic:
                                    aDataType = VectorGraphicDataType::Emf;
                                    break;
                                case constPdfMagic:
                                    aDataType = VectorGraphicDataType::Pdf;
                                    break;
                            }

                            auto aVectorGraphicDataPtr
                                = std::make_shared<VectorGraphicData>(aData, aPath, aDataType);
                            rGraphic = Graphic(aVectorGraphicDataPtr);
                        }
                    }
                }
                else
                {
                    mrStream.SetError(nOriginalError);
                }

                mrStream.Seek(nInitialStreamPosition);
            }
        }
        mrStream.SetEndian(nOldFormat);
    }
}

void TypeSerializer::writeGraphic(const Graphic& rGraphic)
{
    Graphic aGraphic(rGraphic);

    if (!aGraphic.makeAvailable())
        return;

    auto pGfxLink = aGraphic.GetSharedGfxLink();

    if (mrStream.GetVersion() >= SOFFICE_FILEFORMAT_50
        && (mrStream.GetCompressMode() & SvStreamCompressFlags::NATIVE) && pGfxLink
        && pGfxLink->IsNative())
    {
        // native format
        mrStream.WriteUInt32(NATIVE_FORMAT_50);

        // write compat info, destructor writes stuff into the header
        {
            VersionCompat aCompat(mrStream, StreamMode::WRITE, 1);
        }
        pGfxLink->SetPrefMapMode(aGraphic.GetPrefMapMode());
        pGfxLink->SetPrefSize(aGraphic.GetPrefSize());
        writeGfxLink(*pGfxLink);
    }
    else
    {
        // own format
        const SvStreamEndian nOldFormat = mrStream.GetEndian();
        mrStream.SetEndian(SvStreamEndian::LITTLE);

        switch (aGraphic.GetType())
        {
            case GraphicType::NONE:
            case GraphicType::Default:
                break;

            case GraphicType::Bitmap:
            {
                auto pVectorGraphicData = aGraphic.getVectorGraphicData();
                if (pVectorGraphicData)
                {
                    // stream out Vector Graphic defining data (length, byte array and evtl. path)
                    // this is used e.g. in swapping out graphic data and in transporting it over UNO API
                    // as sequence of bytes, but AFAIK not written anywhere to any kind of file, so it should be
                    // no problem to extend it; only used at runtime
                    switch (pVectorGraphicData->getVectorGraphicDataType())
                    {
                        case VectorGraphicDataType::Wmf:
                        {
                            mrStream.WriteUInt32(constWmfMagic);
                            break;
                        }
                        case VectorGraphicDataType::Emf:
                        {
                            mrStream.WriteUInt32(constEmfMagic);
                            break;
                        }
                        case VectorGraphicDataType::Svg:
                        {
                            mrStream.WriteUInt32(constSvgMagic);
                            break;
                        }
                        case VectorGraphicDataType::Pdf:
                        {
                            mrStream.WriteUInt32(constPdfMagic);
                            break;
                        }
                    }

                    sal_uInt32 nSize = pVectorGraphicData->getVectorGraphicDataArrayLength();
                    mrStream.WriteUInt32(nSize);
                    mrStream.WriteBytes(
                        pVectorGraphicData->getVectorGraphicDataArray().getConstArray(), nSize);
                    mrStream.WriteUniOrByteString(pVectorGraphicData->getPath(),
                                                  mrStream.GetStreamCharSet());
                }
                else if (aGraphic.IsAnimated())
                {
                    WriteAnimation(mrStream, aGraphic.GetAnimation());
                }
                else
                {
                    WriteDIBBitmapEx(aGraphic.GetBitmapEx(), mrStream);
                }
            }
            break;

            default:
            {
                if (aGraphic.IsSupportedGraphic())
                    WriteGDIMetaFile(mrStream, rGraphic.GetGDIMetaFile());
            }
            break;
        }
        mrStream.SetEndian(nOldFormat);
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */