1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #include "BorderHandler.hxx"
23 #include "CellColorHandler.hxx"
24 #include "CellMarginHandler.hxx"
25 #include "ConversionHelper.hxx"
26 #include "MeasureHandler.hxx"
27 #include "TablePropertiesHandler.hxx"
28 #include "TDefTableHandler.hxx"
29 
30 #include <ooxml/resourceids.hxx>
31 #include <doctok/sprmids.hxx>
32 
33 #include <com/sun/star/text/SizeType.hpp>
34 #include <com/sun/star/text/VertOrientation.hpp>
35 #include <dmapperLoggers.hxx>
36 
37 
38 namespace writerfilter {
39 namespace dmapper {
40 
TablePropertiesHandler(bool bOOXML)41     TablePropertiesHandler::TablePropertiesHandler( bool bOOXML ) :
42         m_pTableManager( NULL ),
43         m_bOOXML( bOOXML )
44     {
45     }
46 
47 
~TablePropertiesHandler()48     TablePropertiesHandler::~TablePropertiesHandler( )
49     {
50         // Do not delete the table manager... this will be done somewhere else
51         m_pTableManager = NULL;
52     }
53 
sprm(Sprm & rSprm)54     bool TablePropertiesHandler::sprm(Sprm & rSprm)
55     {
56 #ifdef DEBUG_DMAPPER_TABLE_PROPERTIES_HANDLER
57         dmapper_logger->startElement("TablePropertiesHandler.sprm");
58         dmapper_logger->attribute("sprm", rSprm.toString());
59 #endif
60 
61         bool bRet = true;
62         sal_uInt32 nSprmId = rSprm.getId();
63         Value::Pointer_t pValue = rSprm.getValue();
64         sal_Int32 nIntValue = ((pValue.get() != NULL) ? pValue->getInt() : 0);
65         /* WRITERFILTERSTATUS: table: table_sprmdata */
66         switch( nSprmId )
67        {
68             /* WRITERFILTERSTATUS: done: 1, planned: 2, spent: 2 */
69             case NS_ooxml::LN_CT_TrPrBase_jc: //90706
70             /* WRITERFILTERSTATUS: done: 1, planned: 0.5, spent: 0.5 */
71             case NS_ooxml::LN_CT_TblPrBase_jc:
72             /* WRITERFILTERSTATUS: done: 1, planned: 2, spent: 2 */
73             case 0x5400: // sprmTJc
74             {
75                 //table justification 0: left, 1: center, 2: right
76                 sal_Int16 nOrient = ConversionHelper::convertTableJustification( nIntValue );
77                 TablePropertyMapPtr pTableMap( new TablePropertyMap );
78                 pTableMap->setValue( TablePropertyMap::HORI_ORIENT, nOrient );
79                 insertTableProps( pTableMap );
80             }
81             break;
82             /* WRITERFILTERSTATUS: done: 0, planned: 2, spent: 0 */
83             case 0x9601: // sprmTDxaLeft
84             break;
85             /* WRITERFILTERSTATUS: done: 1, planned: 2, spent: 0 */
86             case 0x9602: // sprmTDxaGapHalf
87             {
88                 //m_nGapHalf = ConversionHelper::convertTwipToMM100( nIntValue );
89                 TablePropertyMapPtr pPropMap( new TablePropertyMap );
90                 pPropMap->setValue( TablePropertyMap::GAP_HALF, ConversionHelper::convertTwipToMM100( nIntValue ) );
91                 insertTableProps(pPropMap);
92             }
93             break;
94             /* WRITERFILTERSTATUS: done: 1, planned: 2, spent: 2 */
95             case NS_ooxml::LN_CT_TrPrBase_trHeight: //90703
96             {
97                 //contains unit and value
98                 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
99                 if( pProperties.get())
100                 {   //contains attributes x2902 (LN_unit) and x17e2 (LN_trleft)
101                     MeasureHandlerPtr pMeasureHandler( new MeasureHandler );
102                     pProperties->resolve(*pMeasureHandler);
103                     TablePropertyMapPtr pPropMap( new TablePropertyMap );
104                     pPropMap->Insert( PROP_SIZE_TYPE, false, uno::makeAny( pMeasureHandler->GetRowHeightSizeType() ));
105                     pPropMap->Insert( PROP_HEIGHT, false, uno::makeAny(pMeasureHandler->getMeasureValue() ));
106                     insertRowProps(pPropMap);
107                 }
108             }
109             break;
110             /* WRITERFILTERSTATUS: done: 1, planned: 2, spent: 0 */
111             case 0x3403: // sprmTFCantSplit
112             case NS_sprm::LN_TCantSplit: // 0x3644
113             {
114                 //row can't break across pages if nIntValue == 1
115                 TablePropertyMapPtr pPropMap( new TablePropertyMap );
116                 pPropMap->Insert( PROP_IS_SPLIT_ALLOWED, false, uno::makeAny(sal_Bool( nIntValue == 1 ? sal_False : sal_True ) ));
117                 insertRowProps(pPropMap);
118             }
119             break;
120             /* WRITERFILTERSTATUS: done: 1, planned: 2, spent: 0 */
121             case 0x9407: // sprmTDyaRowHeight
122             {
123                 // table row height - negative values indicate 'exact height' - positive 'at least'
124                 TablePropertyMapPtr pPropMap( new TablePropertyMap );
125                 bool bMinHeight = true;
126                 sal_Int16 nHeight = static_cast<sal_Int16>( nIntValue );
127                 if( nHeight < 0 )
128                 {
129                     bMinHeight = false;
130                     nHeight *= -1;
131                 }
132                 pPropMap->Insert( PROP_SIZE_TYPE, false, uno::makeAny(bMinHeight ? text::SizeType::MIN : text::SizeType::FIX ));
133                 pPropMap->Insert( PROP_HEIGHT, false, uno::makeAny(ConversionHelper::convertTwipToMM100( nHeight )));
134                 insertRowProps(pPropMap);
135             }
136             break;
137             /* WRITERFILTERSTATUS: done: 1, planned: 2, spent: 0 */
138             case NS_ooxml::LN_CT_TcPrBase_vAlign://90694
139             {
140                 sal_Int16 nVertOrient = text::VertOrientation::NONE;
141                 switch( nIntValue ) //0 - top 1 - center 3 - bottom
142                 {
143                     case 1: nVertOrient = text::VertOrientation::CENTER; break;
144                     case 3: nVertOrient = text::VertOrientation::BOTTOM; break;
145                     default:;
146                 };
147                 TablePropertyMapPtr pCellPropMap( new TablePropertyMap() );
148                 pCellPropMap->Insert( PROP_VERT_ORIENT, false, uno::makeAny( nVertOrient ) );
149                 //todo: in ooxml import the value of m_ncell is wrong
150                 cellProps( pCellPropMap );
151             }
152             break;
153             /* WRITERFILTERSTATUS: done: 1, planned: 2, spent: 0 */
154             case NS_ooxml::LN_CT_TblPrBase_tblBorders: //table borders, might be defined in table style
155             {
156                 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
157                 if( pProperties.get())
158                 {
159                     BorderHandlerPtr pBorderHandler( new BorderHandler(m_bOOXML) );
160                     pProperties->resolve(*pBorderHandler);
161                     TablePropertyMapPtr pTablePropMap( new TablePropertyMap );
162                     pTablePropMap->insert( pBorderHandler->getProperties() );
163 
164 #ifdef DEBUG_DMAPPER_TABLE_PROPERTIES_HANDLER
165                     dmapper_logger->addTag(pTablePropMap->toTag());
166 #endif
167                     insertTableProps( pTablePropMap );
168                 }
169             }
170             break;
171             /* WRITERFILTERSTATUS: done: 1, planned: 2, spent: 0 */
172             case NS_ooxml::LN_CT_TcPrBase_tcBorders ://cell borders
173             //contains CT_TcBorders_left, right, top, bottom
174             {
175                 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
176                 if( pProperties.get())
177                 {
178                     //in OOXML there's one set of borders at each cell (if there is any)
179                     TDefTableHandlerPtr pTDefTableHandler( new TDefTableHandler( m_bOOXML ));
180                     pProperties->resolve( *pTDefTableHandler );
181                     TablePropertyMapPtr pCellPropMap( new TablePropertyMap );
182                     pTDefTableHandler->fillCellProperties( 0, pCellPropMap );
183                     cellProps( pCellPropMap );
184                 }
185             }
186             break;
187             case NS_ooxml::LN_CT_TblPrBase_shd:
188             {
189                 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
190                 if( pProperties.get())
191                 {
192                     CellColorHandlerPtr pCellColorHandler( new CellColorHandler);
193                     pProperties->resolve( *pCellColorHandler );
194                     TablePropertyMapPtr pTablePropMap( new TablePropertyMap );
195                     insertTableProps( pCellColorHandler->getProperties() );
196                 }
197             }
198             break;
199             /* WRITERFILTERSTATUS: done: 1, planned: 2, spent: 0 */
200             case 0xd61a : // sprmTCellTopColor
201             /* WRITERFILTERSTATUS: done: 1, planned: 2, spent: 0 */
202             case 0xd61b : // sprmTCellLeftColor
203             /* WRITERFILTERSTATUS: done: 1, planned: 2, spent: 0 */
204             case 0xd61c : // sprmTCellBottomColor
205             /* WRITERFILTERSTATUS: done: 1, planned: 2, spent: 0 */
206             case 0xd61d : // sprmTCellRightColor
207             /* WRITERFILTERSTATUS: done: 1, planned: 2, spent: 0 */
208             case NS_ooxml::LN_CT_TcPrBase_shd:
209             {
210                 // each color sprm contains as much colors as cells are in a row
211                 //LN_CT_TcPrBase_shd: cell shading contains: LN_CT_Shd_val, LN_CT_Shd_fill, LN_CT_Shd_color
212                 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
213                 if( pProperties.get())
214                 {
215                     CellColorHandlerPtr pCellColorHandler( new CellColorHandler );
216                     pProperties->resolve( *pCellColorHandler );
217                     cellProps( pCellColorHandler->getProperties());
218                 }
219             }
220             break;
221 //OOXML table properties
222             /* WRITERFILTERSTATUS: done: 0, planned: 2, spent: 0 */
223             case NS_ooxml::LN_CT_TblPrBase_tblCellMar: //cell margins
224             {
225                 //contains LN_CT_TblCellMar_top, LN_CT_TblCellMar_left, LN_CT_TblCellMar_bottom, LN_CT_TblCellMar_right
226                 writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
227                 if( pProperties.get())
228                 {
229                     CellMarginHandlerPtr pCellMarginHandler( new CellMarginHandler );
230                     pProperties->resolve( *pCellMarginHandler );
231                     TablePropertyMapPtr pMarginProps( new TablePropertyMap );
232                     if( pCellMarginHandler->m_bTopMarginValid )
233                         pMarginProps->setValue( TablePropertyMap::CELL_MAR_TOP, pCellMarginHandler->m_nTopMargin );
234                     if( pCellMarginHandler->m_bBottomMarginValid )
235                         pMarginProps->setValue( TablePropertyMap::CELL_MAR_BOTTOM, pCellMarginHandler->m_nBottomMargin );
236                     if( pCellMarginHandler->m_bLeftMarginValid )
237                         pMarginProps->setValue( TablePropertyMap::CELL_MAR_LEFT, pCellMarginHandler->m_nLeftMargin );
238                     if( pCellMarginHandler->m_bRightMarginValid )
239                         pMarginProps->setValue( TablePropertyMap::CELL_MAR_RIGHT, pCellMarginHandler->m_nRightMargin );
240                     insertTableProps(pMarginProps);
241                 }
242             }
243             break;
244            case NS_ooxml::LN_CT_TblPrBase_tblInd:
245            {
246                writerfilter::Reference<Properties>::Pointer_t pProperties = rSprm.getProps();
247                if (pProperties.get())
248                {
249                    MeasureHandlerPtr pHandler(new MeasureHandler);
250                    pProperties->resolve(*pHandler);
251                    TablePropertyMapPtr pTblIndMap(new TablePropertyMap);
252                    sal_uInt32 nTblInd = pHandler->getMeasureValue();
253                    pTblIndMap->setValue( TablePropertyMap::LEFT_MARGIN, nTblInd);
254                    insertTableProps(pTblIndMap);
255                }
256            }
257             break;
258             default: bRet = false;
259         }
260 
261 #ifdef DEBUG_DMAPPER_TABLE_PROPERTIES_HANDLER
262         dmapper_logger->endElement("TablePropertiesHandler.sprm");
263 #endif
264 
265         return bRet;
266     }
267 }}
268