Coverage Report

Created: 2026-03-31 11:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/oox/source/drawingml/table/tableproperties.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
#include <drawingml/table/tableproperties.hxx>
21
#include <drawingml/table/tablestylelist.hxx>
22
#include <oox/drawingml/drawingmltypes.hxx>
23
#include <com/sun/star/table/XTable.hpp>
24
#include <com/sun/star/table/XMergeableCellRange.hpp>
25
#include <oox/core/xmlfilterbase.hxx>
26
#include "predefined-table-styles.cxx"
27
28
using namespace ::oox::core;
29
using namespace ::com::sun::star;
30
using namespace ::com::sun::star::uno;
31
using namespace ::com::sun::star::beans;
32
using namespace ::com::sun::star::table;
33
34
namespace oox::drawingml::table {
35
36
TableProperties::TableProperties()
37
126
: mbFirstRow( false )
38
126
, mbFirstCol( false )
39
126
, mbLastRow( false )
40
126
, mbLastCol( false )
41
126
, mbBandRow( false )
42
126
, mbBandCol( false )
43
126
{
44
126
    maBgColor.setUnused();
45
126
}
46
47
static void CreateTableRows( const uno::Reference< XTableRows >& xTableRows, const std::vector< TableRow >& rvTableRows )
48
126
{
49
126
    if ( rvTableRows.size() > 1 )
50
120
        xTableRows->insertByIndex( 0, rvTableRows.size() - 1 );
51
126
    std::vector< TableRow >::const_iterator aTableRowIter( rvTableRows.begin() );
52
126
    uno::Reference< container::XIndexAccess > xIndexAccess( xTableRows, UNO_QUERY_THROW );
53
126
    sal_Int32 nCols = std::min<sal_Int32>(xIndexAccess->getCount(), rvTableRows.size());
54
717
    for (sal_Int32 n = 0; n < nCols; ++n)
55
591
    {
56
591
        Reference< XPropertySet > xPropSet( xIndexAccess->getByIndex( n ), UNO_QUERY_THROW );
57
591
        xPropSet->setPropertyValue( u"Height"_ustr, Any( static_cast< sal_Int32 >( aTableRowIter->getHeight() / 360 ) ) );
58
591
        ++aTableRowIter;
59
591
    }
60
126
}
61
62
static void CreateTableColumns( const Reference< XTableColumns >& xTableColumns, const std::vector< sal_Int32 >& rvTableGrid )
63
126
{
64
126
    if ( rvTableGrid.size() > 1 )
65
120
        xTableColumns->insertByIndex( 0, rvTableGrid.size() - 1 );
66
126
    std::vector< sal_Int32 >::const_iterator aTableGridIter( rvTableGrid.begin() );
67
126
    uno::Reference< container::XIndexAccess > xIndexAccess( xTableColumns, UNO_QUERY_THROW );
68
126
    sal_Int32 nCols = std::min<sal_Int32>(xIndexAccess->getCount(), rvTableGrid.size());
69
725
    for (sal_Int32 n = 0; n < nCols; ++n)
70
599
    {
71
599
        Reference< XPropertySet > xPropSet( xIndexAccess->getByIndex( n ), UNO_QUERY_THROW );
72
599
        xPropSet->setPropertyValue( u"Width"_ustr, Any( static_cast< sal_Int32 >( *aTableGridIter++ / 360 ) ) );
73
599
    }
74
126
}
75
76
static void MergeCells( const uno::Reference< XTable >& xTable, sal_Int32 nCol, sal_Int32 nRow, sal_Int32 nColSpan, sal_Int32 nRowSpan )
77
0
{
78
0
   if( xTable.is() ) try
79
0
   {
80
0
       Reference< XMergeableCellRange > xRange( xTable->createCursorByRange( xTable->getCellRangeByPosition( nCol, nRow,nCol + nColSpan - 1, nRow + nRowSpan - 1 ) ), UNO_QUERY_THROW );
81
0
       if( xRange->isMergeable() )
82
0
               xRange->merge();
83
0
   }
84
0
   catch( Exception& )
85
0
   {
86
0
   }
87
0
}
88
89
const TableStyle& TableProperties::getUsedTableStyle( const ::oox::core::XmlFilterBase& rFilterBase, std::unique_ptr<TableStyle>& rTableStyleToDelete )
90
126
{
91
126
    ::oox::core::XmlFilterBase& rBase( const_cast< ::oox::core::XmlFilterBase& >( rFilterBase ) );
92
93
126
    TableStyle* pTableStyle = nullptr;
94
126
    if ( mpTableStyle )
95
0
        pTableStyle = &*mpTableStyle;
96
126
    else if ( !getStyleId().isEmpty() && rBase.getTableStyles() )
97
40
    {
98
40
        const std::vector< TableStyle >& rTableStyles( rBase.getTableStyles()->getTableStyles() );
99
40
        const OUString aStyleId( getStyleId() );
100
101
40
        for (auto const& tableStyle : rTableStyles)
102
34
        {
103
34
            if ( const_cast< TableStyle& >(tableStyle).getStyleId() == aStyleId )
104
32
            {
105
32
                pTableStyle = &const_cast< TableStyle& >(tableStyle);
106
32
                break;  // we get the correct style
107
32
            }
108
34
        }
109
        //if the pptx just has table style id, but no table style content, we will create the table style ourselves
110
40
        if (!pTableStyle)
111
8
        {
112
8
            rTableStyleToDelete = CreateTableStyle(aStyleId);
113
8
            pTableStyle = rTableStyleToDelete.get();
114
8
        }
115
40
    }
116
117
126
    if ( !pTableStyle )
118
86
    {
119
86
        static TableStyle theDefaultTableStyle;
120
86
        return theDefaultTableStyle;
121
86
    }
122
123
40
    return *pTableStyle;
124
126
}
125
126
void TableProperties::pushToPropSet(const ::oox::core::XmlFilterBase& rFilterBase,
127
                                    const Reference<XPropertySet>& xPropSet,
128
                                    const TextListStylePtr& pMasterTextListStyle)
129
126
{
130
126
    uno::Reference<XColumnRowRange> xColumnRowRange(xPropSet->getPropertyValue(u"Model"_ustr),
131
126
                                                    uno::UNO_QUERY_THROW);
132
133
126
    CreateTableColumns(xColumnRowRange->getColumns(), mvTableGrid);
134
126
    CreateTableRows(xColumnRowRange->getRows(), mvTableRows);
135
136
126
    std::unique_ptr<TableStyle> xTableStyleToDelete;
137
126
    const TableStyle& rTableStyle(getUsedTableStyle(rFilterBase, xTableStyleToDelete));
138
126
    sal_Int32 nRow = 0;
139
140
126
    for (auto& tableRow : mvTableRows)
141
591
    {
142
591
        sal_Int32 nColumn = 0;
143
591
        sal_Int32 nColumnSize = tableRow.getTableCells().size();
144
591
        sal_Int32 nRemovedColumn = 0;
145
591
        sal_Int32 nRemovedRow = 0;
146
147
3.45k
        for (sal_Int32 nColIndex = 0; nColIndex < nColumnSize; nColIndex++)
148
2.86k
        {
149
2.86k
            TableCell& rTableCell(tableRow.getTableCells().at(nColIndex));
150
151
2.86k
            if (!rTableCell.getvMerge() && !rTableCell.gethMerge())
152
2.86k
            {
153
2.86k
                uno::Reference<XTable> xTable(xColumnRowRange, uno::UNO_QUERY_THROW);
154
2.86k
                bool bMerged = false;
155
156
2.86k
                if ((rTableCell.getRowSpan() > 1) || (rTableCell.getGridSpan() > 1))
157
0
                {
158
0
                    MergeCells(xTable, nColumn, nRow, rTableCell.getGridSpan(),
159
0
                               rTableCell.getRowSpan());
160
161
0
                    if (rTableCell.getGridSpan() > 1)
162
0
                    {
163
0
                        nRemovedColumn = (rTableCell.getGridSpan() - 1);
164
                        // MergeCells removes columns. Our loop does not know about those
165
                        // removed columns and we skip handling those removed columns.
166
0
                        nColIndex += nRemovedColumn;
167
                        // It will adjust new column number after push current column's
168
                        // props with pushToXCell.
169
0
                        bMerged = true;
170
0
                    }
171
172
0
                    if (rTableCell.getRowSpan() > 1)
173
0
                        nRemovedRow = (rTableCell.getRowSpan() - 1);
174
0
                }
175
176
2.86k
                Reference<XCellRange> xCellRange(xTable, UNO_QUERY_THROW);
177
2.86k
                Reference<XCell> xCell;
178
179
2.86k
                if (nRemovedColumn)
180
0
                {
181
0
                    try
182
0
                    {
183
0
                        xCell = xCellRange->getCellByPosition(nColumn, nRow);
184
0
                    }
185
                    // Exception can come from TableModel::getCellByPosition when a column
186
                    // is removed while merging columns. So adjust again here.
187
0
                    catch (Exception&)
188
0
                    {
189
0
                        xCell = xCellRange->getCellByPosition(nColumn - nRemovedColumn, nRow);
190
0
                    }
191
0
                }
192
2.86k
                else
193
2.86k
                    xCell = xCellRange->getCellByPosition(nColumn, nRow);
194
195
196
2.86k
                sal_Int32 nMaxCol = tableRow.getTableCells().size() - nRemovedColumn - 1;
197
2.86k
                sal_Int32 nMaxRow =  mvTableRows.size() - nRemovedRow - 1;
198
199
2.86k
                rTableCell.pushToXCell(rFilterBase, pMasterTextListStyle, xCell, *this, rTableStyle,
200
2.86k
                                       nColumn, nMaxCol, nRow, nMaxRow);
201
202
2.86k
                if (bMerged)
203
0
                    nColumn += nRemovedColumn;
204
205
2.86k
                nRemovedRow = 0;
206
2.86k
            }
207
2.86k
            ++nColumn;
208
2.86k
        }
209
591
        ++nRow;
210
591
    }
211
212
126
    xTableStyleToDelete.reset();
213
126
}
214
}
215
216
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */