LibreOffice Module writerfilter (master) 1
TablePropertiesHandler.cxx
Go to the documentation of this file.
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 <sal/config.h>
21
22#include <string_view>
23
24#include "BorderHandler.hxx"
25#include "CellColorHandler.hxx"
26#include "CellMarginHandler.hxx"
27#include "ConversionHelper.hxx"
28#include "MeasureHandler.hxx"
31#include "TagLogger.hxx"
32#include "TDefTableHandler.hxx"
34
35#include <ooxml/resourceids.hxx>
36
37#include <com/sun/star/text/VertOrientation.hpp>
38#include <oox/token/tokens.hxx>
39
40using namespace com::sun::star;
41using namespace oox;
42
43namespace writerfilter::dmapper {
44
46 m_pCurrentInteropGrabBag(nullptr),
47 m_pTableManager( nullptr )
48 {
49 }
50
52 {
53#ifdef DBG_UTIL
54 TagLogger::getInstance().startElement("TablePropertiesHandler.sprm");
55 TagLogger::getInstance().attribute("sprm", rSprm.toString());
56#endif
57
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 )
63 {
64 case NS_ooxml::LN_CT_TrPrBase_jc:
65 case NS_ooxml::LN_CT_TblPrBase_jc:
66 {
67 sal_Int16 nOrient = ConversionHelper::convertTableJustification( nIntValue );
69 pTableMap->setValue( TablePropertyMap::HORI_ORIENT, nOrient );
70 insertTableProps( pTableMap );
71 }
72 break;
73 case NS_ooxml::LN_CT_TrPrBase_trHeight:
74 {
75 //contains unit and value
77 if( pProperties )
78 { //contains attributes x2902 (LN_unit) and x17e2 (LN_trleft)
79 MeasureHandlerPtr pMeasureHandler( new MeasureHandler );
80 pProperties->resolve(*pMeasureHandler);
82
83 pPropMap->Insert( PROP_SIZE_TYPE, uno::Any( pMeasureHandler->GetRowHeightSizeType() ), false);
84 pPropMap->Insert( PROP_HEIGHT, uno::Any(pMeasureHandler->getMeasureValue() ));
85
86 insertRowProps(pPropMap);
87 }
88 }
89 break;
90 case NS_ooxml::LN_CT_TrPr_ins:
91 case NS_ooxml::LN_CT_TrPr_del:
92 {
94 if( pProperties )
95 {
96 sal_Int32 nToken = sal_Int32();
97 switch( nSprmId )
98 {
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;
105 }
106 auto pTrackChangesHandler = std::make_shared<TrackChangesHandler>( nToken );
107 pProperties->resolve(*pTrackChangesHandler);
109
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);
115 }
116 }
117 break;
118 case NS_ooxml::LN_CT_TcPrBase_cellIns:
119 case NS_ooxml::LN_CT_TcPrBase_cellDel:
120 {
122 if( pProperties )
123 {
124 sal_Int32 nToken;
125 switch( nSprmId )
126 {
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;
136 }
137 auto pTrackChangesHandler = std::make_shared<TrackChangesHandler>( nToken );
138 pProperties->resolve(*pTrackChangesHandler);
140
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);
146 }
147 }
148 break;
149 case NS_ooxml::LN_CT_TrPrBase_cantSplit:
150 {
151 //row can't break across pages if nIntValue == 1
153 pPropMap->Insert( PROP_IS_SPLIT_ALLOWED, uno::Any( nIntValue != 1 ) );
154 insertRowProps(pPropMap);
155 }
156 break;
157 case NS_ooxml::LN_CT_TcPrBase_vAlign:
158 {
159 sal_Int16 nVertOrient = text::VertOrientation::NONE;
160 switch( nIntValue )
161 {
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:;
165 }
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 );
171 {
172 OUString aVertOrient;
173 switch( nIntValue )
174 {
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;
179 }
180 if (!aVertOrient.isEmpty())
181 {
182 beans::PropertyValue aValue;
183 aValue.Name = "vAlign";
184 aValue.Value <<= aVertOrient;
185 m_pCurrentInteropGrabBag->push_back(aValue);
186 }
187 }
188 }
189 break;
190 case NS_ooxml::LN_CT_TblPrBase_tblBorders: //table borders, might be defined in table style
191 {
193 if( pProperties )
194 {
195 auto pBorderHandler = std::make_shared<BorderHandler>(true);
197 pBorderHandler->enableInteropGrabBag("tblBorders");
198 pProperties->resolve(*pBorderHandler);
200 m_pCurrentInteropGrabBag->push_back(pBorderHandler->getInteropGrabBag());
201 TablePropertyMapPtr pTablePropMap( new TablePropertyMap );
202 pTablePropMap->InsertProps(pBorderHandler->getProperties());
203
204#ifdef DBG_UTIL
205 pTablePropMap->dumpXml();
206#endif
207 insertTableProps( pTablePropMap );
208 }
209 }
210 break;
211 case NS_ooxml::LN_CT_TblPrBase_tblLayout:
212 {
214 if (pManager)
215 pManager->SetLayoutType(static_cast<sal_uInt32>(nIntValue));
216 }
217 break;
218 case NS_ooxml::LN_CT_TblPrEx_tblBorders:
219 {
221 if( pProperties)
222 {
223 auto pBorderHandler = std::make_shared<BorderHandler>(true);
224 pProperties->resolve(*pBorderHandler);
225 TablePropertyMapPtr pTablePropMap( new TablePropertyMap );
226 pTablePropMap->InsertProps(pBorderHandler->getProperties());
227
228#ifdef DBG_UTIL
229 pTablePropMap->dumpXml();
230#endif
231 tableExceptionProps( pTablePropMap );
232 }
233 }
234 break;
235 case NS_ooxml::LN_CT_TcPrBase_tcBorders ://cell borders
236 //contains CT_TcBorders_left, right, top, bottom
237 {
239 if( pProperties )
240 {
241 //in OOXML there's one set of borders at each cell (if there is any)
242 tools::SvRef< TDefTableHandler > pTDefTableHandler( new TDefTableHandler());
244 pTDefTableHandler->enableInteropGrabBag("tcBorders");
245 pProperties->resolve( *pTDefTableHandler );
247 m_pCurrentInteropGrabBag->push_back(pTDefTableHandler->getInteropGrabBag());
248 TablePropertyMapPtr pCellPropMap( new TablePropertyMap );
249 pTDefTableHandler->fillCellProperties( pCellPropMap );
250 cellProps( pCellPropMap );
251 }
252 }
253 break;
254 case NS_ooxml::LN_CT_TcPrBase_tcMar:
255
256 {
258 if (pProperties)
259 {
260 auto pCellMarginHandler = std::make_shared<CellMarginHandler>();
262 pCellMarginHandler->enableInteropGrabBag("tcMar");
263 pProperties->resolve(*pCellMarginHandler);
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);
276 }
277 }
278 break;
279/* // tdf#123189 skip to keep MSO interoperability
280 case NS_ooxml::LN_CT_TblPrBase_shd:
281 {
282 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
283 if( pProperties.get())
284 {
285 std::shared_ptr<CellColorHandler> pCellColorHandler( new CellColorHandler);
286 pProperties->resolve( *pCellColorHandler );
287 TablePropertyMapPtr pTablePropMap( new TablePropertyMap );
288 insertTableProps( pCellColorHandler->getProperties() );
289 }
290 }
291*/
292 break;
293 case NS_ooxml::LN_CT_TcPrBase_shd:
294 {
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
298 if( pProperties )
299 {
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();
306 m_pCurrentInteropGrabBag->push_back(aGrabBag);
307 pPropertyMap->Insert( PROP_CELL_INTEROP_GRAB_BAG, aGrabBag.Value );
308 cellProps( pPropertyMap );
309 }
310 }
311 break;
312//OOXML table properties
313 case NS_ooxml::LN_CT_TblPrBase_tblCellMar: //cell margins
314 {
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
318 if( pProperties )
319 {
320 auto pCellMarginHandler = std::make_shared<CellMarginHandler>();
322 pCellMarginHandler->enableInteropGrabBag("tblCellMar");
323 pProperties->resolve( *pCellMarginHandler );
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);
336 }
337 }
338 break;
339 case NS_ooxml::LN_CT_TblPrBase_tblInd:
340 {
342 if (pProperties)
343 {
344 MeasureHandlerPtr pHandler(new MeasureHandler);
346 pHandler->enableInteropGrabBag("tblInd");
347 pProperties->resolve( *pHandler );
349 m_pCurrentInteropGrabBag->push_back(pHandler->getInteropGrabBag());
351 sal_uInt32 nTblInd = pHandler->getMeasureValue();
352 pTblIndMap->setValue( TablePropertyMap::LEFT_MARGIN, nTblInd);
353 insertTableProps(pTblIndMap);
354 }
355 }
356 break;
357 case NS_ooxml::LN_CT_TcPrBase_hideMark:
358 if (nIntValue)
359 {
361 pPropMap->Insert(PROP_CELL_HIDE_MARK, uno::Any(nIntValue));
362 cellProps(pPropMap);
363 }
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)
370 {
371 case NS_ooxml::LN_CT_TblPrBase_tblStyleRowBandSize:
372 case NS_ooxml::LN_CT_TblPrBase_tblStyleColBandSize:
374 {
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);
379 }
380 break;
381 }
382 break;
383 }
384
385#ifdef DBG_UTIL
387#endif
388
389 return bRet;
390 }
391
392 void TablePropertiesHandler::SetInteropGrabBag(std::vector<beans::PropertyValue>& rValue)
393 {
394 m_pCurrentInteropGrabBag = &rValue;
395 }
396}
397
398/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
An SPRM: Section, Paragraph and Run Modifier.
virtual sal_uInt32 getId() const =0
Returns id of the SPRM.
virtual writerfilter::Reference< Properties >::Pointer_t getProps()=0
Returns reference to properties contained in the SPRM.
virtual Value::Pointer_t getValue()=0
Returns value of the SPRM.
virtual std::string toString() const =0
Returns string representation of sprm.
static TagLogger & getInstance()
Definition: TagLogger.cxx:95
void startElement(const std::string &name)
Definition: TagLogger.cxx:140
void attribute(const std::string &name, const std::string &value)
Definition: TagLogger.cxx:150
Handler for sprms that contain a measure and a unit.
void tableExceptionProps(TablePropertyMapPtr pProps)
std::vector< css::beans::PropertyValue > * m_pCurrentInteropGrabBag
void SetInteropGrabBag(std::vector< css::beans::PropertyValue > &rValue)
float u
sal_Int16 convertTableJustification(sal_Int32 nIntValue)
DefTokenId nToken