summaryrefslogtreecommitdiff
path: root/sw/source/filter/html/htmlreqifreader.cxx
blob: 8a6101fa93ee36519487fda4d4d39bfc72e69484 (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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/* -*- 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/.
 */

#include "htmlreqifreader.hxx"

#include <comphelper/scopeguard.hxx>
#include <filter/msfilter/rtfutil.hxx>
#include <rtl/strbuf.hxx>
#include <sot/storage.hxx>
#include <svtools/parrtf.hxx>
#include <svtools/rtfkeywd.hxx>
#include <svtools/rtftoken.h>
#include <tools/stream.hxx>
#include <filter/msfilter/msdffimp.hxx>
#include <vcl/cvtgrf.hxx>
#include <ndole.hxx>
#include <sal/log.hxx>
#include <vcl/FilterConfigItem.hxx>
#include <vcl/wmf.hxx>
#include <comphelper/propertyvalue.hxx>
#include <fmtfsize.hxx>
#include <frmfmt.hxx>

using namespace com::sun::star;

namespace
{
/// RTF parser that just extracts a single OLE2 object from a file.
class ReqIfRtfReader : public SvRTFParser
{
public:
    ReqIfRtfReader(SvStream& rStream);
    void NextToken(int nToken) override;
    bool WriteObjectData(SvStream& rOLE);

private:
    bool m_bInObjData = false;
    OStringBuffer m_aHex;
};

ReqIfRtfReader::ReqIfRtfReader(SvStream& rStream)
    : SvRTFParser(rStream)
{
}

void ReqIfRtfReader::NextToken(int nToken)
{
    switch (nToken)
    {
        case '}':
            m_bInObjData = false;
            break;
        case RTF_TEXTTOKEN:
            if (m_bInObjData)
                m_aHex.append(OUStringToOString(aToken, RTL_TEXTENCODING_ASCII_US));
            break;
        case RTF_OBJDATA:
            m_bInObjData = true;
            break;
    }
}

bool ReqIfRtfReader::WriteObjectData(SvStream& rOLE)
{
    return msfilter::rtfutil::ExtractOLE2FromObjdata(m_aHex.makeStringAndClear(), rOLE);
}

/// Looks up what OLE1 calls the ClassName, see [MS-OLEDS] 2.3.8 CompObjStream.
OString ExtractOLEClassName(const tools::SvRef<SotStorage>& xStorage)
{
    OString aRet;

    SotStorageStream* pCompObj = xStorage->OpenSotStream("\1CompObj");
    if (!pCompObj)
        return aRet;

    pCompObj->Seek(0);
    pCompObj->SeekRel(28); // Header
    if (!pCompObj->good())
        return aRet;

    sal_uInt32 nData;
    pCompObj->ReadUInt32(nData); // AnsiUserType
    pCompObj->SeekRel(nData);
    if (!pCompObj->good())
        return aRet;

    pCompObj->ReadUInt32(nData); // AnsiClipboardFormat
    pCompObj->SeekRel(nData);
    if (!pCompObj->good())
        return aRet;

    pCompObj->ReadUInt32(nData); // Reserved1
    return read_uInt8s_ToOString(*pCompObj, nData - 1); // -1 because it is null-terminated
}

/// Parses the presentation stream of an OLE2 storage.
bool ParseOLE2Presentation(SvStream& rOle2, sal_uInt32& nWidth, sal_uInt32& nHeight,
                           SvStream& rPresentationData)
{
    // See [MS-OLEDS] 2.3.4, OLEPresentationStream
    rOle2.Seek(0);
    tools::SvRef<SotStorage> pStorage = new SotStorage(rOle2);
    tools::SvRef<SotStorageStream> xOle2Presentation
        = pStorage->OpenSotStream("\002OlePres000", StreamMode::STD_READ);

    // Read AnsiClipboardFormat.
    sal_uInt32 nMarkerOrLength = 0;
    xOle2Presentation->ReadUInt32(nMarkerOrLength);
    if (nMarkerOrLength != 0xffffffff)
        // FormatOrAnsiString is not present
        return false;
    sal_uInt32 nFormatOrAnsiLength = 0;
    xOle2Presentation->ReadUInt32(nFormatOrAnsiLength);
    if (nFormatOrAnsiLength != 0x00000003) // CF_METAFILEPICT
        return false;

    // Read TargetDeviceSize.
    sal_uInt32 nTargetDeviceSize = 0;
    xOle2Presentation->ReadUInt32(nTargetDeviceSize);
    if (nTargetDeviceSize != 0x00000004)
        // TargetDevice is present
        return false;

    sal_uInt32 nAspect = 0;
    xOle2Presentation->ReadUInt32(nAspect);
    sal_uInt32 nLindex = 0;
    xOle2Presentation->ReadUInt32(nLindex);
    sal_uInt32 nAdvf = 0;
    xOle2Presentation->ReadUInt32(nAdvf);
    sal_uInt32 nReserved1 = 0;
    xOle2Presentation->ReadUInt32(nReserved1);
    xOle2Presentation->ReadUInt32(nWidth);
    xOle2Presentation->ReadUInt32(nHeight);
    sal_uInt32 nSize = 0;
    xOle2Presentation->ReadUInt32(nSize);

    // Read Data.
    if (nSize > xOle2Presentation->remainingSize())
        return false;

    if (nSize <= 64)
    {
        SAL_WARN("sw.html",
                 "ParseOLE2Presentation: ignoring potentially broken small preview: size is "
                     << nSize);
        return false;
    }

    std::vector<char> aBuffer(nSize);
    xOle2Presentation->ReadBytes(aBuffer.data(), aBuffer.size());
    rPresentationData.WriteBytes(aBuffer.data(), aBuffer.size());

    return true;
}

/**
 * Inserts an OLE1 header before an OLE2 storage, assuming that the storage has an Ole10Native
 * stream.
 */
OString InsertOLE1HeaderFromOle10NativeStream(tools::SvRef<SotStorage>& xStorage,
                                              SwOLENode& rOLENode, SvStream& rOle1)
{
    tools::SvRef<SotStorageStream> xOle1Stream
        = xStorage->OpenSotStream("\1Ole10Native", StreamMode::STD_READ);
    sal_uInt32 nOle1Size = 0;
    xOle1Stream->ReadUInt32(nOle1Size);

    OString aClassName;
    if (xStorage->GetClassName() == SvGlobalName(0x0003000A, 0, 0, 0xc0, 0, 0, 0, 0, 0, 0, 0x46))
    {
        aClassName = "PBrush";
    }
    else
    {
        if (xStorage->GetClassName()
            != SvGlobalName(0x0003000C, 0, 0, 0xc0, 0, 0, 0, 0, 0, 0, 0x46))
        {
            SAL_WARN("sw.html", "InsertOLE1HeaderFromOle10NativeStream: unexpected class id: "
                                    << xStorage->GetClassName().GetHexName());
        }
        aClassName = "Package";
    }

    // Write ObjectHeader, see [MS-OLEDS] 2.2.4.
    rOle1.Seek(0);
    // OLEVersion.
    rOle1.WriteUInt32(0x00000501);

    // FormatID is EmbeddedObject.
    rOle1.WriteUInt32(0x00000002);

    // ClassName
    rOle1.WriteUInt32(aClassName.isEmpty() ? 0 : aClassName.getLength() + 1);
    if (!aClassName.isEmpty())
    {
        rOle1.WriteOString(aClassName);
        // Null terminated pascal string.
        rOle1.WriteChar(0);
    }

    // TopicName.
    rOle1.WriteUInt32(0);

    // ItemName.
    rOle1.WriteUInt32(0);

    // NativeDataSize
    rOle1.WriteUInt32(nOle1Size);

    // Write the actual native data.
    rOle1.WriteStream(*xOle1Stream, nOle1Size);

    // Write Presentation.
    if (!rOLENode.GetGraphic())
    {
        return aClassName;
    }

    const Graphic& rGraphic = *rOLENode.GetGraphic();
    Size aSize = rOLENode.GetTwipSize();
    SvMemoryStream aGraphicStream;
    if (GraphicConverter::Export(aGraphicStream, rGraphic, ConvertDataFormat::WMF) != ERRCODE_NONE)
    {
        return aClassName;
    }

    auto pGraphicAry = static_cast<const sal_uInt8*>(aGraphicStream.GetData());
    sal_uInt64 nPresentationData = aGraphicStream.TellEnd();
    msfilter::rtfutil::StripMetafileHeader(pGraphicAry, nPresentationData);

    // OLEVersion.
    rOle1.WriteUInt32(0x00000501);
    // FormatID: constant means the ClassName field is present.
    rOle1.WriteUInt32(0x00000005);
    // ClassName: null terminated pascal string.
    OString aPresentationClassName("METAFILEPICT");
    rOle1.WriteUInt32(aPresentationClassName.getLength() + 1);
    rOle1.WriteOString(aPresentationClassName);
    rOle1.WriteChar(0);
    // Width.
    rOle1.WriteUInt32(aSize.getWidth());
    // Height.
    rOle1.WriteUInt32(aSize.getHeight() * -1);
    // PresentationDataSize
    rOle1.WriteUInt32(8 + nPresentationData);
    // Reserved1-4.
    rOle1.WriteUInt16(0x0008);
    rOle1.WriteUInt16(0x31b1);
    rOle1.WriteUInt16(0x1dd9);
    rOle1.WriteUInt16(0x0000);
    rOle1.WriteBytes(pGraphicAry, nPresentationData);

    return aClassName;
}

/**
 * Writes an OLE1 header and data from rOle2 to rOle1.
 *
 * In case rOle2 has presentation data, then its size is written to nWidth/nHeight.  Otherwise
 * nWidth/nHeight/pPresentationData/nPresentationData is used for the presentation data.
 */
OString InsertOLE1Header(SvStream& rOle2, SvStream& rOle1, sal_uInt32& nWidth, sal_uInt32& nHeight,
                         SwOLENode& rOLENode, const sal_uInt8* pPresentationData,
                         sal_uInt64 nPresentationData)
{
    rOle2.Seek(0);
    tools::SvRef<SotStorage> xStorage(new SotStorage(rOle2));
    if (xStorage->GetError() != ERRCODE_NONE)
        return OString();

    if (xStorage->IsStream("\1Ole10Native"))
    {
        return InsertOLE1HeaderFromOle10NativeStream(xStorage, rOLENode, rOle1);
    }

    OString aClassName = ExtractOLEClassName(xStorage);

    // Write ObjectHeader, see [MS-OLEDS] 2.2.4.
    rOle1.Seek(0);
    // OLEVersion.
    rOle1.WriteUInt32(0x00000501);

    // FormatID is EmbeddedObject.
    rOle1.WriteUInt32(0x00000002);

    // ClassName
    rOle1.WriteUInt32(aClassName.isEmpty() ? 0 : aClassName.getLength() + 1);
    if (!aClassName.isEmpty())
    {
        rOle1.WriteOString(aClassName);
        // Null terminated pascal string.
        rOle1.WriteChar(0);
    }

    // TopicName.
    rOle1.WriteUInt32(0);

    // ItemName.
    rOle1.WriteUInt32(0);

    // NativeDataSize
    rOle1.WriteUInt32(rOle2.TellEnd());

    // Write the actual native data.
    rOle2.Seek(0);
    rOle1.WriteStream(rOle2);

    // Write Presentation.
    SvMemoryStream aPresentationData;
    // OLEVersion.
    rOle1.WriteUInt32(0x00000501);
    // FormatID: constant means the ClassName field is present.
    rOle1.WriteUInt32(0x00000005);
    // ClassName: null terminated pascal string.
    OString aPresentationClassName("METAFILEPICT");
    rOle1.WriteUInt32(aPresentationClassName.getLength() + 1);
    rOle1.WriteOString(aPresentationClassName);
    rOle1.WriteChar(0);
    const sal_uInt8* pBytes = nullptr;
    sal_uInt64 nBytes = 0;
    if (ParseOLE2Presentation(rOle2, nWidth, nHeight, aPresentationData))
    {
        // Take presentation data for OLE1 from OLE2.
        pBytes = static_cast<const sal_uInt8*>(aPresentationData.GetData());
        nBytes = aPresentationData.Tell();
    }
    else
    {
        // Take presentation data for OLE1 from RTF.
        pBytes = pPresentationData;
        nBytes = nPresentationData;
    }
    // Width.
    rOle1.WriteUInt32(nWidth);
    // Height.
    rOle1.WriteUInt32(nHeight * -1);
    // PresentationDataSize: size of (reserved fields + pBytes).
    rOle1.WriteUInt32(8 + nBytes);
    // Reserved1-4.
    rOle1.WriteUInt16(0x0008);
    rOle1.WriteUInt16(0x31b1);
    rOle1.WriteUInt16(0x1dd9);
    rOle1.WriteUInt16(0x0000);
    rOle1.WriteBytes(pBytes, nBytes);

    return aClassName;
}

/// Writes presentation data with the specified size to rRtf as an RTF hexdump.
void WrapOleGraphicInRtf(SvStream& rRtf, sal_uInt32 nWidth, sal_uInt32 nHeight,
                         const sal_uInt8* pPresentationData, sal_uInt64 nPresentationData)
{
    // Start result.
    rRtf.WriteCharPtr("{" OOO_STRING_SVTOOLS_RTF_RESULT);

    // Start pict.
    rRtf.WriteCharPtr("{" OOO_STRING_SVTOOLS_RTF_PICT);

    rRtf.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_WMETAFILE "8");
    rRtf.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_PICW);
    rRtf.WriteOString(OString::number(nWidth));
    rRtf.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_PICH);
    rRtf.WriteOString(OString::number(nHeight));
    rRtf.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_PICWGOAL);
    rRtf.WriteOString(OString::number(nWidth));
    rRtf.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_PICHGOAL);
    rRtf.WriteOString(OString::number(nHeight));
    if (pPresentationData)
    {
        rRtf.WriteCharPtr(SAL_NEWLINE_STRING);
        msfilter::rtfutil::WriteHex(pPresentationData, nPresentationData, &rRtf);
    }

    // End pict.
    rRtf.WriteCharPtr("}");

    // End result.
    rRtf.WriteCharPtr("}");
}
}

namespace SwReqIfReader
{
bool ExtractOleFromRtf(SvStream& rRtf, SvStream& rOle, bool& bOwnFormat)
{
    // Add missing header/footer.
    SvMemoryStream aRtf;
    aRtf.WriteOString("{\\rtf1");
    aRtf.WriteStream(rRtf);
    aRtf.WriteOString("}");
    aRtf.Seek(0);

    // Read the RTF markup.
    tools::SvRef<ReqIfRtfReader> xReader(new ReqIfRtfReader(aRtf));
    SvParserState eState = xReader->CallParser();
    if (eState == SvParserState::Error)
        return false;

    // Write the OLE2 data.
    if (!xReader->WriteObjectData(rOle))
        return false;

    tools::SvRef<SotStorage> pStorage = new SotStorage(rOle);
    OUString aFilterName = SvxMSDffManager::GetFilterNameFromClassID(pStorage->GetClassName());
    bOwnFormat = !aFilterName.isEmpty();
    if (!bOwnFormat)
    {
        // Real OLE2 data, we're done.
        rOle.Seek(0);
        return true;
    }

    // ODF-in-OLE2 case, extract actual data.
    SvMemoryStream aMemory;
    SvxMSDffManager::ExtractOwnStream(*pStorage, aMemory);
    rOle.Seek(0);
    aMemory.Seek(0);
    rOle.WriteStream(aMemory);
    // Stream length is current position + 1.
    rOle.SetStreamSize(aMemory.GetSize() + 1);
    rOle.Seek(0);
    return true;
}

bool WrapOleInRtf(SvStream& rOle2, SvStream& rRtf, SwOLENode& rOLENode,
                  const SwFrameFormat& rFormat)
{
    sal_uInt64 nPos = rOle2.Tell();
    comphelper::ScopeGuard g([&rOle2, nPos] { rOle2.Seek(nPos); });

    // Write OLE1 header, then the RTF wrapper.
    SvMemoryStream aOLE1;

    // Prepare presentation data early, so it's available to both OLE1 and RTF.
    Size aSize = rFormat.GetFrameSize().GetSize();
    sal_uInt32 nWidth = aSize.getWidth();
    sal_uInt32 nHeight = aSize.getHeight();
    const Graphic* pGraphic = rOLENode.GetGraphic();
    const sal_uInt8* pPresentationData = nullptr;
    sal_uInt64 nPresentationData = 0;
    SvMemoryStream aGraphicStream;
    if (pGraphic)
    {
        uno::Sequence<beans::PropertyValue> aFilterData
            = { comphelper::makePropertyValue("EmbedEMF", false) };
        FilterConfigItem aConfigItem(&aFilterData);
        if (ConvertGraphicToWMF(*pGraphic, aGraphicStream, &aConfigItem))
        {
            pPresentationData = static_cast<const sal_uInt8*>(aGraphicStream.GetData());
            nPresentationData = aGraphicStream.TellEnd();
            msfilter::rtfutil::StripMetafileHeader(pPresentationData, nPresentationData);
        }
    }
    OString aClassName = InsertOLE1Header(rOle2, aOLE1, nWidth, nHeight, rOLENode,
                                          pPresentationData, nPresentationData);

    // Start object.
    rRtf.WriteCharPtr("{" OOO_STRING_SVTOOLS_RTF_OBJECT);
    rRtf.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_OBJEMB);

    // Start objclass.
    rRtf.WriteCharPtr("{" OOO_STRING_SVTOOLS_RTF_IGNORE OOO_STRING_SVTOOLS_RTF_OBJCLASS " ");
    rRtf.WriteOString(aClassName);
    // End objclass.
    rRtf.WriteCharPtr("}");

    // Object size.
    rRtf.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_OBJW);
    rRtf.WriteOString(OString::number(nWidth));
    rRtf.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_OBJH);
    rRtf.WriteOString(OString::number(nHeight));

    // Start objdata.
    rRtf.WriteCharPtr(
        "{" OOO_STRING_SVTOOLS_RTF_IGNORE OOO_STRING_SVTOOLS_RTF_OBJDATA SAL_NEWLINE_STRING);
    msfilter::rtfutil::WriteHex(static_cast<const sal_uInt8*>(aOLE1.GetData()), aOLE1.GetSize(),
                                &rRtf);
    // End objdata.
    rRtf.WriteCharPtr("}");

    if (pPresentationData)
    {
        WrapOleGraphicInRtf(rRtf, nWidth, nHeight, pPresentationData, nPresentationData);
    }

    // End object.
    rRtf.WriteCharPtr("}");

    return true;
}

bool WrapGraphicInRtf(const Graphic& rGraphic, const SwFrameFormat& rFormat, SvStream& rRtf)
{
    // Start object.
    rRtf.WriteCharPtr("{" OOO_STRING_SVTOOLS_RTF_OBJECT);
    rRtf.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_OBJEMB);

    // Object size: as used in the document model (not pixel size)
    Size aSize = rFormat.GetFrameSize().GetSize();
    sal_uInt32 nWidth = aSize.getWidth();
    sal_uInt32 nHeight = aSize.getHeight();
    rRtf.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_OBJW);
    rRtf.WriteOString(OString::number(nWidth));
    rRtf.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_OBJH);
    rRtf.WriteOString(OString::number(nHeight));
    rRtf.WriteOString(SAL_NEWLINE_STRING);

    // Start objclass.
    rRtf.WriteCharPtr("{" OOO_STRING_SVTOOLS_RTF_IGNORE OOO_STRING_SVTOOLS_RTF_OBJCLASS " ");
    OString aClassName("PBrush");
    rRtf.WriteOString(aClassName);
    // End objclass.
    rRtf.WriteCharPtr("}");
    rRtf.WriteOString(SAL_NEWLINE_STRING);

    // Start objdata.
    rRtf.WriteCharPtr("{" OOO_STRING_SVTOOLS_RTF_IGNORE OOO_STRING_SVTOOLS_RTF_OBJDATA " ");

    SvMemoryStream aOle1;
    // Write ObjectHeader, see [MS-OLEDS] 2.2.4.
    // OLEVersion.
    aOle1.WriteUInt32(0x00000501);

    // FormatID is EmbeddedObject.
    aOle1.WriteUInt32(0x00000002);

    // ClassName
    aOle1.WriteUInt32(aClassName.getLength() + 1);
    aOle1.WriteOString(aClassName);
    // Null terminated pascal string.
    aOle1.WriteChar(0);

    // TopicName.
    aOle1.WriteUInt32(0);

    // ItemName.
    aOle1.WriteUInt32(0);

    // NativeDataSize
    SvMemoryStream aNativeData;
    if (GraphicConverter::Export(aNativeData, rGraphic, ConvertDataFormat::BMP) != ERRCODE_NONE)
    {
        SAL_WARN("sw.html", "WrapGraphicInRtf: bmp conversion failed");
    }
    aOle1.WriteUInt32(aNativeData.TellEnd());

    // Write the actual native data.
    aNativeData.Seek(0);
    aOle1.WriteStream(aNativeData);

    // Prepare presentation data.
    const sal_uInt8* pPresentationData = nullptr;
    sal_uInt64 nPresentationData = 0;
    SvMemoryStream aGraphicStream;
    uno::Sequence<beans::PropertyValue> aFilterData
        = { comphelper::makePropertyValue("EmbedEMF", false) };
    FilterConfigItem aConfigItem(&aFilterData);
    if (ConvertGraphicToWMF(rGraphic, aGraphicStream, &aConfigItem))
    {
        pPresentationData = static_cast<const sal_uInt8*>(aGraphicStream.GetData());
        nPresentationData = aGraphicStream.TellEnd();
        msfilter::rtfutil::StripMetafileHeader(pPresentationData, nPresentationData);
    }

    // Write Presentation.
    // OLEVersion.
    aOle1.WriteUInt32(0x00000501);
    // FormatID: constant means the ClassName field is present.
    aOle1.WriteUInt32(0x00000005);
    // ClassName: null terminated pascal string.
    OString aPresentationClassName("METAFILEPICT");
    aOle1.WriteUInt32(aPresentationClassName.getLength() + 1);
    aOle1.WriteOString(aPresentationClassName);
    aOle1.WriteChar(0);
    const sal_uInt8* pBytes = nullptr;
    sal_uInt64 nBytes = 0;
    // Take presentation data for OLE1 from RTF.
    pBytes = pPresentationData;
    nBytes = nPresentationData;
    // Width.
    aOle1.WriteUInt32(nWidth);
    // Height.
    aOle1.WriteUInt32(nHeight * -1);
    // PresentationDataSize: size of (reserved fields + pBytes).
    aOle1.WriteUInt32(8 + nBytes);
    // Reserved1-4.
    aOle1.WriteUInt16(0x0008);
    aOle1.WriteUInt16(0x31b1);
    aOle1.WriteUInt16(0x1dd9);
    aOle1.WriteUInt16(0x0000);
    aOle1.WriteBytes(pBytes, nBytes);

    // End objdata.
    msfilter::rtfutil::WriteHex(static_cast<const sal_uInt8*>(aOle1.GetData()), aOle1.GetSize(),
                                &rRtf);
    rRtf.WriteCharPtr("}");
    rRtf.WriteOString(SAL_NEWLINE_STRING);

    rRtf.WriteCharPtr("{" OOO_STRING_SVTOOLS_RTF_RESULT);
    rRtf.WriteCharPtr("{" OOO_STRING_SVTOOLS_RTF_PICT);

    Size aMapped(rGraphic.GetPrefSize());
    rRtf.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_PICW);
    rRtf.WriteOString(OString::number(aMapped.Width()));
    rRtf.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_PICH);
    rRtf.WriteOString(OString::number(aMapped.Height()));

    rRtf.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_PICWGOAL);
    rRtf.WriteOString(OString::number(nWidth));
    rRtf.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_PICHGOAL);
    rRtf.WriteOString(OString::number(nHeight));
    rRtf.WriteCharPtr(OOO_STRING_SVTOOLS_RTF_WMETAFILE "8");
    rRtf.WriteOString(SAL_NEWLINE_STRING);

    if (pPresentationData)
    {
        msfilter::rtfutil::WriteHex(pPresentationData, nPresentationData, &rRtf);
        rRtf.WriteOString(SAL_NEWLINE_STRING);
    }

    // End pict.
    rRtf.WriteCharPtr("}");

    // End result.
    rRtf.WriteCharPtr("}");

    // End object.
    rRtf.WriteCharPtr("}");
    return true;
}
}

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