summaryrefslogtreecommitdiff
path: root/sw/source/filter/ww8/rtfsdrexport.cxx
blob: 9e45602cf380507f746f2955df15a5e0bc887221 (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
641
642
643
644
645
646
/* -*- 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 "rtfsdrexport.hxx"
#include "rtfattributeoutput.hxx"
#include <svtools/rtfkeywd.hxx>
#include <filter/msfilter/rtfutil.hxx>
#include <editeng/editobj.hxx>
#include <svx/svdotext.hxx>
#include <svx/unoapi.hxx>
#include <vcl/cvtgrf.hxx>
#include <textboxhelper.hxx>
#include <dcontact.hxx>
#include <algorithm>

using namespace css;

RtfSdrExport::RtfSdrExport(RtfExport& rExport)
    : EscherEx(EscherExGlobalRef(new EscherExGlobal), nullptr),
      m_rExport(rExport),
      m_rAttrOutput(static_cast<RtfAttributeOutput&>(m_rExport.AttrOutput())),
      m_pSdrObject(nullptr),
      m_nShapeType(ESCHER_ShpInst_Nil),
      m_nShapeFlags(0) ,
      m_aShapeStyle(200),
      m_pShapeTypeWritten(new bool[ ESCHER_ShpInst_COUNT ]),
      m_aTextBoxes(SwTextBoxHelper::findTextBoxes(m_rExport.m_pDoc))
{
    mnGroupLevel = 1;
    memset(m_pShapeTypeWritten, 0, ESCHER_ShpInst_COUNT * sizeof(bool));
}

RtfSdrExport::~RtfSdrExport()
{
    delete mpOutStrm;
    mpOutStrm = nullptr;
    delete[] m_pShapeTypeWritten;
    m_pShapeTypeWritten = nullptr;
}

void RtfSdrExport::OpenContainer(sal_uInt16 nEscherContainer, int nRecInstance)
{
    EscherEx::OpenContainer(nEscherContainer, nRecInstance);

    if (nEscherContainer == ESCHER_SpContainer)
    {
        m_nShapeType = ESCHER_ShpInst_Nil;
        if (!m_aShapeStyle.isEmpty())
            m_aShapeStyle.makeStringAndClear();
        m_aShapeStyle.ensureCapacity(200);
        m_aShapeProps.clear();
    }
}

void RtfSdrExport::CloseContainer()
{
    if (mRecTypes.back() == ESCHER_SpContainer)
    {
        // write the shape now when we have all the info
        sal_Int32 nShapeElement = StartShape();
        EndShape(nShapeElement);

        // cleanup
        m_nShapeType = ESCHER_ShpInst_Nil;
    }

    EscherEx::CloseContainer();
}

sal_uInt32 RtfSdrExport::EnterGroup(const OUString& /*rShapeName*/, const Rectangle* /*pRect*/)
{
    return GenerateShapeId();
}

void RtfSdrExport::LeaveGroup()
{
    /* noop */
}

void RtfSdrExport::AddShape(sal_uInt32 nShapeType, sal_uInt32 nShapeFlags, sal_uInt32 /*nShapeId*/)
{
    m_nShapeType = nShapeType;
    m_nShapeFlags = nShapeFlags;
}

inline sal_uInt16 impl_GetUInt16(const sal_uInt8*& pVal)
{
    sal_uInt16 nRet = *pVal++;
    nRet += (*pVal++) << 8;
    return nRet;
}

inline sal_Int32 impl_GetPointComponent(const sal_uInt8*& pVal, sal_Size& rVerticesPos, sal_uInt16 nPointSize)
{
    sal_Int32 nRet = 0;
    if ((nPointSize == 0xfff0) || (nPointSize == 4))
    {
        sal_uInt16 nUnsigned = *pVal++;
        nUnsigned += (*pVal++) << 8;
        rVerticesPos += 2;

        nRet = sal_Int16(nUnsigned);
    }
    else if (nPointSize == 8)
    {
        sal_uInt32 nUnsigned = *pVal++;
        nUnsigned += (*pVal++) << 8;
        nUnsigned += (*pVal++) << 16;
        nUnsigned += (*pVal++) << 24;
        rVerticesPos += 4;

        nRet = nUnsigned;
    }

    return nRet;
}

void RtfSdrExport::Commit(EscherPropertyContainer& rProps, const Rectangle& rRect)
{
    if (m_nShapeType == ESCHER_ShpInst_Nil)
        return;

    if (m_nShapeType == ESCHER_ShpInst_Line)
        AddLineDimensions(rRect);
    else
        AddRectangleDimensions(m_aShapeStyle, rRect);

    // properties
    const EscherProperties& rOpts = rProps.GetOpts();
    for (EscherProperties::const_iterator it = rOpts.begin(); it != rOpts.end(); ++it)
    {
        sal_uInt16 nId = (it->nPropId & 0x0FFF);

        switch (nId)
        {
        case ESCHER_Prop_WrapText:
        {
            int nWrapType = 0;
            switch (it->nPropValue)
            {
            case ESCHER_WrapSquare:
                nWrapType = 2;
                break;
            case ESCHER_WrapByPoints:
                nWrapType = 4;
                break;
            case ESCHER_WrapNone:
                nWrapType = 3;
                break;
            case ESCHER_WrapTopBottom:
                nWrapType = 1;
                break;
            case ESCHER_WrapThrough:
                nWrapType = 5;
                break;
            }
            if (nWrapType)
                m_aShapeStyle.append(OOO_STRING_SVTOOLS_RTF_SHPWR).append((sal_Int32)nWrapType);
        }
        break;
        case ESCHER_Prop_fillColor:
            m_aShapeProps.insert(std::pair<OString,OString>("fillColor", OString::number(it->nPropValue)));
            break;
        case ESCHER_Prop_fillBackColor:
            m_aShapeProps.insert(std::pair<OString,OString>("fillBackColor", OString::number(it->nPropValue)));
            break;
        case ESCHER_Prop_AnchorText:
            m_aShapeProps.insert(std::pair<OString,OString>("anchorText", OString::number(it->nPropValue)));
            break;
        case ESCHER_Prop_fNoFillHitTest:
            if (it->nPropValue)
                m_aShapeProps.insert(std::pair<OString,OString>("fNoFillHitTest", OString::number(1)));
            break;
        case ESCHER_Prop_fNoLineDrawDash:
            // for some reason the value is set to 0x90000 if lines are switched off
            if (it->nPropValue == 0x90000)
                m_aShapeProps.insert(std::pair<OString,OString>("fLine", OString::number(0)));
            break;
        case ESCHER_Prop_lineColor:
            m_aShapeProps.insert(std::pair<OString,OString>("lineColor", OString::number(it->nPropValue)));
            break;
        case ESCHER_Prop_lineBackColor:
            m_aShapeProps.insert(std::pair<OString,OString>("lineBackColor", OString::number(it->nPropValue)));
            break;
        case ESCHER_Prop_lineJoinStyle:
            m_aShapeProps.insert(std::pair<OString,OString>("lineJoinStyle", OString::number(it->nPropValue)));
            break;
        case ESCHER_Prop_fshadowObscured:
            if (it->nPropValue)
                m_aShapeProps.insert(std::pair<OString,OString>("fshadowObscured", "1"));
            break;
        case ESCHER_Prop_geoLeft:
        case ESCHER_Prop_geoTop:
        {
            sal_uInt32 nLeft = 0, nTop = 0;

            if (nId == ESCHER_Prop_geoLeft)
            {
                nLeft = it->nPropValue;
                rProps.GetOpt(ESCHER_Prop_geoTop, nTop);
            }
            else
            {
                nTop = it->nPropValue;
                rProps.GetOpt(ESCHER_Prop_geoLeft, nLeft);
            }

            m_aShapeProps.insert(std::pair<OString,OString>("geoLeft",
                                 OString::number(sal_Int32(nLeft))));
            m_aShapeProps.insert(std::pair<OString,OString>("geoTop",
                                 OString::number(sal_Int32(nTop))));
        }
        break;

        case ESCHER_Prop_geoRight:
        case ESCHER_Prop_geoBottom:
        {
            sal_uInt32 nLeft = 0, nRight = 0, nTop = 0, nBottom = 0;
            rProps.GetOpt(ESCHER_Prop_geoLeft, nLeft);
            rProps.GetOpt(ESCHER_Prop_geoTop, nTop);

            if (nId == ESCHER_Prop_geoRight)
            {
                nRight = it->nPropValue;
                rProps.GetOpt(ESCHER_Prop_geoBottom, nBottom);
            }
            else
            {
                nBottom = it->nPropValue;
                rProps.GetOpt(ESCHER_Prop_geoRight, nRight);
            }

            m_aShapeProps.insert(std::pair<OString,OString>("geoRight",
                                 OString::number(sal_Int32(nRight) - sal_Int32(nLeft))));
            m_aShapeProps.insert(std::pair<OString,OString>("geoBottom",
                                 OString::number(sal_Int32(nBottom) - sal_Int32(nTop))));
        }
        break;
        case ESCHER_Prop_pVertices:
        case ESCHER_Prop_pSegmentInfo:
        {
            EscherPropSortStruct aVertices;
            EscherPropSortStruct aSegments;

            if (rProps.GetOpt(ESCHER_Prop_pVertices, aVertices) &&
                    rProps.GetOpt(ESCHER_Prop_pSegmentInfo, aSegments) &&
                    aVertices.nPropSize >= 6 && aSegments.nPropSize >= 6)
            {
                const sal_uInt8* pVerticesIt = aVertices.pBuf + 6;
                sal_Size nVerticesPos = 6;
                const sal_uInt8* pSegmentIt = aSegments.pBuf;

                OStringBuffer aSegmentInfo(512);
                OStringBuffer aVerticies(512);

                sal_uInt16 nPointSize = aVertices.pBuf[4] + (aVertices.pBuf[5] << 8);

                // number of segments
                sal_uInt16 nSegments = impl_GetUInt16(pSegmentIt);
                sal_Int32 nVertices = 0;
                aSegmentInfo.append("2;").append((sal_Int32)nSegments);
                pSegmentIt += 4;

                for (; nSegments; --nSegments)
                {
                    sal_uInt16 nSeg = impl_GetUInt16(pSegmentIt);

                    // The segment type is stored in the upper 3 bits
                    // and segment count is stored in the lower 13
                    // bits.
                    unsigned char nSegmentType = (nSeg & 0xE000) >> 13;
                    unsigned short nSegmentCount = nSeg & 0x03FF;

                    aSegmentInfo.append(';').append((sal_Int32)nSeg);
                    switch (nSegmentType)
                    {
                    case msopathLineTo:
                        for (unsigned short i = 0; i < nSegmentCount; ++i)
                        {
                            sal_Int32 nX = impl_GetPointComponent(pVerticesIt, nVerticesPos, nPointSize);
                            sal_Int32 nY = impl_GetPointComponent(pVerticesIt, nVerticesPos, nPointSize);
                            aVerticies.append(";(").append(nX).append(",").append(nY).append(")");
                            nVertices ++;
                        }
                        break;
                    case msopathMoveTo:
                    {
                        sal_Int32 nX = impl_GetPointComponent(pVerticesIt, nVerticesPos, nPointSize);
                        sal_Int32 nY = impl_GetPointComponent(pVerticesIt, nVerticesPos, nPointSize);
                        aVerticies.append(";(").append(nX).append(",").append(nY).append(")");
                        nVertices++;
                        break;
                    }
                    case msopathCurveTo:
                        for (unsigned short j = 0; j < nSegmentCount; ++j)
                        {
                            for (int i = 0; i < 3; i++)
                            {
                                sal_Int32 nX = impl_GetPointComponent(pVerticesIt, nVerticesPos, nPointSize);
                                sal_Int32 nY = impl_GetPointComponent(pVerticesIt, nVerticesPos, nPointSize);
                                aVerticies.append(";(").append(nX).append(",").append(nY).append(")");
                                nVertices ++;
                            }
                        }
                        break;
                    case msopathEscape:
                    {
                        // If the segment type is msopathEscape, the lower 13 bits are
                        // divided in a 5 bit escape code and 8 bit
                        // vertex count (not segment count!)
                        unsigned char nVertexCount = nSegmentCount & 0x00FF;
                        nVerticesPos += nVertexCount;
                        break;
                    }
                    case msopathClientEscape:
                    case msopathClose:
                    case msopathEnd:
                        break;
                    default:
                        SAL_WARN("oox", "Totally b0rked\n");
                        break;
                    }
                }

                if (!aVerticies.isEmpty())
                {
                    // We know the number of vertices at the end only, so we have to prepend them here.
                    m_aShapeProps.insert(std::pair<OString,OString>("pVerticies", "8;" + OString::number(nVertices) + aVerticies.makeStringAndClear()));
                }
                if (!aSegmentInfo.isEmpty())
                    m_aShapeProps.insert(std::pair<OString,OString>("pSegmentInfo", aSegmentInfo.makeStringAndClear()));
            }
            else
                SAL_INFO("sw.rtf", OSL_THIS_FUNC << ": unhandled shape path, missing either pVertices or pSegmentInfo");
        }
        break;
        case ESCHER_Prop_shapePath:
            // noop, we use pSegmentInfo instead
            break;
        case ESCHER_Prop_fFillOK:
            if (!it->nPropValue)
                m_aShapeProps.insert(std::pair<OString,OString>("fFillOK", "0"));
            break;
        case ESCHER_Prop_dxTextLeft:
            m_aShapeProps.insert(std::pair<OString,OString>("dxTextLeft", OString::number(it->nPropValue)));
            break;
        case ESCHER_Prop_dyTextTop:
            m_aShapeProps.insert(std::pair<OString,OString>("dyTextTop", OString::number(it->nPropValue)));
            break;
        case ESCHER_Prop_dxTextRight:
            m_aShapeProps.insert(std::pair<OString,OString>("dxTextRight", OString::number(it->nPropValue)));
            break;
        case ESCHER_Prop_dyTextBottom:
            m_aShapeProps.insert(std::pair<OString,OString>("dyTextBottom", OString::number(it->nPropValue)));
            break;
        case ESCHER_Prop_FitTextToShape:
            // Size text to fit shape size: not supported by RTF
            break;
        case ESCHER_Prop_adjustValue:
            m_aShapeProps.insert(std::pair<OString,OString>("adjustValue", OString::number(it->nPropValue)));
            break;
        case ESCHER_Prop_txflTextFlow:
            m_aShapeProps.insert(std::pair<OString,OString>("txflTextFlow", OString::number(it->nPropValue)));
            break;
        case ESCHER_Prop_fillType:
            m_aShapeProps.insert(std::pair<OString,OString>("fillType", OString::number(it->nPropValue)));
            break;
        case ESCHER_Prop_fillOpacity:
            m_aShapeProps.insert(std::pair<OString,OString>("fillOpacity", OString::number(it->nPropValue)));
            break;
        case ESCHER_Prop_fillBlip:
        {
            OStringBuffer aBuf;
            aBuf.append('{').append(OOO_STRING_SVTOOLS_RTF_PICT).append(OOO_STRING_SVTOOLS_RTF_PNGBLIP).append(SAL_NEWLINE_STRING);
            int nHeaderSize = 25; // The first bytes are WW8-specific, we're only interested in the PNG
            aBuf.append(RtfAttributeOutput::WriteHex(it->pBuf + nHeaderSize, it->nPropSize - nHeaderSize));
            aBuf.append('}');
            m_aShapeProps.insert(std::pair<OString,OString>("fillBlip", aBuf.makeStringAndClear()));
        }
        break;
        default:
            SAL_INFO("sw.rtf", OSL_THIS_FUNC << ": unhandled property: " << nId << " (value: " << it->nPropValue << ")");
            break;
        }
    }
}

void RtfSdrExport::AddLineDimensions(const Rectangle& rRectangle)
{
    // We get the position relative to (the current?) character
    m_aShapeProps.insert(std::pair<OString,OString>("posrelh", "3"));

    switch (m_nShapeFlags & 0xC0)
    {
    case 0x40:
        m_aShapeProps.insert(std::pair<OString,OString>("fFlipV", "1"));
        break;
    case 0x80:
        m_aShapeProps.insert(std::pair<OString,OString>("fFlipH", "1"));
        break;
    case 0xC0:
        m_aShapeProps.insert(std::pair<OString,OString>("fFlipV", "1"));
        m_aShapeProps.insert(std::pair<OString,OString>("fFlipH", "1"));
        break;
    }

    // the actual dimensions
    m_aShapeStyle.append(OOO_STRING_SVTOOLS_RTF_SHPLEFT).append(rRectangle.Left());
    m_aShapeStyle.append(OOO_STRING_SVTOOLS_RTF_SHPTOP).append(rRectangle.Top());
    m_aShapeStyle.append(OOO_STRING_SVTOOLS_RTF_SHPRIGHT).append(rRectangle.Right());
    m_aShapeStyle.append(OOO_STRING_SVTOOLS_RTF_SHPBOTTOM).append(rRectangle.Bottom());
}

void RtfSdrExport::AddRectangleDimensions(OStringBuffer& rBuffer, const Rectangle& rRectangle)
{
    // We get the position relative to (the current?) character
    m_aShapeProps.insert(std::pair<OString,OString>("posrelh", "3"));

    rBuffer.append(OOO_STRING_SVTOOLS_RTF_SHPLEFT).append(rRectangle.Left());
    rBuffer.append(OOO_STRING_SVTOOLS_RTF_SHPTOP).append(rRectangle.Top());
    rBuffer.append(OOO_STRING_SVTOOLS_RTF_SHPRIGHT).append(rRectangle.Right());
    rBuffer.append(OOO_STRING_SVTOOLS_RTF_SHPBOTTOM).append(rRectangle.Bottom());
}

extern const char* pShapeTypes[];

static void lcl_AppendSP(OStringBuffer& rRunText, const char cName[], const OString& rValue)
{
    rRunText.append('{').append(OOO_STRING_SVTOOLS_RTF_SP)
    .append('{').append(OOO_STRING_SVTOOLS_RTF_SN " ").append(cName).append('}')
    .append('{').append(OOO_STRING_SVTOOLS_RTF_SV " ").append(rValue).append('}')
    .append('}');
}

void RtfSdrExport::impl_writeGraphic()
{
    // Get the Graphic object from the Sdr one.
    uno::Reference<drawing::XShape> xShape = GetXShapeForSdrObject(const_cast<SdrObject*>(m_pSdrObject));
    uno::Reference<beans::XPropertySet> xPropertySet(xShape, uno::UNO_QUERY);
    OUString sGraphicURL;
    try
    {
        xPropertySet->getPropertyValue("GraphicURL") >>= sGraphicURL;
    }
    catch (beans::UnknownPropertyException& rException)
    {
        // ATM groupshapes are not supported, just make sure we don't crash on them.
        SAL_WARN("sw.rtf", "failed. Message: " << rException.Message);
        return;
    }
    OString aURLBS(OUStringToOString(sGraphicURL, RTL_TEXTENCODING_UTF8));
    Graphic aGraphic = GraphicObject(aURLBS.copy(RTL_CONSTASCII_LENGTH("vnd.sun.star.GraphicObject:"))).GetTransformedGraphic();

    // Export it to a stream.
    SvMemoryStream aStream;
    GraphicConverter::Export(aStream, aGraphic, ConvertDataFormat::PNG);
    aStream.Seek(STREAM_SEEK_TO_END);
    sal_uInt32 nSize = aStream.Tell();
    const sal_uInt8* pGraphicAry = static_cast<sal_uInt8 const*>(aStream.GetData());

    Size aMapped(aGraphic.GetPrefSize());

    // Add it to the properties.
    RtfStringBuffer aBuf;
    aBuf->append('{').append(OOO_STRING_SVTOOLS_RTF_PICT).append(OOO_STRING_SVTOOLS_RTF_PNGBLIP);
    aBuf->append(OOO_STRING_SVTOOLS_RTF_PICW).append(sal_Int32(aMapped.Width()));
    aBuf->append(OOO_STRING_SVTOOLS_RTF_PICH).append(sal_Int32(aMapped.Height())).append(SAL_NEWLINE_STRING);
    aBuf->append(RtfAttributeOutput::WriteHex(pGraphicAry, nSize));
    aBuf->append('}');
    m_aShapeProps.insert(std::pair<OString,OString>("pib", aBuf.makeStringAndClear()));
}

sal_Int32 RtfSdrExport::StartShape()
{
    if (m_nShapeType == ESCHER_ShpInst_Nil)
        return -1;

    m_aShapeProps.insert(std::pair<OString,OString>("shapeType", OString::number(m_nShapeType)));
    if (ESCHER_ShpInst_PictureFrame == m_nShapeType)
        impl_writeGraphic();

    m_rAttrOutput.RunText().append('{').append(OOO_STRING_SVTOOLS_RTF_SHP);
    m_rAttrOutput.RunText().append('{').append(OOO_STRING_SVTOOLS_RTF_IGNORE).append(OOO_STRING_SVTOOLS_RTF_SHPINST);

    m_rAttrOutput.RunText().append(m_aShapeStyle.makeStringAndClear());
    // Ignore \shpbxpage, \shpbxmargin, and \shpbxcolumn, in favor of the posrelh property.
    m_rAttrOutput.RunText().append(OOO_STRING_SVTOOLS_RTF_SHPBXIGNORE);
    // Ignore \shpbypage, \shpbymargin, and \shpbycolumn, in favor of the posrelh property.
    m_rAttrOutput.RunText().append(OOO_STRING_SVTOOLS_RTF_SHPBYIGNORE);

    for (std::map<OString,OString>::reverse_iterator i = m_aShapeProps.rbegin(); i != m_aShapeProps.rend(); ++i)
        lcl_AppendSP(m_rAttrOutput.RunText(), (*i).first.getStr(), (*i).second);

    lcl_AppendSP(m_rAttrOutput.RunText(), "wzDescription", msfilter::rtfutil::OutString(m_pSdrObject->GetDescription(), m_rExport.eCurrentEncoding));
    lcl_AppendSP(m_rAttrOutput.RunText(), "wzName", msfilter::rtfutil::OutString(m_pSdrObject->GetTitle(), m_rExport.eCurrentEncoding));

    // now check if we have some text
    const SwFrameFormat* pShape = FindFrameFormat(m_pSdrObject);
    if (pShape)
    {
        if (SwFrameFormat* pTextBox = SwTextBoxHelper::findTextBox(pShape))
        {
            ww8::Frame* pFrame = nullptr;
            for (ww8::FrameIter it = m_rExport.m_aFrames.begin(); it != m_rExport.m_aFrames.end(); ++it)
            {
                if (pTextBox == &it->GetFrameFormat())
                {
                    pFrame = &(*it);
                    break;
                }
            }

            if (pFrame)
                m_rAttrOutput.writeTextFrame(*pFrame, /*bTextBox=*/true);
            return m_nShapeType;
        }
    }

    const SdrTextObj* pTextObj = dynamic_cast<const SdrTextObj*>(m_pSdrObject);
    if (pTextObj)
    {
        const OutlinerParaObject* pParaObj = nullptr;
        std::unique_ptr<const OutlinerParaObject> pOwnedParaObj;

        /*
        #i13885#
        When the object is actively being edited, that text is not set into
        the objects normal text object, but lives in a separate object.
        */
        if (pTextObj->IsTextEditActive())
        {
            pOwnedParaObj.reset(pTextObj->GetEditOutlinerParaObject());
            pParaObj = pOwnedParaObj.get();
        }
        else
        {
            pParaObj = pTextObj->GetOutlinerParaObject();
        }

        if (pParaObj)
        {
            // this is reached only in case some text is attached to the shape
            WriteOutliner(*pParaObj, TXT_HFTXTBOX);
        }
    }

    return m_nShapeType;
}

void RtfSdrExport::WriteOutliner(const OutlinerParaObject& rParaObj, TextTypes eType)
{
    SAL_INFO("sw.rtf", OSL_THIS_FUNC << " start");

    const EditTextObject& rEditObj = rParaObj.GetTextObject();
    MSWord_SdrAttrIter aAttrIter(m_rExport, rEditObj, eType);

    sal_Int32 nPara = rEditObj.GetParagraphCount();

    bool bShape = eType == TXT_HFTXTBOX;
    if (bShape)
        m_rAttrOutput.RunText().append('{').append(OOO_STRING_SVTOOLS_RTF_SHPTXT).append(' ');
    for (sal_Int32 n = 0; n < nPara; ++n)
    {
        if (n)
            aAttrIter.NextPara(n);

        rtl_TextEncoding eChrSet = aAttrIter.GetNodeCharSet();

        OUString aStr(rEditObj.GetText(n));
        sal_Int32 nAktPos = 0;
        const sal_Int32 nEnd = aStr.getLength();

        aAttrIter.OutParaAttr(false);
        m_rAttrOutput.RunText().append(m_rAttrOutput.Styles().makeStringAndClear());
        m_rAttrOutput.RunText().append(m_rAttrOutput.StylesEnd().makeStringAndClear());

        do
        {
            const sal_Int32 nNextAttr = std::min(aAttrIter.WhereNext(), nEnd);
            rtl_TextEncoding eNextChrSet = aAttrIter.GetNextCharSet();

            aAttrIter.OutAttr(nAktPos);
            m_rAttrOutput.RunText().append('{');
            m_rAttrOutput.RunText().append(m_rAttrOutput.Styles().makeStringAndClear());
            m_rAttrOutput.RunText().append(m_rAttrOutput.StylesEnd().makeStringAndClear());
            m_rAttrOutput.RunText().append(SAL_NEWLINE_STRING);
            bool bTextAtr = aAttrIter.IsTextAttr(nAktPos);
            if (!bTextAtr)
            {
                OUString aOut(aStr.copy(nAktPos, nNextAttr - nAktPos));
                m_rAttrOutput.RunText().append(msfilter::rtfutil::OutString(aOut, eChrSet));
            }

            m_rAttrOutput.RunText().append('}');

            nAktPos = nNextAttr;
            eChrSet = eNextChrSet;
            aAttrIter.NextPos();
        }
        while (nAktPos < nEnd);
        if (bShape || n + 1 < nPara)
            m_rAttrOutput.RunText().append(OOO_STRING_SVTOOLS_RTF_PAR);
    }
    if (bShape)
        m_rAttrOutput.RunText().append('}');

    SAL_INFO("sw.rtf", OSL_THIS_FUNC << " end");
}

void RtfSdrExport::EndShape(sal_Int32 nShapeElement)
{
    if (nShapeElement >= 0)
    {
        // end of the shape
        m_rAttrOutput.RunText().append('}').append('}');
    }
}

sal_uInt32 RtfSdrExport::AddSdrObject(const SdrObject& rObj)
{
    m_pSdrObject = &rObj;
    return EscherEx::AddSdrObject(rObj);
}

bool RtfSdrExport::isTextBox(const SwFrameFormat& rFrameFormat)
{
    return m_aTextBoxes.find(&rFrameFormat) != m_aTextBoxes.end();
}

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