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