Line data Source code
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 "drawingmanager.hxx"
21 :
22 : #include <com/sun/star/awt/Rectangle.hpp>
23 : #include <com/sun/star/drawing/CircleKind.hpp>
24 : #include <com/sun/star/drawing/PointSequenceSequence.hpp>
25 : #include <com/sun/star/drawing/PolygonKind.hpp>
26 : #include <com/sun/star/drawing/XShapes.hpp>
27 : #include <oox/core/filterbase.hxx>
28 : #include <oox/drawingml/fillproperties.hxx>
29 : #include <oox/drawingml/lineproperties.hxx>
30 : #include <oox/drawingml/shapepropertymap.hxx>
31 : #include <oox/helper/containerhelper.hxx>
32 : #include <oox/token/tokens.hxx>
33 : #include "biffinputstream.hxx"
34 : #include "unitconverter.hxx"
35 :
36 : namespace oox {
37 : namespace xls {
38 :
39 : using namespace ::com::sun::star::awt;
40 : using namespace ::com::sun::star::drawing;
41 : using namespace ::com::sun::star::lang;
42 : using namespace ::com::sun::star::uno;
43 : using namespace ::oox::drawingml;
44 :
45 : namespace {
46 :
47 : // OBJ record -----------------------------------------------------------------
48 :
49 : const sal_uInt16 BIFF_OBJTYPE_UNKNOWN = 0xFFFF; // for internal use only
50 :
51 : // line formatting ------------------------------------------------------------
52 :
53 : const sal_uInt8 BIFF_OBJ_LINE_AUTOCOLOR = 64;
54 :
55 : const sal_uInt8 BIFF_OBJ_LINE_SOLID = 0;
56 : const sal_uInt8 BIFF_OBJ_LINE_DASH = 1;
57 : const sal_uInt8 BIFF_OBJ_LINE_DOT = 2;
58 : const sal_uInt8 BIFF_OBJ_LINE_DASHDOT = 3;
59 : const sal_uInt8 BIFF_OBJ_LINE_DASHDOTDOT = 4;
60 : const sal_uInt8 BIFF_OBJ_LINE_MEDTRANS = 5;
61 : const sal_uInt8 BIFF_OBJ_LINE_DARKTRANS = 6;
62 : const sal_uInt8 BIFF_OBJ_LINE_LIGHTTRANS = 7;
63 : const sal_uInt8 BIFF_OBJ_LINE_NONE = 255;
64 :
65 : const sal_uInt8 BIFF_OBJ_LINE_HAIR = 0;
66 : const sal_uInt8 BIFF_OBJ_LINE_THIN = 1;
67 : const sal_uInt8 BIFF_OBJ_LINE_MEDIUM = 2;
68 : const sal_uInt8 BIFF_OBJ_LINE_THICK = 3;
69 :
70 : const sal_uInt8 BIFF_OBJ_ARROW_OPEN = 1;
71 : const sal_uInt8 BIFF_OBJ_ARROW_FILLED = 2;
72 : const sal_uInt8 BIFF_OBJ_ARROW_OPENBOTH = 3;
73 : const sal_uInt8 BIFF_OBJ_ARROW_FILLEDBOTH = 4;
74 :
75 : const sal_uInt8 BIFF_OBJ_ARROW_NARROW = 0;
76 : const sal_uInt8 BIFF_OBJ_ARROW_MEDIUM = 1;
77 : const sal_uInt8 BIFF_OBJ_ARROW_WIDE = 2;
78 :
79 : // fill formatting ------------------------------------------------------------
80 :
81 : const sal_uInt8 BIFF_OBJ_FILL_AUTOCOLOR = 65;
82 :
83 : const sal_uInt8 BIFF_OBJ_PATT_NONE = 0;
84 : const sal_uInt8 BIFF_OBJ_PATT_SOLID = 1;
85 :
86 : } // namespace
87 :
88 : // Model structures for BIFF OBJ record data
89 :
90 0 : BiffObjLineModel::BiffObjLineModel() :
91 : mnColorIdx( BIFF_OBJ_LINE_AUTOCOLOR ),
92 : mnStyle( BIFF_OBJ_LINE_SOLID ),
93 : mnWidth( BIFF_OBJ_LINE_HAIR ),
94 0 : mbAuto( true )
95 : {
96 0 : }
97 :
98 0 : BiffObjFillModel::BiffObjFillModel() :
99 : mnBackColorIdx( BIFF_OBJ_LINE_AUTOCOLOR ),
100 : mnPattColorIdx( BIFF_OBJ_FILL_AUTOCOLOR ),
101 : mnPattern( BIFF_OBJ_PATT_SOLID ),
102 0 : mbAuto( true )
103 : {
104 0 : }
105 :
106 : // BIFF drawing objects
107 :
108 0 : BiffDrawingObjectContainer::BiffDrawingObjectContainer()
109 : {
110 0 : }
111 :
112 0 : void BiffDrawingObjectContainer::convertAndInsert( BiffDrawingBase& rDrawing, const Reference< XShapes >& rxShapes, const css::awt::Rectangle* pParentRect ) const
113 : {
114 0 : maObjects.forEachMem( &BiffDrawingObjectBase::convertAndInsert, ::boost::ref( rDrawing ), ::boost::cref( rxShapes ), pParentRect );
115 0 : }
116 :
117 0 : BiffDrawingObjectBase::BiffDrawingObjectBase( const WorksheetHelper& rHelper ) :
118 : WorksheetHelper( rHelper ),
119 : maAnchor( rHelper ),
120 : mnObjId( BIFF_OBJ_INVALID_ID ),
121 : mnObjType( BIFF_OBJTYPE_UNKNOWN ),
122 : mbHidden( false ),
123 : mbVisible( true ),
124 : mbPrintable( true ),
125 : mbAreaObj( false ),
126 : mbSimpleMacro( true ),
127 : mbProcessShape( true ),
128 : mbInsertShape( true ),
129 0 : mbCustomDff( false )
130 : {
131 0 : }
132 :
133 0 : BiffDrawingObjectBase::~BiffDrawingObjectBase()
134 : {
135 0 : }
136 :
137 0 : Reference< XShape > BiffDrawingObjectBase::convertAndInsert( BiffDrawingBase& rDrawing,
138 : const Reference< XShapes >& rxShapes, const css::awt::Rectangle* pParentRect ) const
139 : {
140 0 : Reference< XShape > xShape;
141 0 : if( rxShapes.is() && mbProcessShape && !mbHidden ) // TODO: support for hidden objects?
142 : {
143 : // base class 'ShapeAnchor' calculates the shape rectangle in 1/100 mm
144 : // in BIFF3-BIFF5, all shapes have absolute anchor (also children of group shapes)
145 0 : css::awt::Rectangle aShapeRect = maAnchor.calcAnchorRectHmm( getDrawPageSize() );
146 :
147 : // convert the shape, if the calculated rectangle is not empty
148 0 : bool bHasWidth = aShapeRect.Width > 0;
149 0 : bool bHasHeight = aShapeRect.Height > 0;
150 0 : if( mbAreaObj ? (bHasWidth && bHasHeight) : (bHasWidth || bHasHeight) )
151 : {
152 0 : xShape = implConvertAndInsert( rDrawing, rxShapes, aShapeRect );
153 : /* Notify the drawing that a new shape has been inserted (but not
154 : for children of group shapes). For convenience, pass the
155 : rectangle that contains position and size of the shape. */
156 0 : if( !pParentRect && xShape.is() )
157 0 : rDrawing.notifyShapeInserted( xShape, aShapeRect );
158 : }
159 : }
160 0 : return xShape;
161 : }
162 :
163 0 : void BiffDrawingObjectBase::convertLineProperties( ShapePropertyMap& rPropMap, const BiffObjLineModel& rLineModel, sal_uInt16 nArrows ) const
164 : {
165 0 : if( rLineModel.mbAuto )
166 : {
167 0 : BiffObjLineModel aAutoModel;
168 0 : aAutoModel.mbAuto = false;
169 0 : convertLineProperties( rPropMap, aAutoModel, nArrows );
170 0 : return;
171 : }
172 :
173 : /* Convert line formatting to DrawingML line formatting and let the
174 : DrawingML code do the hard work. */
175 0 : LineProperties aLineProps;
176 :
177 0 : if( rLineModel.mnStyle == BIFF_OBJ_LINE_NONE )
178 : {
179 0 : aLineProps.maLineFill.moFillType = XML_noFill;
180 : }
181 : else
182 : {
183 0 : aLineProps.maLineFill.moFillType = XML_solidFill;
184 0 : aLineProps.maLineFill.maFillColor.setPaletteClr( rLineModel.mnColorIdx );
185 0 : aLineProps.moLineCompound = XML_sng;
186 0 : aLineProps.moLineCap = XML_flat;
187 0 : aLineProps.moLineJoint = XML_round;
188 :
189 : // line width: use 0.35 mm per BIFF line width step
190 0 : sal_Int32 nLineWidth = 0;
191 0 : switch( rLineModel.mnWidth )
192 : {
193 : default:
194 0 : case BIFF_OBJ_LINE_HAIR: nLineWidth = 0; break;
195 0 : case BIFF_OBJ_LINE_THIN: nLineWidth = 20; break;
196 0 : case BIFF_OBJ_LINE_MEDIUM: nLineWidth = 40; break;
197 0 : case BIFF_OBJ_LINE_THICK: nLineWidth = 60; break;
198 : }
199 0 : aLineProps.moLineWidth = getLimitedValue< sal_Int32, sal_Int64 >( convertHmmToEmu( nLineWidth ), 0, SAL_MAX_INT32 );
200 :
201 : // dash style and transparency
202 0 : switch( rLineModel.mnStyle )
203 : {
204 : default:
205 : case BIFF_OBJ_LINE_SOLID:
206 0 : aLineProps.moPresetDash = XML_solid;
207 0 : break;
208 : case BIFF_OBJ_LINE_DASH:
209 0 : aLineProps.moPresetDash = XML_lgDash;
210 0 : break;
211 : case BIFF_OBJ_LINE_DOT:
212 0 : aLineProps.moPresetDash = XML_dot;
213 0 : break;
214 : case BIFF_OBJ_LINE_DASHDOT:
215 0 : aLineProps.moPresetDash = XML_lgDashDot;
216 0 : break;
217 : case BIFF_OBJ_LINE_DASHDOTDOT:
218 0 : aLineProps.moPresetDash = XML_lgDashDotDot;
219 0 : break;
220 : case BIFF_OBJ_LINE_MEDTRANS:
221 0 : aLineProps.moPresetDash = XML_solid;
222 0 : aLineProps.maLineFill.maFillColor.addTransformation( XML_alpha, 50 * PER_PERCENT );
223 0 : break;
224 : case BIFF_OBJ_LINE_DARKTRANS:
225 0 : aLineProps.moPresetDash = XML_solid;
226 0 : aLineProps.maLineFill.maFillColor.addTransformation( XML_alpha, 75 * PER_PERCENT );
227 0 : break;
228 : case BIFF_OBJ_LINE_LIGHTTRANS:
229 0 : aLineProps.moPresetDash = XML_solid;
230 0 : aLineProps.maLineFill.maFillColor.addTransformation( XML_alpha, 25 * PER_PERCENT );
231 0 : break;
232 : }
233 :
234 : // line ends
235 0 : bool bLineStart = false;
236 0 : bool bArrow = false;
237 0 : bool bFilled = false;
238 0 : switch( extractValue< sal_uInt8 >( nArrows, 0, 4 ) )
239 : {
240 0 : case BIFF_OBJ_ARROW_OPEN: bLineStart = false; bArrow = true; bFilled = false; break;
241 0 : case BIFF_OBJ_ARROW_OPENBOTH: bLineStart = true; bArrow = true; bFilled = false; break;
242 0 : case BIFF_OBJ_ARROW_FILLED: bLineStart = false; bArrow = true; bFilled = true; break;
243 0 : case BIFF_OBJ_ARROW_FILLEDBOTH: bLineStart = true; bArrow = true; bFilled = true; break;
244 : }
245 0 : if (bArrow)
246 : {
247 : // arrow type (open or closed)
248 0 : sal_Int32 nArrowType = bFilled ? XML_triangle : XML_arrow;
249 0 : aLineProps.maEndArrow.moArrowType = nArrowType;
250 0 : aLineProps.maStartArrow.moArrowType = bLineStart ? nArrowType : XML_none;
251 :
252 : // arrow width
253 0 : sal_Int32 nArrowWidth = XML_med;
254 0 : switch( extractValue< sal_uInt8 >( nArrows, 4, 4 ) )
255 : {
256 0 : case BIFF_OBJ_ARROW_NARROW: nArrowWidth = XML_sm; break;
257 0 : case BIFF_OBJ_ARROW_MEDIUM: nArrowWidth = XML_med; break;
258 0 : case BIFF_OBJ_ARROW_WIDE: nArrowWidth = XML_lg; break;
259 : }
260 0 : aLineProps.maStartArrow.moArrowWidth = aLineProps.maEndArrow.moArrowWidth = nArrowWidth;
261 :
262 : // arrow length
263 0 : sal_Int32 nArrowLength = XML_med;
264 0 : switch( extractValue< sal_uInt8 >( nArrows, 8, 4 ) )
265 : {
266 0 : case BIFF_OBJ_ARROW_NARROW: nArrowLength = XML_sm; break;
267 0 : case BIFF_OBJ_ARROW_MEDIUM: nArrowLength = XML_med; break;
268 0 : case BIFF_OBJ_ARROW_WIDE: nArrowLength = XML_lg; break;
269 : }
270 0 : aLineProps.maStartArrow.moArrowLength = aLineProps.maEndArrow.moArrowLength = nArrowLength;
271 : }
272 : }
273 :
274 0 : aLineProps.pushToPropMap( rPropMap, getBaseFilter().getGraphicHelper() );
275 : }
276 :
277 0 : void BiffDrawingObjectBase::convertFillProperties( ShapePropertyMap& rPropMap, const BiffObjFillModel& rFillModel ) const
278 : {
279 0 : if( rFillModel.mbAuto )
280 : {
281 0 : BiffObjFillModel aAutoModel;
282 0 : aAutoModel.mbAuto = false;
283 0 : convertFillProperties( rPropMap, aAutoModel );
284 0 : return;
285 : }
286 :
287 : /* Convert fill formatting to DrawingML fill formatting and let the
288 : DrawingML code do the hard work. */
289 0 : FillProperties aFillProps;
290 :
291 0 : if( rFillModel.mnPattern == BIFF_OBJ_PATT_NONE )
292 : {
293 0 : aFillProps.moFillType = XML_noFill;
294 : }
295 : else
296 : {
297 : const sal_Int32 spnPatternPresets[] = {
298 : XML_TOKEN_INVALID, XML_TOKEN_INVALID, XML_pct50, XML_pct50, XML_pct25,
299 : XML_dkHorz, XML_dkVert, XML_dkDnDiag, XML_dkUpDiag, XML_smCheck, XML_trellis,
300 : XML_ltHorz, XML_ltVert, XML_ltDnDiag, XML_ltUpDiag, XML_smGrid, XML_diagCross,
301 0 : XML_pct20, XML_pct10 };
302 0 : sal_Int32 nPatternPreset = STATIC_ARRAY_SELECT( spnPatternPresets, rFillModel.mnPattern, XML_TOKEN_INVALID );
303 0 : if( nPatternPreset == XML_TOKEN_INVALID )
304 : {
305 0 : aFillProps.moFillType = XML_solidFill;
306 0 : aFillProps.maFillColor.setPaletteClr( rFillModel.mnPattColorIdx );
307 : }
308 : else
309 : {
310 0 : aFillProps.moFillType = XML_pattFill;
311 0 : aFillProps.maPatternProps.maPattFgColor.setPaletteClr( rFillModel.mnPattColorIdx );
312 0 : aFillProps.maPatternProps.maPattBgColor.setPaletteClr( rFillModel.mnBackColorIdx );
313 0 : aFillProps.maPatternProps.moPattPreset = nPatternPreset;
314 : }
315 : #if 0
316 : static const sal_uInt8 sppnPatterns[][ 8 ] =
317 : {
318 : { 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55 },
319 : { 0x77, 0xDD, 0x77, 0xDD, 0x77, 0xDD, 0x77, 0xDD },
320 : { 0x88, 0x22, 0x88, 0x22, 0x88, 0x22, 0x88, 0x22 },
321 : { 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00 },
322 : { 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC },
323 : { 0x33, 0x66, 0xCC, 0x99, 0x33, 0x66, 0xCC, 0x99 },
324 : { 0xCC, 0x66, 0x33, 0x99, 0xCC, 0x66, 0x33, 0x99 },
325 : { 0xCC, 0xCC, 0x33, 0x33, 0xCC, 0xCC, 0x33, 0x33 },
326 : { 0xCC, 0xFF, 0x33, 0xFF, 0xCC, 0xFF, 0x33, 0xFF },
327 : { 0xFF, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00 },
328 : { 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88 },
329 : { 0x11, 0x22, 0x44, 0x88, 0x11, 0x22, 0x44, 0x88 },
330 : { 0x88, 0x44, 0x22, 0x11, 0x88, 0x44, 0x22, 0x11 },
331 : { 0xFF, 0x11, 0x11, 0x11, 0xFF, 0x11, 0x11, 0x11 },
332 : { 0xAA, 0x44, 0xAA, 0x11, 0xAA, 0x44, 0xAA, 0x11 },
333 : { 0x88, 0x00, 0x22, 0x00, 0x88, 0x00, 0x22, 0x00 },
334 : { 0x80, 0x00, 0x08, 0x00, 0x80, 0x00, 0x08, 0x00 }
335 : };
336 : const sal_uInt8* const pnPattern = sppnPatterns[ ::std::min< size_t >( rFillData.mnPattern - 2, SAL_N_ELEMENTS( sppnPatterns ) ) ];
337 : // create 2-colored 8x8 DIB
338 : SvMemoryStream aMemStrm;
339 : // { 0x0C, 0x00, 0x00, 0x00, 0x08, 0x00, 0x08, 0x00, 0x01, 0x00, 0x01, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00 }
340 : aMemStrm << sal_uInt32( 12 ) << sal_Int16( 8 ) << sal_Int16( 8 ) << sal_uInt16( 1 ) << sal_uInt16( 1 );
341 : aMemStrm << sal_uInt8( 0xFF ) << sal_uInt8( 0xFF ) << sal_uInt8( 0xFF );
342 : aMemStrm << sal_uInt8( 0x00 ) << sal_uInt8( 0x00 ) << sal_uInt8( 0x00 );
343 : for( size_t nIdx = 0; nIdx < 8; ++nIdx )
344 : aMemStrm << sal_uInt32( pnPattern[ nIdx ] ); // 32-bit little-endian
345 : aMemStrm.Seek( STREAM_SEEK_TO_BEGIN );
346 : Bitmap aBitmap;
347 : aBitmap.Read( aMemStrm, FALSE );
348 : XOBitmap aXOBitmap( aBitmap );
349 : aXOBitmap.Bitmap2Array();
350 : aXOBitmap.SetBitmapType( XBITMAP_8X8 );
351 : if( aXOBitmap.GetBackgroundColor().GetColor() == COL_BLACK )
352 : ::std::swap( aPattColor, aBackColor );
353 : aXOBitmap.SetPixelColor( aPattColor );
354 : aXOBitmap.SetBackgroundColor( aBackColor );
355 : rSdrObj.SetMergedItem( XFillStyleItem( XFILL_BITMAP ) );
356 : rSdrObj.SetMergedItem( XFillBitmapItem( EMPTY_OUSTRING, aXOBitmap ) );
357 : #endif
358 : }
359 :
360 0 : aFillProps.pushToPropMap( rPropMap, getBaseFilter().getGraphicHelper() );
361 : }
362 :
363 : // BIFF drawing page
364 :
365 0 : BiffDrawingBase::BiffDrawingBase( const WorksheetHelper& rHelper, const Reference< XDrawPage >& rxDrawPage ) :
366 : WorksheetHelper( rHelper ),
367 0 : mxDrawPage( rxDrawPage )
368 : {
369 0 : }
370 :
371 0 : void BiffDrawingBase::finalizeImport()
372 : {
373 0 : Reference< XShapes > xShapes( mxDrawPage, UNO_QUERY );
374 : OSL_ENSURE( xShapes.is(), "BiffDrawingBase::finalizeImport - no shapes container" );
375 0 : if( !xShapes.is() )
376 0 : return;
377 :
378 : // process list of objects to be skipped
379 0 : for( BiffObjIdVector::const_iterator aIt = maSkipObjs.begin(), aEnd = maSkipObjs.end(); aIt != aEnd; ++aIt )
380 0 : if( BiffDrawingObjectBase* pDrawingObj = maObjMapId.get( *aIt ).get() )
381 0 : pDrawingObj->setProcessShape( false );
382 :
383 : // process drawing objects without DFF data
384 0 : maRawObjs.convertAndInsert( *this, xShapes );
385 : }
386 :
387 0 : BiffSheetDrawing::BiffSheetDrawing( const WorksheetHelper& rHelper ) :
388 0 : BiffDrawingBase( rHelper, rHelper.getDrawPage() )
389 : {
390 0 : }
391 :
392 0 : void BiffSheetDrawing::notifyShapeInserted( const Reference< XShape >& /*rxShape*/, const css::awt::Rectangle& rShapeRect )
393 : {
394 : // collect all shape positions in the WorksheetHelper base class
395 0 : extendShapeBoundingBox( rShapeRect );
396 0 : }
397 :
398 : } // namespace xls
399 48 : } // namespace oox
400 :
401 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|