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 
23 
24 #include "oox/drawingml/table/tablecell.hxx"
25 #include "oox/drawingml/table/tableproperties.hxx"
26 #include "oox/drawingml/shapepropertymap.hxx"
27 #include "oox/drawingml/textbody.hxx"
28 #include "oox/core/xmlfilterbase.hxx"
29 #include "oox/helper/propertyset.hxx"
30 #include <com/sun/star/container/XNameContainer.hpp>
31 #include <com/sun/star/beans/XMultiPropertySet.hpp>
32 #include <com/sun/star/table/XTable.hpp>
33 #include <com/sun/star/table/XMergeableCellRange.hpp>
34 #include <com/sun/star/table/BorderLine.hpp>
35 #include <com/sun/star/drawing/LineStyle.hpp>
36 #include <com/sun/star/drawing/TextVerticalAdjust.hpp>
37 #include <com/sun/star/drawing/TextHorizontalAdjust.hpp>
38 #include <com/sun/star/text/XText.hpp>
39 #include <com/sun/star/text/WritingMode.hpp>
40 
41 using rtl::OUString;
42 using namespace ::oox::core;
43 using namespace ::com::sun::star;
44 using namespace ::com::sun::star::uno;
45 using namespace ::com::sun::star::beans;
46 using ::com::sun::star::table::BorderLine;
47 using ::com::sun::star::drawing::LineStyle;
48 
49 namespace oox { namespace drawingml { namespace table {
50 
51 TableCell::TableCell()
52 : mnRowSpan ( 1 )
53 , mnGridSpan( 1 )
54 , mbhMerge( sal_False )
55 , mbvMerge( sal_False )
56 , mnMarL( 91440 )
57 , mnMarR( 91440 )
58 , mnMarT( 45720 )
59 , mnMarB( 45720 )
60 , mnVertToken( XML_horz )
61 , mnAnchorToken( XML_t )
62 , mbAnchorCtr( sal_False )
63 , mnHorzOverflowToken( XML_clip )
64 {
65 }
66 TableCell::~TableCell()
67 {
68 }
69 
70 void applyLineAttributes( const ::oox::core::XmlFilterBase& rFilterBase,
71         Reference< XPropertySet >& rxPropSet, oox::drawingml::LineProperties& rLineProperties,
72         sal_Int32 nPropId )
73 {
74     BorderLine aBorderLine( 0, 0, 0, 0 );
75     if( rLineProperties.maLineFill.moFillType.differsFrom( XML_noFill ) )
76     {
77         Color aColor = rLineProperties.maLineFill.getBestSolidColor();
78         aBorderLine.Color = aColor.getColor( rFilterBase.getGraphicHelper() );
79         aBorderLine.OuterLineWidth = static_cast< sal_Int16 >( GetCoordinate( rLineProperties.moLineWidth.get( 0 ) ) / 4 );
80         aBorderLine.InnerLineWidth = static_cast< sal_Int16 >( GetCoordinate( rLineProperties.moLineWidth.get( 0 ) ) / 4 );
81         aBorderLine.LineDistance = 0;
82     }
83 
84     PropertySet aPropSet( rxPropSet );
85     aPropSet.setProperty( nPropId, aBorderLine );
86 }
87 
88 void applyBorder( TableStylePart& rTableStylePart, sal_Int32 nLineType, oox::drawingml::LineProperties& rLineProperties )
89 {
90 	std::map < sal_Int32, ::oox::drawingml::LinePropertiesPtr >& rPartLineBorders( rTableStylePart.getLineBorders() );
91 	std::map < sal_Int32, ::oox::drawingml::LinePropertiesPtr >::const_iterator aIter( rPartLineBorders.find( nLineType ) );
92 	if ( ( aIter != rPartLineBorders.end() ) && aIter->second.get() )
93 		rLineProperties.assignUsed( *aIter->second );
94 }
95 
96 void applyTableStylePart( const ::oox::core::XmlFilterBase& rFilterBase, const Reference < ::com::sun::star::table::XCell >& rxCell, oox::drawingml::FillProperties& rFillProperties,
97 	 oox::drawingml::LineProperties& rLeftBorder,
98 	 oox::drawingml::LineProperties& rRightBorder,
99 	 oox::drawingml::LineProperties& rTopBorder,
100 	 oox::drawingml::LineProperties& rBottomBorder,
101 	 oox::drawingml::LineProperties& rTopLeftToBottomRightBorder,
102 	 oox::drawingml::LineProperties& rBottomLeftToTopRightBorder,
103 	TableStylePart& rTableStylePart )
104 {
105 	boost::shared_ptr< ::oox::drawingml::FillProperties >& rPartFillPropertiesPtr( rTableStylePart.getFillProperties() );
106 	if ( rPartFillPropertiesPtr.get() )
107 		rFillProperties.assignUsed( *rPartFillPropertiesPtr );
108 
109 	applyBorder( rTableStylePart, XML_left, rLeftBorder );
110 	applyBorder( rTableStylePart, XML_right, rRightBorder );
111 	applyBorder( rTableStylePart, XML_top, rTopBorder );
112 	applyBorder( rTableStylePart, XML_bottom, rBottomBorder );
113 	applyBorder( rTableStylePart, XML_tl2br, rTopLeftToBottomRightBorder );
114 	applyBorder( rTableStylePart, XML_tr2bl, rBottomLeftToTopRightBorder );
115 
116     TextCharacterProperties aTextCharProps;
117     aTextCharProps.maLatinFont = rTableStylePart.getLatinFont();
118     aTextCharProps.maAsianFont = rTableStylePart.getAsianFont();
119     aTextCharProps.maComplexFont = rTableStylePart.getComplexFont();
120     aTextCharProps.maSymbolFont = rTableStylePart.getSymbolFont();
121     aTextCharProps.maCharColor = rTableStylePart.getTextColor();
122 
123     PropertySet aPropSet( rxCell );
124     aTextCharProps.pushToPropSet( aPropSet, rFilterBase );
125 }
126 
127 void applyTableCellProperties( const Reference < ::com::sun::star::table::XCell >& rxCell, const TableCell& rTableCell )
128 {
129 	static const rtl::OUString	sTopBorder( RTL_CONSTASCII_USTRINGPARAM( "TextUpperDistance" ) );
130 	static const rtl::OUString	sBottomBorder( RTL_CONSTASCII_USTRINGPARAM( "TextLowerDistance" ) );
131 	static const rtl::OUString	sLeftBorder( RTL_CONSTASCII_USTRINGPARAM( "TextLeftDistance" ) );
132 	static const rtl::OUString	sRightBorder( RTL_CONSTASCII_USTRINGPARAM( "TextRightDistance" ) );
133 	static const rtl::OUString	sVerticalAdjust( RTL_CONSTASCII_USTRINGPARAM( "TextVerticalAdjust" ) );
134 
135 	Reference< XPropertySet > xPropSet( rxCell, UNO_QUERY_THROW );
136 	xPropSet->setPropertyValue( sTopBorder, Any( static_cast< sal_Int32 >( rTableCell.getTopMargin() / 360 ) ) );
137 	xPropSet->setPropertyValue( sRightBorder, Any( static_cast< sal_Int32 >( rTableCell.getRightMargin() / 360 ) ) );
138 	xPropSet->setPropertyValue( sLeftBorder, Any( static_cast< sal_Int32 >( rTableCell.getLeftMargin() / 360 ) ) );
139 	xPropSet->setPropertyValue( sBottomBorder, Any( static_cast< sal_Int32 >( rTableCell.getBottomMargin() / 360 ) ) );
140 
141 	drawing::TextVerticalAdjust eVA;
142 	switch( rTableCell.getAnchorToken() )
143 	{
144 		case XML_ctr:	eVA = drawing::TextVerticalAdjust_CENTER; break;
145 		case XML_b:		eVA = drawing::TextVerticalAdjust_BOTTOM; break;
146 		case XML_just:
147 		case XML_dist:
148 		default:
149 		case XML_t:		eVA = drawing::TextVerticalAdjust_TOP; break;
150 	}
151 	xPropSet->setPropertyValue( sVerticalAdjust, Any( eVA ) );
152 }
153 
154 void TableCell::pushToXCell( const ::oox::core::XmlFilterBase& rFilterBase, ::oox::drawingml::TextListStylePtr pMasterTextListStyle,
155 	const ::com::sun::star::uno::Reference < ::com::sun::star::table::XCell >& rxCell, const TableProperties& rTableProperties,
156 		const TableStyle& rTableStyle, sal_Int32 nColumn, sal_Int32 nMaxColumn, sal_Int32 nRow, sal_Int32 nMaxRow )
157 {
158 	TableStyle& rTable( const_cast< TableStyle& >( rTableStyle ) );
159 	TableProperties& rProperties( const_cast< TableProperties& >( rTableProperties ) );
160 
161 	Reference< text::XText > xText( rxCell, UNO_QUERY_THROW );
162 	Reference< text::XTextCursor > xAt = xText->createTextCursor();
163 
164 	applyTableCellProperties( rxCell, *this );
165     TextCharacterProperties aTextStyleProps;
166 	getTextBody()->insertAt( rFilterBase, xText, xAt, aTextStyleProps, pMasterTextListStyle );
167 
168 	Reference< XPropertySet > xPropSet( rxCell, UNO_QUERY_THROW );
169 	oox::drawingml::FillProperties aFillProperties;
170 	oox::drawingml::LineProperties aLinePropertiesLeft;
171 	oox::drawingml::LineProperties aLinePropertiesRight;
172 	oox::drawingml::LineProperties aLinePropertiesTop;
173 	oox::drawingml::LineProperties aLinePropertiesBottom;
174 	oox::drawingml::LineProperties aLinePropertiesTopLeftToBottomRight;
175 	oox::drawingml::LineProperties aLinePropertiesBottomLeftToTopRight;
176 
177 	boost::shared_ptr< ::oox::drawingml::FillProperties >& rBackgroundFillPropertiesPtr( rTable.getBackgroundFillProperties() );
178 	if ( rBackgroundFillPropertiesPtr.get() )
179 		aFillProperties.assignUsed( *rBackgroundFillPropertiesPtr );
180 
181 	applyTableStylePart( rFilterBase, rxCell, aFillProperties,
182 		aLinePropertiesLeft,
183 		aLinePropertiesRight,
184 		aLinePropertiesTop,
185 		aLinePropertiesBottom,
186 		aLinePropertiesTopLeftToBottomRight,
187 		aLinePropertiesBottomLeftToTopRight,
188 		rTable.getWholeTbl() );
189 
190 	if ( rProperties.isFirstRow() && ( nRow == 0 ) )
191 	{
192 		applyTableStylePart( rFilterBase, rxCell, aFillProperties,
193 			aLinePropertiesLeft,
194 			aLinePropertiesRight,
195 			aLinePropertiesTop,
196 			aLinePropertiesBottom,
197 			aLinePropertiesTopLeftToBottomRight,
198 			aLinePropertiesBottomLeftToTopRight,
199 			rTable.getFirstRow() );
200 	}
201 	if ( rProperties.isLastRow() && ( nRow == nMaxRow ) )
202 	{
203 		applyTableStylePart( rFilterBase, rxCell, aFillProperties,
204 			aLinePropertiesLeft,
205 			aLinePropertiesRight,
206 			aLinePropertiesTop,
207 			aLinePropertiesBottom,
208 			aLinePropertiesTopLeftToBottomRight,
209 			aLinePropertiesBottomLeftToTopRight,
210 			rTable.getLastRow() );
211 	}
212 	if ( rProperties.isFirstCol() && ( nColumn == 0 ) )
213 	{
214 		applyTableStylePart( rFilterBase, rxCell, aFillProperties,
215 			aLinePropertiesLeft,
216 			aLinePropertiesRight,
217 			aLinePropertiesTop,
218 			aLinePropertiesBottom,
219 			aLinePropertiesTopLeftToBottomRight,
220 			aLinePropertiesBottomLeftToTopRight,
221 			rTable.getFirstCol() );
222 	}
223 	if ( rProperties.isLastCol() && ( nColumn == nMaxColumn ) )
224 	{
225 		applyTableStylePart( rFilterBase, rxCell, aFillProperties,
226 			aLinePropertiesLeft,
227 			aLinePropertiesRight,
228 			aLinePropertiesTop,
229 			aLinePropertiesBottom,
230 			aLinePropertiesTopLeftToBottomRight,
231 			aLinePropertiesBottomLeftToTopRight,
232 			rTable.getLastCol() );
233 	}
234 	if ( rProperties.isBandRow() )
235 	{
236 		if ( ( !rProperties.isFirstRow() || ( nRow != 0 ) ) &&
237 			( !rProperties.isLastRow() || ( nRow != nMaxRow ) ) )
238 		{
239 			sal_Int32 nBand = nRow;
240 			if ( rProperties.isFirstRow() )
241 				nBand++;
242 			if ( nBand & 1 )
243 			{
244 				applyTableStylePart( rFilterBase, rxCell, aFillProperties,
245 					aLinePropertiesLeft,
246 					aLinePropertiesRight,
247 					aLinePropertiesTop,
248 					aLinePropertiesBottom,
249 					aLinePropertiesTopLeftToBottomRight,
250 					aLinePropertiesBottomLeftToTopRight,
251 					rTable.getBand2H() );
252 			}
253 			else
254 			{
255 				applyTableStylePart( rFilterBase, rxCell, aFillProperties,
256 					aLinePropertiesLeft,
257 					aLinePropertiesRight,
258 					aLinePropertiesTop,
259 					aLinePropertiesBottom,
260 					aLinePropertiesTopLeftToBottomRight,
261 					aLinePropertiesBottomLeftToTopRight,
262 					rTable.getBand1H() );
263 			}
264 		}
265 	}
266 	if ( ( nRow == 0 ) && ( nColumn == 0 ) )
267 	{
268 		applyTableStylePart( rFilterBase, rxCell, aFillProperties,
269 			aLinePropertiesLeft,
270 			aLinePropertiesRight,
271 			aLinePropertiesTop,
272 			aLinePropertiesBottom,
273 			aLinePropertiesTopLeftToBottomRight,
274 			aLinePropertiesBottomLeftToTopRight,
275 			rTable.getNwCell() );
276 	}
277 	if ( ( nRow == nMaxRow ) && ( nColumn == 0 ) )
278 	{
279 		applyTableStylePart( rFilterBase, rxCell, aFillProperties,
280 			aLinePropertiesLeft,
281 			aLinePropertiesRight,
282 			aLinePropertiesTop,
283 			aLinePropertiesBottom,
284 			aLinePropertiesTopLeftToBottomRight,
285 			aLinePropertiesBottomLeftToTopRight,
286 			rTable.getSwCell() );
287 	}
288 	if ( ( nRow == 0 ) && ( nColumn == nMaxColumn ) )
289 	{
290 		applyTableStylePart( rFilterBase, rxCell, aFillProperties,
291 			aLinePropertiesLeft,
292 			aLinePropertiesRight,
293 			aLinePropertiesTop,
294 			aLinePropertiesBottom,
295 			aLinePropertiesTopLeftToBottomRight,
296 			aLinePropertiesBottomLeftToTopRight,
297 			rTable.getNeCell() );
298 	}
299 	if ( ( nRow == nMaxColumn ) && ( nColumn == nMaxColumn ) )
300 	{
301 		applyTableStylePart( rFilterBase, rxCell, aFillProperties,
302 			aLinePropertiesLeft,
303 			aLinePropertiesRight,
304 			aLinePropertiesTop,
305 			aLinePropertiesBottom,
306 			aLinePropertiesTopLeftToBottomRight,
307 			aLinePropertiesBottomLeftToTopRight,
308 			rTable.getSeCell() );
309 	}
310 	if ( rProperties.isBandCol() )
311 	{
312 		if ( ( !rProperties.isFirstCol() || ( nColumn != 0 ) ) &&
313 			( !rProperties.isLastCol() || ( nColumn != nMaxColumn ) ) )
314 		{
315 			sal_Int32 nBand = nColumn;
316 			if ( rProperties.isFirstCol() )
317 				nBand++;
318 			if ( nBand & 1 )
319 			{
320 				applyTableStylePart( rFilterBase, rxCell, aFillProperties,
321 					aLinePropertiesLeft,
322 					aLinePropertiesRight,
323 					aLinePropertiesTop,
324 					aLinePropertiesBottom,
325 					aLinePropertiesTopLeftToBottomRight,
326 					aLinePropertiesBottomLeftToTopRight,
327 					rTable.getBand2V() );
328 			}
329 			else
330 			{
331 				applyTableStylePart( rFilterBase, rxCell, aFillProperties,
332 					aLinePropertiesLeft,
333 					aLinePropertiesRight,
334 					aLinePropertiesTop,
335 					aLinePropertiesBottom,
336 					aLinePropertiesTopLeftToBottomRight,
337 					aLinePropertiesBottomLeftToTopRight,
338 					rTable.getBand1V() );
339 			}
340 		}
341 	}
342 	aLinePropertiesLeft.assignUsed( maLinePropertiesLeft );
343     aLinePropertiesRight.assignUsed( maLinePropertiesRight );
344     aLinePropertiesTop.assignUsed( maLinePropertiesTop );
345     aLinePropertiesBottom.assignUsed( maLinePropertiesBottom );
346     aLinePropertiesTopLeftToBottomRight.assignUsed( maLinePropertiesTopLeftToBottomRight );
347     aLinePropertiesBottomLeftToTopRight.assignUsed( maLinePropertiesBottomLeftToTopRight );
348     applyLineAttributes( rFilterBase, xPropSet, aLinePropertiesLeft, PROP_LeftBorder );
349     applyLineAttributes( rFilterBase, xPropSet, aLinePropertiesRight, PROP_RightBorder );
350     applyLineAttributes( rFilterBase, xPropSet, aLinePropertiesTop, PROP_TopBorder );
351     applyLineAttributes( rFilterBase, xPropSet, aLinePropertiesBottom, PROP_BottomBorder );
352     applyLineAttributes( rFilterBase, xPropSet, aLinePropertiesTopLeftToBottomRight, PROP_DiagonalTLBR );
353     applyLineAttributes( rFilterBase, xPropSet, aLinePropertiesBottomLeftToTopRight, PROP_DiagonalBLTR );
354 
355 	aFillProperties.assignUsed( maFillProperties );
356     ShapePropertyMap aPropMap( rFilterBase.getModelObjectHelper() );
357     // TODO: phClr?
358     aFillProperties.pushToPropMap( aPropMap, rFilterBase.getGraphicHelper() );
359     PropertySet( xPropSet ).setProperties( aPropMap );
360 
361     if ( getVertToken() == XML_eaVert )
362     {
363         xPropSet->setPropertyValue(::rtl::OUString::createFromAscii( "TextWritingMode" ) , Any(com::sun::star::text::WritingMode_TB_RL) );
364     }
365 }
366 
367 } } }
368