Avoid potential negative array index access to cached text.
[LibreOffice.git] / writerfilter / source / dmapper / TablePropertiesHandler.cxx
blob8ea3eae5c056c227c54b459b10492be32c459171
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 #include <sal/config.h>
22 #include <string_view>
24 #include "BorderHandler.hxx"
25 #include "CellColorHandler.hxx"
26 #include "CellMarginHandler.hxx"
27 #include "ConversionHelper.hxx"
28 #include "MeasureHandler.hxx"
29 #include "TrackChangesHandler.hxx"
30 #include "TablePropertiesHandler.hxx"
31 #include "TagLogger.hxx"
32 #include "TDefTableHandler.hxx"
33 #include "DomainMapperTableManager.hxx"
35 #include <ooxml/resourceids.hxx>
37 #include <com/sun/star/text/VertOrientation.hpp>
38 #include <oox/token/tokens.hxx>
40 using namespace com::sun::star;
41 using namespace oox;
43 namespace writerfilter::dmapper {
45 TablePropertiesHandler::TablePropertiesHandler() :
46 m_pCurrentInteropGrabBag(nullptr),
47 m_pTableManager( nullptr )
51 bool TablePropertiesHandler::sprm(Sprm & rSprm)
53 #ifdef DBG_UTIL
54 TagLogger::getInstance().startElement("TablePropertiesHandler.sprm");
55 TagLogger::getInstance().attribute("sprm", rSprm.toString());
56 #endif
58 bool bRet = true;
59 sal_uInt32 nSprmId = rSprm.getId();
60 Value::Pointer_t pValue = rSprm.getValue();
61 sal_Int32 nIntValue = (pValue ? pValue->getInt() : 0);
62 switch( nSprmId )
64 case NS_ooxml::LN_CT_TrPrBase_jc:
65 case NS_ooxml::LN_CT_TblPrBase_jc:
67 sal_Int16 nOrient = ConversionHelper::convertTableJustification( nIntValue );
68 TablePropertyMapPtr pTableMap( new TablePropertyMap );
69 pTableMap->setValue( TablePropertyMap::HORI_ORIENT, nOrient );
70 insertTableProps( pTableMap );
72 break;
73 case NS_ooxml::LN_CT_TrPrBase_trHeight:
75 //contains unit and value
76 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
77 if( pProperties )
78 { //contains attributes x2902 (LN_unit) and x17e2 (LN_trleft)
79 MeasureHandlerPtr pMeasureHandler( new MeasureHandler );
80 pProperties->resolve(*pMeasureHandler);
81 TablePropertyMapPtr pPropMap( new TablePropertyMap );
83 pPropMap->Insert( PROP_SIZE_TYPE, uno::Any( pMeasureHandler->GetRowHeightSizeType() ), false);
84 pPropMap->Insert( PROP_HEIGHT, uno::Any(pMeasureHandler->getMeasureValue() ));
86 insertRowProps(pPropMap);
89 break;
90 case NS_ooxml::LN_CT_TrPr_ins:
91 case NS_ooxml::LN_CT_TrPr_del:
93 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
94 if( pProperties )
96 sal_Int32 nToken = sal_Int32();
97 switch( nSprmId )
99 case NS_ooxml::LN_CT_TrPr_ins:
100 nToken = XML_tableRowInsert;
101 break;
102 case NS_ooxml::LN_CT_TrPr_del:
103 nToken = XML_tableRowDelete;
104 break;
106 auto pTrackChangesHandler = std::make_shared<TrackChangesHandler>( nToken );
107 pProperties->resolve(*pTrackChangesHandler);
108 TablePropertyMapPtr pPropMap( new TablePropertyMap );
110 // Add the 'track changes' properties to the 'table row' via UNO.
111 // This way - in the SW core - when it receives this - it will create a new 'Table Redline' object for that row
112 uno::Sequence<beans::PropertyValue> aTableRedlineProperties = pTrackChangesHandler->getRedlineProperties();
113 pPropMap->Insert( PROP_TABLE_REDLINE_PARAMS , uno::Any( aTableRedlineProperties ));
114 insertRowProps(pPropMap);
117 break;
118 case NS_ooxml::LN_CT_TcPrBase_cellIns:
119 case NS_ooxml::LN_CT_TcPrBase_cellDel:
121 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
122 if( pProperties )
124 sal_Int32 nToken;
125 switch( nSprmId )
127 case NS_ooxml::LN_CT_TcPrBase_cellIns:
128 nToken = XML_tableCellInsert;
129 break;
130 case NS_ooxml::LN_CT_TcPrBase_cellDel:
131 nToken = XML_tableCellDelete;
132 break;
133 default:
134 throw lang::IllegalArgumentException("illegal redline token type", nullptr, 0);
135 break;
137 auto pTrackChangesHandler = std::make_shared<TrackChangesHandler>( nToken );
138 pProperties->resolve(*pTrackChangesHandler);
139 TablePropertyMapPtr pPropMap( new TablePropertyMap );
141 // Add the 'track changes' properties to the 'table row' via UNO.
142 // This way - in the SW core - when it receives this - it will create a new 'Table Redline' object for that row
143 uno::Sequence<beans::PropertyValue> aTableRedlineProperties = pTrackChangesHandler->getRedlineProperties();
144 pPropMap->Insert( PROP_TABLE_REDLINE_PARAMS , uno::Any( aTableRedlineProperties ));
145 cellProps(pPropMap);
148 break;
149 case NS_ooxml::LN_CT_TrPrBase_cantSplit:
151 //row can't break across pages if nIntValue == 1
152 TablePropertyMapPtr pPropMap( new TablePropertyMap );
153 pPropMap->Insert( PROP_IS_SPLIT_ALLOWED, uno::Any( nIntValue != 1 ) );
154 insertRowProps(pPropMap);
156 break;
157 case NS_ooxml::LN_CT_TcPrBase_vAlign:
159 sal_Int16 nVertOrient = text::VertOrientation::NONE;
160 switch( nIntValue )
162 case NS_ooxml::LN_Value_ST_VerticalJc_center: nVertOrient = text::VertOrientation::CENTER; break;
163 case NS_ooxml::LN_Value_ST_VerticalJc_bottom: nVertOrient = text::VertOrientation::BOTTOM; break;
164 default:;
166 TablePropertyMapPtr pCellPropMap( new TablePropertyMap() );
167 pCellPropMap->Insert( PROP_VERT_ORIENT, uno::Any( nVertOrient ) );
168 //todo: in ooxml import the value of m_ncell is wrong
169 cellProps( pCellPropMap );
170 if (m_pCurrentInteropGrabBag)
172 OUString aVertOrient;
173 switch( nIntValue )
175 case NS_ooxml::LN_Value_ST_VerticalJc_top: aVertOrient = "top"; break;
176 case NS_ooxml::LN_Value_ST_VerticalJc_center: aVertOrient = "center"; break;
177 case NS_ooxml::LN_Value_ST_VerticalJc_both: aVertOrient = "both"; break;
178 case NS_ooxml::LN_Value_ST_VerticalJc_bottom: aVertOrient = "bottom"; break;
180 if (!aVertOrient.isEmpty())
182 beans::PropertyValue aValue;
183 aValue.Name = "vAlign";
184 aValue.Value <<= aVertOrient;
185 m_pCurrentInteropGrabBag->push_back(aValue);
189 break;
190 case NS_ooxml::LN_CT_TblPrBase_tblBorders: //table borders, might be defined in table style
192 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
193 if( pProperties )
195 auto pBorderHandler = std::make_shared<BorderHandler>(true);
196 if (m_pCurrentInteropGrabBag)
197 pBorderHandler->enableInteropGrabBag("tblBorders");
198 pProperties->resolve(*pBorderHandler);
199 if (m_pCurrentInteropGrabBag)
200 m_pCurrentInteropGrabBag->push_back(pBorderHandler->getInteropGrabBag());
201 TablePropertyMapPtr pTablePropMap( new TablePropertyMap );
202 pTablePropMap->InsertProps(pBorderHandler->getProperties());
204 #ifdef DBG_UTIL
205 pTablePropMap->dumpXml();
206 #endif
207 insertTableProps( pTablePropMap );
210 break;
211 case NS_ooxml::LN_CT_TblPrBase_tblLayout:
213 DomainMapperTableManager* pManager = dynamic_cast<DomainMapperTableManager*>(m_pTableManager);
214 if (pManager)
215 pManager->SetLayoutType(static_cast<sal_uInt32>(nIntValue));
217 break;
218 case NS_ooxml::LN_CT_TblPrEx_tblBorders:
220 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
221 if( pProperties)
223 auto pBorderHandler = std::make_shared<BorderHandler>(true);
224 pProperties->resolve(*pBorderHandler);
225 TablePropertyMapPtr pTablePropMap( new TablePropertyMap );
226 pTablePropMap->InsertProps(pBorderHandler->getProperties());
228 #ifdef DBG_UTIL
229 pTablePropMap->dumpXml();
230 #endif
231 tableExceptionProps( pTablePropMap );
234 break;
235 case NS_ooxml::LN_CT_TcPrBase_tcBorders ://cell borders
236 //contains CT_TcBorders_left, right, top, bottom
238 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
239 if( pProperties )
241 //in OOXML there's one set of borders at each cell (if there is any)
242 tools::SvRef< TDefTableHandler > pTDefTableHandler( new TDefTableHandler());
243 if (m_pCurrentInteropGrabBag)
244 pTDefTableHandler->enableInteropGrabBag("tcBorders");
245 pProperties->resolve( *pTDefTableHandler );
246 if (m_pCurrentInteropGrabBag)
247 m_pCurrentInteropGrabBag->push_back(pTDefTableHandler->getInteropGrabBag());
248 TablePropertyMapPtr pCellPropMap( new TablePropertyMap );
249 pTDefTableHandler->fillCellProperties( pCellPropMap );
250 cellProps( pCellPropMap );
253 break;
254 case NS_ooxml::LN_CT_TcPrBase_tcMar:
257 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
258 if (pProperties)
260 auto pCellMarginHandler = std::make_shared<CellMarginHandler>();
261 if (m_pCurrentInteropGrabBag)
262 pCellMarginHandler->enableInteropGrabBag("tcMar");
263 pProperties->resolve(*pCellMarginHandler);
264 if (m_pCurrentInteropGrabBag)
265 m_pCurrentInteropGrabBag->push_back(pCellMarginHandler->getInteropGrabBag());
266 TablePropertyMapPtr pCellProperties(new TablePropertyMap);
267 if (pCellMarginHandler->m_bTopMarginValid)
268 pCellProperties->Insert(PROP_TOP_BORDER_DISTANCE, uno::Any(pCellMarginHandler->m_nTopMargin));
269 if (pCellMarginHandler->m_bLeftMarginValid)
270 pCellProperties->Insert(PROP_LEFT_BORDER_DISTANCE, uno::Any(pCellMarginHandler->m_nLeftMargin));
271 if (pCellMarginHandler->m_bBottomMarginValid)
272 pCellProperties->Insert(PROP_BOTTOM_BORDER_DISTANCE, uno::Any(pCellMarginHandler->m_nBottomMargin));
273 if (pCellMarginHandler->m_bRightMarginValid)
274 pCellProperties->Insert(PROP_RIGHT_BORDER_DISTANCE, uno::Any(pCellMarginHandler->m_nRightMargin));
275 cellProps(pCellProperties);
278 break;
279 /* // tdf#123189 skip to keep MSO interoperability
280 case NS_ooxml::LN_CT_TblPrBase_shd:
282 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
283 if( pProperties.get())
285 std::shared_ptr<CellColorHandler> pCellColorHandler( new CellColorHandler);
286 pProperties->resolve( *pCellColorHandler );
287 TablePropertyMapPtr pTablePropMap( new TablePropertyMap );
288 insertTableProps( pCellColorHandler->getProperties() );
292 break;
293 case NS_ooxml::LN_CT_TcPrBase_shd:
295 // each color sprm contains as much colors as cells are in a row
296 //LN_CT_TcPrBase_shd: cell shading contains: LN_CT_Shd_val, LN_CT_Shd_fill, LN_CT_Shd_color
297 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
298 if( pProperties )
300 auto pCellColorHandler = std::make_shared<CellColorHandler>();
301 pCellColorHandler->enableInteropGrabBag("shd"); //enable to store shd unsupported props in grab bag
302 pProperties->resolve( *pCellColorHandler );
303 TablePropertyMapPtr pPropertyMap = pCellColorHandler->getProperties();
304 beans::PropertyValue aGrabBag = pCellColorHandler->getInteropGrabBag();
305 if (m_pCurrentInteropGrabBag)
306 m_pCurrentInteropGrabBag->push_back(aGrabBag);
307 pPropertyMap->Insert( PROP_CELL_INTEROP_GRAB_BAG, aGrabBag.Value );
308 cellProps( pPropertyMap );
311 break;
312 //OOXML table properties
313 case NS_ooxml::LN_CT_TblPrBase_tblCellMar: //cell margins
315 //contains LN_CT_TblCellMar_top, LN_CT_TblCellMar_left, LN_CT_TblCellMar_bottom, LN_CT_TblCellMar_right
316 // LN_CT_TblCellMar_start, LN_CT_TblCellMar_end
317 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
318 if( pProperties )
320 auto pCellMarginHandler = std::make_shared<CellMarginHandler>();
321 if (m_pCurrentInteropGrabBag)
322 pCellMarginHandler->enableInteropGrabBag("tblCellMar");
323 pProperties->resolve( *pCellMarginHandler );
324 if (m_pCurrentInteropGrabBag)
325 m_pCurrentInteropGrabBag->push_back(pCellMarginHandler->getInteropGrabBag());
326 TablePropertyMapPtr pMarginProps( new TablePropertyMap );
327 if( pCellMarginHandler->m_bTopMarginValid )
328 pMarginProps->setValue( TablePropertyMap::CELL_MAR_TOP, pCellMarginHandler->m_nTopMargin );
329 if( pCellMarginHandler->m_bBottomMarginValid )
330 pMarginProps->setValue( TablePropertyMap::CELL_MAR_BOTTOM, pCellMarginHandler->m_nBottomMargin );
331 if( pCellMarginHandler->m_bLeftMarginValid )
332 pMarginProps->setValue( TablePropertyMap::CELL_MAR_LEFT, pCellMarginHandler->m_nLeftMargin );
333 if( pCellMarginHandler->m_bRightMarginValid )
334 pMarginProps->setValue( TablePropertyMap::CELL_MAR_RIGHT, pCellMarginHandler->m_nRightMargin );
335 insertTableProps(pMarginProps);
338 break;
339 case NS_ooxml::LN_CT_TblPrBase_tblInd:
341 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
342 if (pProperties)
344 MeasureHandlerPtr pHandler(new MeasureHandler);
345 if (m_pCurrentInteropGrabBag)
346 pHandler->enableInteropGrabBag("tblInd");
347 pProperties->resolve( *pHandler );
348 if (m_pCurrentInteropGrabBag)
349 m_pCurrentInteropGrabBag->push_back(pHandler->getInteropGrabBag());
350 TablePropertyMapPtr pTblIndMap(new TablePropertyMap);
351 sal_uInt32 nTblInd = pHandler->getMeasureValue();
352 pTblIndMap->setValue( TablePropertyMap::LEFT_MARGIN, nTblInd);
353 insertTableProps(pTblIndMap);
356 break;
357 case NS_ooxml::LN_CT_TcPrBase_hideMark:
358 if (nIntValue)
360 TablePropertyMapPtr pPropMap(new TablePropertyMap());
361 pPropMap->Insert(PROP_CELL_HIDE_MARK, uno::Any(nIntValue));
362 cellProps(pPropMap);
364 break;
365 default:
366 // Not handled here, give the next handler a chance.
367 bRet = false;
368 // However, these logically belong here, so save the value if necessary.
369 switch (nSprmId)
371 case NS_ooxml::LN_CT_TblPrBase_tblStyleRowBandSize:
372 case NS_ooxml::LN_CT_TblPrBase_tblStyleColBandSize:
373 if (m_pCurrentInteropGrabBag)
375 beans::PropertyValue aValue;
376 aValue.Name = (nSprmId == NS_ooxml::LN_CT_TblPrBase_tblStyleRowBandSize ? std::u16string_view(u"tblStyleRowBandSize") : std::u16string_view(u"tblStyleColBandSize"));
377 aValue.Value <<= nIntValue;
378 m_pCurrentInteropGrabBag->push_back(aValue);
380 break;
382 break;
385 #ifdef DBG_UTIL
386 TagLogger::getInstance().endElement();
387 #endif
389 return bRet;
392 void TablePropertiesHandler::SetInteropGrabBag(std::vector<beans::PropertyValue>& rValue)
394 m_pCurrentInteropGrabBag = &rValue;
398 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */