Avoid potential negative array index access to cached text.
[LibreOffice.git] / writerfilter / source / dmapper / TableData.hxx
blob53b99ed7c7fdcdb301c933ce91fba05756db2d5f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #pragma once
22 #include <com/sun/star/text/XTextRange.hpp>
24 #include "PropertyMap.hxx"
26 #include <utility>
27 #include <vector>
29 namespace writerfilter::dmapper
32 /**
33 Class containing the data to describe a table cell.
35 class CellData final : public virtual SvRefBase
37 /**
38 Handle to start of cell.
40 css::uno::Reference<css::text::XTextRange> mStart;
42 /**
43 Handle to end of cell.
45 css::uno::Reference<css::text::XTextRange> mEnd;
47 /**
48 Pointer to properties of cell.
50 TablePropertyMapPtr mpProps;
52 bool mbOpen;
54 sal_uInt32 m_nGridSpan; ///< number of grid columns in the parent table's table grid which this cell defines
56 public:
57 typedef tools::SvRef<CellData> Pointer_t;
59 CellData(css::uno::Reference<css::text::XTextRange> const & start, TablePropertyMapPtr pProps)
60 : mStart(start), mEnd(start), mpProps(std::move(pProps)), mbOpen(true)
61 , m_nGridSpan(1)
65 /**
66 Set the end handle of a cell.
68 @param end the end handle of the cell
70 void setEnd(css::uno::Reference<css::text::XTextRange> const & end) { mEnd = end; mbOpen = false; }
72 /**
73 Adds properties to the cell.
75 @param pProps the properties to add
77 void insertProperties(TablePropertyMapPtr pProps)
79 if( mpProps )
80 mpProps->InsertProps(pProps.get());
81 else
82 mpProps = pProps;
85 /**
86 Return start handle of the cell.
88 const css::uno::Reference<css::text::XTextRange>& getStart() const { return mStart; }
90 /**
91 Return end handle of the cell.
93 const css::uno::Reference<css::text::XTextRange>& getEnd() const { return mEnd; }
95 /**
96 Return properties of the cell.
98 const TablePropertyMapPtr& getProperties() const { return mpProps; }
100 bool isOpen() const { return mbOpen; }
102 sal_uInt32 getGridSpan() const { return m_nGridSpan; }
103 void setGridSpan( sal_uInt32 nSpan ) { m_nGridSpan = nSpan; }
105 void SetStart(const css::uno::Reference<css::text::XTextRange>& xStart) { mStart = xStart; }
107 bool IsValid() const;
111 Class to handle data of a table row.
113 class RowData final : public virtual SvRefBase
115 typedef ::std::vector<CellData::Pointer_t> Cells;
118 the cell data of the row
120 Cells mCells;
123 the properties of the row
125 mutable TablePropertyMapPtr mpProperties;
127 sal_uInt32 m_nGridBefore; ///< number of grid columns in the parent table's table grid which must be skipped before the contents of this table row are added to the parent table
128 sal_uInt32 m_nGridAfter; ///< number of grid columns in the parent table's table grid which shall be left after the last cell in the table row
130 public:
131 typedef tools::SvRef<RowData> Pointer_t;
133 RowData()
134 : m_nGridBefore(0)
135 , m_nGridAfter(0)
139 RowData(const RowData& rRowData)
140 : SvRefBase(), mCells(rRowData.mCells), mpProperties(rRowData.mpProperties)
141 , m_nGridBefore(rRowData.m_nGridBefore)
142 , m_nGridAfter(rRowData.m_nGridAfter)
147 Add a cell to the row.
149 @param start the start handle of the cell
150 @param end the end handle of the cell
151 @param pProps the properties of the cell
152 @param bAddBefore true: add an empty cell at beginning of the row for gridBefore
154 void addCell(const css::uno::Reference<css::text::XTextRange>& start, TablePropertyMapPtr pProps, bool bAddBefore = false)
156 CellData::Pointer_t pCellData(new CellData(start, pProps));
157 if (bAddBefore)
159 mCells.insert(mCells.begin(), pCellData);
160 mCells[0]->setEnd(start);
162 else
163 mCells.push_back(pCellData);
166 void endCell(const css::uno::Reference<css::text::XTextRange>& end)
168 if (mCells.size() > 0)
169 mCells.back()->setEnd(end);
172 bool isCellOpen() const
174 return mCells.size() > 0 && mCells.back()->isOpen();
177 void SetCellStart(const css::uno::Reference<css::text::XTextRange>& xStart)
179 if (mCells.empty())
181 return;
184 mCells.back()->SetStart(xStart);
187 bool IsCellValid() const
189 return !mCells.empty() && mCells.back()->IsValid();
193 Add properties to the row.
195 @param pProperties the properties to set
197 void insertProperties(TablePropertyMapPtr pProperties)
199 if( pProperties )
201 if( !mpProperties )
202 mpProperties = pProperties;
203 else
204 mpProperties->InsertProps(pProperties.get());
209 Add properties to the last cell of the row.
211 void insertCellProperties(const TablePropertyMapPtr& pProps)
213 if (!mCells.empty())
214 mCells.back()->insertProperties(pProps);
218 Return number of cells in the row.
220 unsigned int getCellCount() const
222 return mCells.size();
226 Return start handle of a cell in the row.
228 @param i index of the cell
230 const css::uno::Reference<css::text::XTextRange>& getCellStart(unsigned int i) const
232 return mCells[i]->getStart();
236 Return end handle of a cell in the row.
238 @param i index of the cell
240 const css::uno::Reference<css::text::XTextRange>& getCellEnd(unsigned int i) const
242 return mCells[i]->getEnd();
246 Return the properties of a cell in the row.
248 @param i index of the cell
250 TablePropertyMapPtr const & getCellProperties(unsigned int i) const
252 return mCells[i]->getProperties();
256 Return properties of the row.
258 const TablePropertyMapPtr& getProperties() const
260 return mpProperties;
263 sal_uInt32 getGridBefore() const { return m_nGridBefore; }
264 void setGridBefore(sal_uInt32 nSkipGrids) { m_nGridBefore = nSkipGrids; }
265 sal_uInt32 getGridAfter() const { return m_nGridAfter; }
266 void setGridAfter(sal_uInt32 nSkipGrids) { m_nGridAfter = nSkipGrids; }
267 sal_uInt32 getGridSpan(sal_uInt32 i) { return mCells[i]->getGridSpan(); }
268 std::vector< sal_uInt32 > getGridSpans()
270 std::vector< sal_uInt32 > nRet;
271 for (auto const& aCell: mCells)
272 nRet.push_back(aCell->getGridSpan());
273 return nRet;
275 void setCurrentGridSpan(sal_uInt32 nSpan, bool bFirstCell = false)
277 if ( mCells.size() )
279 if ( bFirstCell )
280 mCells.front()->setGridSpan(nSpan);
281 else
282 mCells.back()->setGridSpan(nSpan);
288 Class that holds the data of a table.
290 class TableData : public virtual SvRefBase
292 typedef ::std::vector<RowData::Pointer_t> Rows;
295 the data of the rows of the table
297 Rows mRows;
300 pointer to the data of the current row (while building up the table data).
302 RowData::Pointer_t mpRow;
305 depth of the current table in a hierarchy of tables
307 unsigned int mnDepth;
310 initialize mpRow
312 void newRow() { mpRow = RowData::Pointer_t(new RowData()); }
314 public:
315 typedef tools::SvRef<TableData> Pointer_t;
317 explicit TableData(unsigned int nDepth) : mnDepth(nDepth) { newRow(); }
320 End the current row.
322 Sets properties of the current row and pushes the row to the
323 back of the rows currently contained in the table.
325 @param pProperties properties of the row to be ended
327 void endRow(TablePropertyMapPtr pProperties)
329 mpRow->insertProperties(pProperties);
330 mRows.push_back(mpRow);
331 newRow();
335 Add a cell to the current row.
337 @param start start handle of the cell
338 @param end end handle of the cell
339 @param pProps properties of the cell
341 void addCell(const css::uno::Reference<css::text::XTextRange>& start, TablePropertyMapPtr pProps)
343 mpRow->addCell(start, pProps);
347 End the current cell of the current row.
349 @parm end end handle of the cell
351 void endCell(const css::uno::Reference<css::text::XTextRange>& end)
353 mpRow->endCell(end);
357 Return if the current cell of the current row is open.
359 bool isCellOpen() const
361 return mpRow->isCellOpen();
364 void SetCellStart(const css::uno::Reference<css::text::XTextRange>& xStart)
366 mpRow->SetCellStart(xStart);
369 bool IsCellValid() const
371 return mpRow->IsCellValid();
375 Insert properties to the current cell of the current row.
377 @param pProps the properties to add
379 void insertCellProperties(const TablePropertyMapPtr& pProps)
381 mpRow->insertCellProperties(pProps);
385 Return number of rows in the table.
387 unsigned int getRowCount() const
389 return mRows.size();
393 Return depth of table in surrounding table hierarchy.
395 unsigned int getDepth() const
397 return mnDepth;
401 Return row data of a certain row.
403 @param i index of the row
405 RowData::Pointer_t const & getRow(unsigned int i) const
407 return mRows[i];
410 const RowData::Pointer_t& getCurrentRow() const
412 return mpRow;
419 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */