summaryrefslogtreecommitdiff
path: root/svx/source/table/tablertfimporter.cxx
blob: fb3af5dd82ed936f3e068078fd7eadb529564440 (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
/* -*- 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 <memory>
#include <vector>

#include <com/sun/star/table/XTable.hpp>
#include <com/sun/star/table/XMergeableCellRange.hpp>

#include <tools/stream.hxx>
#include <svtools/rtftoken.h>

#include <svx/svdetc.hxx>
#include <editeng/outlobj.hxx>

#include <cell.hxx>
#include <svx/svdotable.hxx>
#include <svx/svdoutl.hxx>
#include <editeng/editeng.hxx>
#include <editeng/editdata.hxx>
#include <svx/svdmodel.hxx>
#include <editeng/svxrtf.hxx>
#include <sal/log.hxx>
#include <tools/debug.hxx>

using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::table;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::beans;

namespace sdr::table {

namespace {

struct RTFCellDefault
{
    SfxItemSet          maItemSet;
    sal_Int32           mnRowSpan;
    sal_Int32           mnColSpan;   // MergeCell if >1, merged cells if 0
    sal_Int32           mnCellX;

    explicit RTFCellDefault( SfxItemPool* pPool ) : maItemSet( *pPool ), mnRowSpan(1), mnColSpan(1), mnCellX(0) {}
};

}

typedef std::vector< std::shared_ptr< RTFCellDefault > > RTFCellDefaultVector;

namespace {

struct RTFCellInfo
{
    SfxItemSet          maItemSet;
    sal_Int32           mnStartPara;
    sal_Int32           mnParaCount;
    sal_Int32           mnCellX;
    sal_Int32           mnRowSpan;
    std::shared_ptr< RTFCellInfo > mxVMergeCell;

    explicit RTFCellInfo( SfxItemPool& rPool ) : maItemSet(  rPool ), mnStartPara(0), mnParaCount(0), mnCellX(0), mnRowSpan(1) {}
};

}

typedef std::shared_ptr< RTFCellInfo > RTFCellInfoPtr;
typedef std::vector< RTFCellInfoPtr > RTFColumnVector;

typedef std::shared_ptr< RTFColumnVector > RTFColumnVectorPtr;

class SdrTableRTFParser
{
public:
    explicit SdrTableRTFParser( SdrTableObj& rTableObj );

    void Read( SvStream& rStream );

    void ProcToken( RtfImportInfo* pInfo );

    void NextRow();
    void NextColumn();
    void NewCellRow();

    void InsertCell( RtfImportInfo const * pInfo );
    void InsertColumnEdge( sal_Int32 nEdge );

    void FillTable();

    DECL_LINK( RTFImportHdl, RtfImportInfo&, void );

private:
    SdrTableObj&    mrTableObj;
    std::unique_ptr<SdrOutliner> mpOutliner;
    SfxItemPool&    mrItemPool;

    RTFCellDefaultVector maDefaultList;
    RTFCellDefaultVector::iterator maDefaultIterator;

    int             mnLastToken;
    bool            mbNewDef;

    sal_Int32       mnStartPara;

    sal_Int32       mnRowCnt;
    sal_Int32       mnLastEdge;
    sal_Int32       mnVMergeIdx;

    std::vector< sal_Int32 > maColumnEdges;
    std::vector< sal_Int32 >::iterator maLastEdge;
    std::vector< RTFColumnVectorPtr > maRows;

    std::unique_ptr<RTFCellDefault> mpInsDefault;
    RTFCellDefault* mpActDefault;
    RTFCellDefault* mpDefMerge;

    Reference< XTable > mxTable;

    RTFColumnVectorPtr mxLastRow;
    // Copy assignment is forbidden and not implemented.
    SdrTableRTFParser (const SdrTableRTFParser &) = delete;
    SdrTableRTFParser & operator= (const SdrTableRTFParser &) = delete;
};

SdrTableRTFParser::SdrTableRTFParser( SdrTableObj& rTableObj )
: mrTableObj( rTableObj )
, mpOutliner( SdrMakeOutliner( OutlinerMode::TextObject, rTableObj.getSdrModelFromSdrObject() ) )
, mrItemPool( rTableObj.getSdrModelFromSdrObject().GetItemPool() )
, mnLastToken( 0 )
, mbNewDef( false )
, mnStartPara( 0 )
, mnRowCnt( 0 )
, mnLastEdge( 0 )
, mnVMergeIdx ( 0 )
, mpActDefault( nullptr )
, mpDefMerge( nullptr )
, mxTable( rTableObj.getTable() )
{
    mpOutliner->SetUpdateMode(true);
    mpOutliner->SetStyleSheet( 0, mrTableObj.GetStyleSheet() );
    mpInsDefault.reset( new RTFCellDefault( &mrItemPool ) );
}

void SdrTableRTFParser::Read( SvStream& rStream )
{
    EditEngine& rEdit = const_cast< EditEngine& >( mpOutliner->GetEditEngine() );

    Link<RtfImportInfo&,void> aOldLink( rEdit.GetRtfImportHdl() );
    rEdit.SetRtfImportHdl( LINK( this, SdrTableRTFParser, RTFImportHdl ) );
    mpOutliner->Read( rStream, OUString(), EETextFormat::Rtf );
    rEdit.SetRtfImportHdl( aOldLink );

    FillTable();
}

IMPL_LINK( SdrTableRTFParser, RTFImportHdl, RtfImportInfo&, rInfo, void )
{
    switch ( rInfo.eState )
    {
        case RtfImportState::NextToken:
            ProcToken( &rInfo );
            break;
        case RtfImportState::UnknownAttr:
            ProcToken( &rInfo );
            break;
        case RtfImportState::Start:
        {
            SvxRTFParser* pParser = static_cast<SvxRTFParser*>(rInfo.pParser);
            pParser->SetAttrPool( &mrItemPool );
            RTFPardAttrMapIds& rMap = pParser->GetPardMap();
            rMap.nBox = SDRATTR_TABLE_BORDER;
        }
            break;
        case RtfImportState::End:
            if ( rInfo.aSelection.nEndPos )
            {
                mpActDefault = nullptr;
                rInfo.nToken = RTF_PAR;
                rInfo.aSelection.nEndPara++;
                ProcToken( &rInfo );
            }
            break;
        case RtfImportState::SetAttr:
        case RtfImportState::InsertText:
        case RtfImportState::InsertPara:
            break;
        default:
            SAL_WARN( "svx.table","unknown ImportInfo.eState");
    }
}

void SdrTableRTFParser::NextRow()
{
    mxLastRow = maRows.back();
    mnVMergeIdx = 0;
    ++mnRowCnt;
}

void SdrTableRTFParser::InsertCell( RtfImportInfo const * pInfo )
{

    RTFCellInfoPtr xCellInfo = std::make_shared<RTFCellInfo>(mrItemPool);

    xCellInfo->mnStartPara = mnStartPara;
    xCellInfo->mnParaCount = pInfo->aSelection.nEndPara - 1 - mnStartPara;
    xCellInfo->mnCellX = mpActDefault->mnCellX;
    xCellInfo->mnRowSpan = mpActDefault->mnRowSpan;


    if ( mxLastRow != nullptr )
    {
        sal_Int32 nSize = mxLastRow->size();
        while( mnVMergeIdx < nSize &&
             (*mxLastRow)[mnVMergeIdx]->mnCellX < xCellInfo->mnCellX )
            ++mnVMergeIdx;

        if ( xCellInfo->mnRowSpan == 0 && mnVMergeIdx < nSize )
        {
            RTFCellInfoPtr xLastCell( (*mxLastRow)[mnVMergeIdx] );
            if (xLastCell->mnRowSpan)
                xCellInfo->mxVMergeCell = xLastCell;
            else
                xCellInfo->mxVMergeCell = xLastCell->mxVMergeCell;
        }
    }

    if( !maRows.empty() )
    {
        RTFColumnVectorPtr xColumn( maRows.back() );
        if ( xCellInfo->mxVMergeCell )
        {
            if ( xColumn->empty() ||
                    xColumn->back()->mxVMergeCell != xCellInfo->mxVMergeCell )
                xCellInfo->mxVMergeCell->mnRowSpan++;
        }

        xColumn->push_back( xCellInfo );
    }

    mnStartPara = pInfo->aSelection.nEndPara - 1;
}

void SdrTableRTFParser::InsertColumnEdge( sal_Int32 nEdge )
{
    auto aNextEdge = std::lower_bound( maLastEdge, maColumnEdges.end(), nEdge );

    if ( aNextEdge == maColumnEdges.end() || nEdge != *aNextEdge )
    {
        maLastEdge = maColumnEdges.insert( aNextEdge , nEdge );
        mnLastEdge = nEdge;
    }
}

void SdrTableRTFParser::FillTable()
{
    try
    {
        sal_Int32 nColCount = mxTable->getColumnCount();
        Reference< XTableColumns > xCols( mxTable->getColumns(), UNO_SET_THROW );
        sal_Int32 nColMax = maColumnEdges.size();
        if( nColCount < nColMax )
        {
            xCols->insertByIndex( nColCount, nColMax - nColCount );
            nColCount = mxTable->getColumnCount();
        }

        const OUString sWidth("Width");
        sal_Int32 nCol, nLastEdge = 0;
        for( nCol = 0; nCol < nColCount; nCol++ )
        {
            Reference< XPropertySet > xSet( xCols->getByIndex( nCol ), UNO_QUERY_THROW );
            sal_Int32 nWidth = maColumnEdges[nCol] - nLastEdge;

            xSet->setPropertyValue( sWidth, Any( nWidth ) );
            nLastEdge += nWidth;
        }

        const sal_Int32 nRowCount = mxTable->getRowCount();
        if( nRowCount < mnRowCnt )
        {
            Reference< XTableRows > xRows( mxTable->getRows(), UNO_SET_THROW );
            xRows->insertByIndex( nRowCount, mnRowCnt - nRowCount );
        }

        for( sal_Int32 nRow = 0; nRow < static_cast<sal_Int32>(maRows.size()); nRow++ )
        {
            RTFColumnVectorPtr xColumn( maRows[nRow] );
            nCol = 0;
            auto aEdge = maColumnEdges.begin();
            for( sal_Int32 nIdx = 0; nCol < nColMax && nIdx < static_cast<sal_Int32>(xColumn->size()); nIdx++ )
            {
                RTFCellInfoPtr xCellInfo( (*xColumn)[nIdx] );

                CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
                if( xCell.is() && xCellInfo )
                {
                    const SfxPoolItem *pPoolItem = nullptr;
                    if( xCellInfo->maItemSet.GetItemState(SDRATTR_TABLE_BORDER,false,&pPoolItem)==SfxItemState::SET)
                        xCell->SetMergedItem( *pPoolItem );

                    std::unique_ptr<OutlinerParaObject> pTextObject(mpOutliner->CreateParaObject( xCellInfo->mnStartPara, xCellInfo->mnParaCount ));
                    if( pTextObject )
                    {
                        SdrOutliner& rOutliner=mrTableObj.ImpGetDrawOutliner();
                        rOutliner.SetUpdateMode(true);
                        rOutliner.SetText( *pTextObject );
                        mrTableObj.NbcSetOutlinerParaObjectForText( rOutliner.CreateParaObject(), xCell.get() );
                    }

                    sal_Int32 nLastRow = nRow;
                    if ( xCellInfo->mnRowSpan )
                        nLastRow += xCellInfo->mnRowSpan - 1;

                    aEdge = std::lower_bound( aEdge, maColumnEdges.end(), xCellInfo->mnCellX );
                    sal_Int32 nLastCol = nCol;
                    if ( aEdge != maColumnEdges.end() )
                    {
                        nLastCol = std::distance( maColumnEdges.begin(), aEdge);
                        ++aEdge;
                    }

                    if ( nLastCol > nCol || nLastRow > nRow )
                    {
                         Reference< XMergeableCellRange > xRange( mxTable->createCursorByRange( mxTable->getCellRangeByPosition( nCol, nRow, nLastCol, nLastRow ) ), UNO_QUERY_THROW );
                         if( xRange->isMergeable() )
                             xRange->merge();
                    }
                    nCol = nLastCol + 1;
                }
            }
        }

        tools::Rectangle aRect( mrTableObj.GetSnapRect() );
        aRect.SetRight( aRect.Left() + nLastEdge );
        mrTableObj.NbcSetSnapRect( aRect );

    }
    catch( Exception& )
    {
        OSL_FAIL("sdr::table::SdrTableRTFParser::InsertCell(), exception caught!" );
    }
}

void SdrTableRTFParser::NewCellRow()
{
    if( mbNewDef )
    {
        mbNewDef = false;

        maRows.push_back( std::make_shared<std::vector<std::shared_ptr<RTFCellInfo>>>( ) );
    }
    mpDefMerge = nullptr;
    maDefaultIterator = maDefaultList.begin();

    NextColumn();

    DBG_ASSERT( mpActDefault, "NewCellRow: pActDefault==0" );
}

void SdrTableRTFParser::NextColumn()
{
    if( maDefaultIterator != maDefaultList.end() )
        mpActDefault = (*maDefaultIterator++).get();
    else
        mpActDefault = nullptr;
}

static long TwipsToHundMM( long nIn )
{
    long nRet = OutputDevice::LogicToLogic( nIn, MapUnit::MapTwip, MapUnit::Map100thMM );
    return nRet;
}

void SdrTableRTFParser::ProcToken( RtfImportInfo* pInfo )
{
    switch ( pInfo->nToken )
    {
        case RTF_TROWD:         // denotes table row default, before RTF_CELLX
        {
            maDefaultList.clear();
            mpDefMerge = nullptr;
            mnLastToken = pInfo->nToken;
            maLastEdge = maColumnEdges.begin();
            mnLastEdge = 0;
        }
        break;
        case RTF_CLMGF:         // The first cell of cells to be merged
        {
            mpDefMerge = mpInsDefault.get();
            mnLastToken = pInfo->nToken;
        }
        break;
        case RTF_CLMRG:         // A cell to be merged with the preceding cell
        {
            if ( !mpDefMerge )
                mpDefMerge = maDefaultList.back().get();
            DBG_ASSERT( mpDefMerge, "RTF_CLMRG: pDefMerge==0" );
            if( mpDefMerge )
                mpDefMerge->mnColSpan++;
            mpInsDefault->mnColSpan = 0;
            mnLastToken = pInfo->nToken;
        }
        break;
        case RTF_CLVMGF:
        {
            mnLastToken = pInfo->nToken;
        }
        break;
        case RTF_CLVMRG:
        {
            mpInsDefault->mnRowSpan = 0;
            mnLastToken = pInfo->nToken;
        }
        break;
        case RTF_CELLX:         // closes cell default
        {
            mbNewDef = true;
            std::shared_ptr<RTFCellDefault> pDefault( mpInsDefault.release() );
            maDefaultList.push_back( pDefault );


            const sal_Int32 nSize = TwipsToHundMM( pInfo->nTokenValue );
            if ( nSize > mnLastEdge )
                InsertColumnEdge( nSize );

            pDefault->mnCellX = nSize;
            // Record cellx in the first merged cell.
            if ( mpDefMerge && pDefault->mnColSpan == 0 )
                mpDefMerge->mnCellX = nSize;

            mpInsDefault.reset( new RTFCellDefault( &mrItemPool ) );

            mnLastToken = pInfo->nToken;
        }
        break;
        case RTF_INTBL:         // before the first RTF_CELL
        {
            if ( mnLastToken != RTF_INTBL && mnLastToken != RTF_CELL && mnLastToken != RTF_PAR )
            {
                NewCellRow();
                mnLastToken = pInfo->nToken;
            }
        }
        break;
        case RTF_CELL:          // denotes the end of a cell.
        {
            DBG_ASSERT( mpActDefault, "RTF_CELL: pActDefault==0" );
            if ( mbNewDef || !mpActDefault )
                NewCellRow();
            if ( !mpActDefault )
                mpActDefault = mpInsDefault.get();
            if ( mpActDefault->mnColSpan > 0 )
            {
                InsertCell(pInfo);
            }
            NextColumn();
            mnLastToken = pInfo->nToken;
        }
        break;
        case RTF_ROW:           // means the end of a row
        {
            NextRow();
            mnLastToken = pInfo->nToken;
        }
        break;
        case RTF_PAR:           // Paragraph
            mnLastToken = pInfo->nToken;
            break;
        default:
        {   // do not set nLastToken
            switch ( pInfo->nToken & ~(0xff | RTF_TABLEDEF) )
            {
                case RTF_SHADINGDEF:
//                  ((SvxRTFParser*)pInfo->pParser)->ReadBackgroundAttr(pInfo->nToken, mpInsDefault->maItemSet, sal_True );
                break;
                case RTF_BRDRDEF:
                    static_cast<SvxRTFParser*>(pInfo->pParser)->ReadBorderAttr(pInfo->nToken, mpInsDefault->maItemSet, true );
                break;
            }
        }
    }
}

void ImportAsRTF( SvStream& rStream, SdrTableObj& rObj )
{
    SdrTableRTFParser aParser( rObj );
    aParser.Read( rStream );
}

}

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