1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <tools/diagnose_ex.h>
22 #include <canvas/canvastools.hxx>
26 #include <com/sun/star/beans/NamedValue.hpp>
27 #include <com/sun/star/awt/Rectangle.hpp>
28 #include <com/sun/star/animations/ValuePair.hpp>
29 #include <com/sun/star/drawing/FillStyle.hpp>
30 #include <com/sun/star/drawing/LineStyle.hpp>
31 #include <com/sun/star/awt/FontSlant.hpp>
33 #include <basegfx/polygon/b2dpolygon.hxx>
34 #include <basegfx/polygon/b2dpolygontools.hxx>
35 #include <basegfx/range/b2drange.hxx>
36 #include <basegfx/vector/b2dvector.hxx>
37 #include <basegfx/vector/b2ivector.hxx>
38 #include <basegfx/matrix/b2dhommatrix.hxx>
39 #include <basegfx/numeric/ftools.hxx>
40 #include <basegfx/utils/lerp.hxx>
41 #include <basegfx/matrix/b2dhommatrixtools.hxx>
43 #include <cppcanvas/basegfxfactory.hxx>
45 #include <unoview.hxx>
46 #include <smilfunctionparser.hxx>
52 using namespace ::com::sun::star
;
60 class NamedValueComparator
63 explicit NamedValueComparator( const beans::NamedValue
& rKey
) :
68 bool operator()( const beans::NamedValue
& rValue
) const
70 return rValue
.Name
== mrKey
.Name
&& rValue
.Value
== mrKey
.Value
;
74 const beans::NamedValue
& mrKey
;
77 ::basegfx::B2DHomMatrix
getAttributedShapeTransformation( const ::basegfx::B2DRectangle
& rShapeBounds
,
78 const ShapeAttributeLayerSharedPtr
& pAttr
)
80 ::basegfx::B2DHomMatrix aTransform
;
81 const ::basegfx::B2DSize
& rSize( rShapeBounds
.getRange() );
83 const double nShearX( pAttr
->isShearXAngleValid() ?
84 pAttr
->getShearXAngle() :
86 const double nShearY( pAttr
->isShearYAngleValid() ?
87 pAttr
->getShearYAngle() :
89 const double nRotation( pAttr
->isRotationAngleValid() ?
90 pAttr
->getRotationAngle()*M_PI
/180.0 :
93 // scale, shear and rotation pivot point is the shape
94 // center - adapt origin accordingly
95 aTransform
.translate( -0.5, -0.5 );
97 // ensure valid size (zero size will inevitably lead
98 // to a singular transformation matrix)
99 aTransform
.scale( ::basegfx::pruneScaleValue(
101 ::basegfx::pruneScaleValue(
104 const bool bNeedShearX( !::basegfx::fTools::equalZero(nShearX
) );
105 const bool bNeedShearY( !::basegfx::fTools::equalZero(nShearY
) );
106 const bool bNeedRotation( !::basegfx::fTools::equalZero(nRotation
) );
108 if( bNeedRotation
|| bNeedShearX
|| bNeedShearY
)
111 aTransform
.shearX( nShearX
);
114 aTransform
.shearY( nShearY
);
117 aTransform
.rotate( nRotation
);
120 // move left, top corner back to position of the
121 // shape. Since we've already translated the
122 // center of the shape to the origin (the
123 // translate( -0.5, -0.5 ) above), translate to
124 // center of final shape position here.
125 aTransform
.translate( rShapeBounds
.getCenterX(),
126 rShapeBounds
.getCenterY() );
132 // Value extraction from Any
133 // =========================
135 /// extract unary double value from Any
136 bool extractValue( double& o_rValue
,
137 const uno::Any
& rSourceAny
,
138 const ShapeSharedPtr
& rShape
,
139 const ::basegfx::B2DVector
& rSlideBounds
)
141 // try to extract numeric value (double, or smaller POD, like float or int)
142 if( rSourceAny
>>= o_rValue
)
148 // try to extract string
150 if( !(rSourceAny
>>= aString
) )
151 return false; // nothing left to try
153 // parse the string into an ExpressionNode
156 // Parse string into ExpressionNode, eval node at time 0.0
157 o_rValue
= (*SmilFunctionParser::parseSmilValue(
159 calcRelativeShapeBounds(rSlideBounds
,
160 rShape
->getBounds()) ))(0.0);
170 /// extract enum/constant group value from Any
171 bool extractValue( sal_Int32
& o_rValue
,
172 const uno::Any
& rSourceAny
,
173 const ShapeSharedPtr
& /*rShape*/,
174 const ::basegfx::B2DVector
& /*rSlideBounds*/ )
176 // try to extract numeric value (int, or smaller POD, like byte)
177 if( rSourceAny
>>= o_rValue
)
183 // okay, no plain int. Maybe one of the domain-specific enums?
184 drawing::FillStyle eFillStyle
;
185 if( rSourceAny
>>= eFillStyle
)
187 o_rValue
= sal::static_int_cast
<sal_Int16
>(eFillStyle
);
193 drawing::LineStyle eLineStyle
;
194 if( rSourceAny
>>= eLineStyle
)
196 o_rValue
= sal::static_int_cast
<sal_Int16
>(eLineStyle
);
202 awt::FontSlant eFontSlant
;
203 if( rSourceAny
>>= eFontSlant
)
205 o_rValue
= sal::static_int_cast
<sal_Int16
>(eFontSlant
);
211 // nothing left to try. Failure
215 /// extract enum/constant group value from Any
216 bool extractValue( sal_Int16
& o_rValue
,
217 const uno::Any
& rSourceAny
,
218 const ShapeSharedPtr
& rShape
,
219 const ::basegfx::B2DVector
& rSlideBounds
)
222 if( !extractValue(aValue
,rSourceAny
,rShape
,rSlideBounds
) )
225 if( std::numeric_limits
<sal_Int16
>::max() < aValue
||
226 std::numeric_limits
<sal_Int16
>::min() > aValue
)
231 o_rValue
= static_cast<sal_Int16
>(aValue
);
236 /// extract color value from Any
237 bool extractValue( RGBColor
& o_rValue
,
238 const uno::Any
& rSourceAny
,
239 const ShapeSharedPtr
& /*rShape*/,
240 const ::basegfx::B2DVector
& /*rSlideBounds*/ )
242 // try to extract numeric value (double, or smaller POD, like float or int)
245 if( rSourceAny
>>= nTmp
)
247 sal_uInt32
aIntColor( static_cast< sal_uInt32
>(nTmp
) );
249 // TODO(F2): Handle color values correctly, here
250 o_rValue
= unoColor2RGBColor( aIntColor
);
257 // try double sequence
259 uno::Sequence
< double > aTmp
;
260 if( rSourceAny
>>= aTmp
)
262 ENSURE_OR_THROW( aTmp
.getLength() == 3,
263 "extractValue(): inappropriate length for RGB color value" );
265 o_rValue
= RGBColor( aTmp
[0], aTmp
[1], aTmp
[2] );
272 // try sal_Int32 sequence
274 uno::Sequence
< sal_Int32
> aTmp
;
275 if( rSourceAny
>>= aTmp
)
277 ENSURE_OR_THROW( aTmp
.getLength() == 3,
278 "extractValue(): inappropriate length for RGB color value" );
281 o_rValue
= RGBColor( ::cppcanvas::makeColor(
282 static_cast<sal_uInt8
>(aTmp
[0]),
283 static_cast<sal_uInt8
>(aTmp
[1]),
284 static_cast<sal_uInt8
>(aTmp
[2]),
292 // try sal_Int8 sequence
294 uno::Sequence
< sal_Int8
> aTmp
;
295 if( rSourceAny
>>= aTmp
)
297 ENSURE_OR_THROW( aTmp
.getLength() == 3,
298 "extractValue(): inappropriate length for RGB color value" );
300 o_rValue
= RGBColor( ::cppcanvas::makeColor( aTmp
[0], aTmp
[1], aTmp
[2], 255 ) );
307 // try to extract string
309 if( !(rSourceAny
>>= aString
) )
310 return false; // nothing left to try
312 // TODO(F2): Provide symbolic color values here
313 o_rValue
= RGBColor( 0.5, 0.5, 0.5 );
318 /// extract color value from Any
319 bool extractValue( HSLColor
& o_rValue
,
320 const uno::Any
& rSourceAny
,
321 const ShapeSharedPtr
& /*rShape*/,
322 const ::basegfx::B2DVector
& /*rSlideBounds*/ )
324 // try double sequence
326 uno::Sequence
< double > aTmp
;
327 if( rSourceAny
>>= aTmp
)
329 ENSURE_OR_THROW( aTmp
.getLength() == 3,
330 "extractValue(): inappropriate length for HSL color value" );
332 o_rValue
= HSLColor( aTmp
[0], aTmp
[1], aTmp
[2] );
339 // try sal_Int8 sequence
341 uno::Sequence
< sal_Int8
> aTmp
;
342 if( rSourceAny
>>= aTmp
)
344 ENSURE_OR_THROW( aTmp
.getLength() == 3,
345 "extractValue(): inappropriate length for HSL color value" );
347 o_rValue
= HSLColor( aTmp
[0]*360.0/255.0, aTmp
[1]/255.0, aTmp
[2]/255.0 );
354 return false; // nothing left to try
357 /// extract plain string from Any
358 bool extractValue( OUString
& o_rValue
,
359 const uno::Any
& rSourceAny
,
360 const ShapeSharedPtr
& /*rShape*/,
361 const ::basegfx::B2DVector
& /*rSlideBounds*/ )
363 // try to extract string
364 if( !(rSourceAny
>>= o_rValue
) )
365 return false; // nothing left to try
370 /// extract bool value from Any
371 bool extractValue( bool& o_rValue
,
372 const uno::Any
& rSourceAny
,
373 const ShapeSharedPtr
& /*rShape*/,
374 const ::basegfx::B2DVector
& /*rSlideBounds*/ )
377 // try to extract bool value
378 if( rSourceAny
>>= bTmp
)
386 // try to extract string
388 if( !(rSourceAny
>>= aString
) )
389 return false; // nothing left to try
391 // we also take the strings "true" and "false",
392 // as well as "on" and "off" here
393 if( aString
.equalsIgnoreAsciiCase("true") ||
394 aString
.equalsIgnoreAsciiCase("on") )
399 if( aString
.equalsIgnoreAsciiCase("false") ||
400 aString
.equalsIgnoreAsciiCase("off") )
406 // ultimately failed.
410 /// extract double 2-tuple from Any
411 bool extractValue( ::basegfx::B2DTuple
& o_rPair
,
412 const uno::Any
& rSourceAny
,
413 const ShapeSharedPtr
& rShape
,
414 const ::basegfx::B2DVector
& rSlideBounds
)
416 animations::ValuePair aPair
;
418 if( !(rSourceAny
>>= aPair
) )
422 if( !extractValue( nFirst
, aPair
.First
, rShape
, rSlideBounds
) )
426 if( !extractValue( nSecond
, aPair
.Second
, rShape
, rSlideBounds
) )
429 o_rPair
.setX( nFirst
);
430 o_rPair
.setY( nSecond
);
435 bool findNamedValue( uno::Sequence
< beans::NamedValue
> const& rSequence
,
436 const beans::NamedValue
& rSearchKey
)
438 const beans::NamedValue
* pArray
= rSequence
.getConstArray();
439 const size_t nLen( rSequence
.getLength() );
441 return ::std::any_of( pArray
,
443 NamedValueComparator( rSearchKey
) );
446 basegfx::B2DRange
calcRelativeShapeBounds( const basegfx::B2DVector
& rPageSize
,
447 const basegfx::B2DRange
& rShapeBounds
)
449 return basegfx::B2DRange( rShapeBounds
.getMinX() / rPageSize
.getX(),
450 rShapeBounds
.getMinY() / rPageSize
.getY(),
451 rShapeBounds
.getMaxX() / rPageSize
.getX(),
452 rShapeBounds
.getMaxY() / rPageSize
.getY() );
455 // TODO(F2): Currently, the positional attributes DO NOT mirror the XShape properties.
456 // First and foremost, this is because we must operate with the shape boundrect,
457 // not position and size (the conversion between logic rect, snap rect and boundrect
458 // are non-trivial for draw shapes, and I won't duplicate them here). Thus, shapes
459 // rotated on the page will still have 0.0 rotation angle, as the metafile
460 // representation fetched over the API is our default zero case.
462 ::basegfx::B2DHomMatrix
getShapeTransformation( const ::basegfx::B2DRectangle
& rShapeBounds
,
463 const ShapeAttributeLayerSharedPtr
& pAttr
)
467 const basegfx::B2DHomMatrix
aTransform(basegfx::utils::createScaleTranslateB2DHomMatrix(
468 rShapeBounds
.getWidth(), rShapeBounds
.getHeight(),
469 rShapeBounds
.getMinX(), rShapeBounds
.getMinY()));
475 return getAttributedShapeTransformation( rShapeBounds
,
480 ::basegfx::B2DHomMatrix
getSpriteTransformation( const ::basegfx::B2DVector
& rPixelSize
,
481 const ::basegfx::B2DVector
& rOrigSize
,
482 const ShapeAttributeLayerSharedPtr
& pAttr
)
484 ::basegfx::B2DHomMatrix aTransform
;
488 const double nShearX( pAttr
->isShearXAngleValid() ?
489 pAttr
->getShearXAngle() :
491 const double nShearY( pAttr
->isShearYAngleValid() ?
492 pAttr
->getShearYAngle() :
494 const double nRotation( pAttr
->isRotationAngleValid() ?
495 pAttr
->getRotationAngle()*M_PI
/180.0 :
498 // scale, shear and rotation pivot point is the
499 // sprite's pixel center - adapt origin accordingly
500 aTransform
.translate( -0.5*rPixelSize
.getX(),
501 -0.5*rPixelSize
.getY() );
503 const ::basegfx::B2DSize
aSize(
504 pAttr
->isWidthValid() ? pAttr
->getWidth() : rOrigSize
.getX(),
505 pAttr
->isHeightValid() ? pAttr
->getHeight() : rOrigSize
.getY() );
507 // ensure valid size (zero size will inevitably lead
508 // to a singular transformation matrix).
509 aTransform
.scale( ::basegfx::pruneScaleValue(
511 ::basegfx::pruneScaleValue(
512 rOrigSize
.getX() ) ),
513 ::basegfx::pruneScaleValue(
515 ::basegfx::pruneScaleValue(
516 rOrigSize
.getY() ) ) );
518 const bool bNeedShearX( !::basegfx::fTools::equalZero(nShearX
) );
519 const bool bNeedShearY( !::basegfx::fTools::equalZero(nShearY
) );
520 const bool bNeedRotation( !::basegfx::fTools::equalZero(nRotation
) );
522 if( bNeedRotation
|| bNeedShearX
|| bNeedShearY
)
525 aTransform
.shearX( nShearX
);
528 aTransform
.shearY( nShearY
);
531 aTransform
.rotate( nRotation
);
534 // move left, top corner back to original position of
535 // the sprite (we've translated the center of the
536 // sprite to the origin above).
537 aTransform
.translate( 0.5*rPixelSize
.getX(),
538 0.5*rPixelSize
.getY() );
541 // return identity transform for un-attributed
542 // shapes. This renders the sprite as-is, in its
543 // document-supplied size.
547 ::basegfx::B2DRectangle
getShapeUpdateArea( const ::basegfx::B2DRectangle
& rUnitBounds
,
548 const ::basegfx::B2DHomMatrix
& rShapeTransform
,
549 const ShapeAttributeLayerSharedPtr
& pAttr
)
551 ::basegfx::B2DHomMatrix aTransform
;
554 pAttr
->isCharScaleValid() &&
555 fabs(pAttr
->getCharScale()) > 1.0 )
557 // enlarge shape bounds. Have to consider the worst
558 // case here (the text fully fills the shape)
560 const double nCharScale( pAttr
->getCharScale() );
562 // center of scaling is the middle of the shape
563 aTransform
.translate( -0.5, -0.5 );
564 aTransform
.scale( nCharScale
, nCharScale
);
565 aTransform
.translate( 0.5, 0.5 );
568 aTransform
*= rShapeTransform
;
570 ::basegfx::B2DRectangle aRes
;
572 // apply shape transformation to unit rect
573 return ::canvas::tools::calcTransformedRectBounds(
579 ::basegfx::B2DRange
getShapeUpdateArea( const ::basegfx::B2DRange
& rUnitBounds
,
580 const ::basegfx::B2DRange
& rShapeBounds
)
582 return ::basegfx::B2DRectangle(
583 basegfx::utils::lerp( rShapeBounds
.getMinX(),
584 rShapeBounds
.getMaxX(),
585 rUnitBounds
.getMinX() ),
586 basegfx::utils::lerp( rShapeBounds
.getMinY(),
587 rShapeBounds
.getMaxY(),
588 rUnitBounds
.getMinY() ),
589 basegfx::utils::lerp( rShapeBounds
.getMinX(),
590 rShapeBounds
.getMaxX(),
591 rUnitBounds
.getMaxX() ),
592 basegfx::utils::lerp( rShapeBounds
.getMinY(),
593 rShapeBounds
.getMaxY(),
594 rUnitBounds
.getMaxY() ) );
597 ::basegfx::B2DRectangle
getShapePosSize( const ::basegfx::B2DRectangle
& rOrigBounds
,
598 const ShapeAttributeLayerSharedPtr
& pAttr
)
600 // an already empty shape bound need no further
601 // treatment. In fact, any changes applied below would
602 // actually remove the special empty state, thus, don't
605 rOrigBounds
.isEmpty() )
611 // cannot use maBounds anymore, attributes might have been
613 // Have to use absolute values here, as negative sizes
614 // (aka mirrored shapes) _still_ have the same bounds,
615 // only with mirrored content.
616 ::basegfx::B2DSize aSize
;
617 aSize
.setX( fabs( pAttr
->isWidthValid() ?
619 rOrigBounds
.getWidth() ) );
620 aSize
.setY( fabs( pAttr
->isHeightValid() ?
622 rOrigBounds
.getHeight() ) );
624 ::basegfx::B2DPoint aPos
;
625 aPos
.setX( pAttr
->isPosXValid() ?
627 rOrigBounds
.getCenterX() );
628 aPos
.setY( pAttr
->isPosYValid() ?
630 rOrigBounds
.getCenterY() );
632 // the positional attribute retrieved from the
633 // ShapeAttributeLayer actually denotes the _middle_
634 // of the shape (do it as the PPTs do...)
635 return ::basegfx::B2DRectangle( aPos
- 0.5*aSize
,
640 RGBColor
unoColor2RGBColor( sal_Int32 nColor
)
643 ::cppcanvas::makeColor(
644 // convert from API color to IntSRGBA color
645 // (0xAARRGGBB -> 0xRRGGBBAA)
646 static_cast< sal_uInt8
>( nColor
>> 16U ),
647 static_cast< sal_uInt8
>( nColor
>> 8U ),
648 static_cast< sal_uInt8
>( nColor
),
649 static_cast< sal_uInt8
>( nColor
>> 24U ) ) );
652 sal_Int32
RGBAColor2UnoColor( ::cppcanvas::IntSRGBA aColor
)
654 return ::cppcanvas::makeColorARGB(
655 // convert from IntSRGBA color to API color
656 // (0xRRGGBBAA -> 0xAARRGGBB)
657 static_cast< sal_uInt8
>(0),
658 ::cppcanvas::getRed(aColor
),
659 ::cppcanvas::getGreen(aColor
),
660 ::cppcanvas::getBlue(aColor
));
663 void fillRect( const ::cppcanvas::CanvasSharedPtr
& rCanvas
,
664 const ::basegfx::B2DRectangle
& rRect
,
665 ::cppcanvas::IntSRGBA aFillColor
)
667 const ::basegfx::B2DPolygon
aPoly(
668 ::basegfx::utils::createPolygonFromRect( rRect
));
670 ::cppcanvas::PolyPolygonSharedPtr
pPolyPoly(
671 ::cppcanvas::BaseGfxFactory::createPolyPolygon( rCanvas
, aPoly
) );
675 pPolyPoly
->setRGBAFillColor( aFillColor
);
680 void initSlideBackground( const ::cppcanvas::CanvasSharedPtr
& rCanvas
,
681 const ::basegfx::B2ISize
& rSize
)
683 ::cppcanvas::CanvasSharedPtr
pCanvas( rCanvas
->clone() );
685 // set transformation to identitiy (->device pixel)
686 pCanvas
->setTransformation( ::basegfx::B2DHomMatrix() );
688 // #i42440# Fill the _full_ background in
689 // black. Since we had to extend the bitmap by one
690 // pixel, and the bitmap is initialized white,
691 // depending on the slide content a one pixel wide
692 // line will show to the bottom and the right.
694 ::basegfx::B2DRectangle( 0.0, 0.0,
699 // fill the bounds rectangle in white. Subtract one pixel
700 // from both width and height, because the slide size is
701 // chosen one pixel larger than given by the drawing
702 // layer. This is because shapes with line style, that
703 // have the size of the slide would otherwise be cut
704 // off. OTOH, every other slide background (solid fill,
705 // gradient, bitmap) render one pixel less, thus revealing
706 // ugly white pixel to the right and the bottom.
708 ::basegfx::B2DRectangle( 0.0, 0.0,
714 ::basegfx::B2DRectangle
getAPIShapeBounds( const uno::Reference
< drawing::XShape
>& xShape
)
716 uno::Reference
< beans::XPropertySet
> xPropSet( xShape
,
717 uno::UNO_QUERY_THROW
);
719 awt::Rectangle aTmpRect
;
720 if( !(xPropSet
->getPropertyValue("BoundRect") >>= aTmpRect
) )
722 ENSURE_OR_THROW( false,
723 "getAPIShapeBounds(): Could not get \"BoundRect\" property from shape" );
726 return ::basegfx::B2DRectangle( aTmpRect
.X
,
728 aTmpRect
.X
+aTmpRect
.Width
,
729 aTmpRect
.Y
+aTmpRect
.Height
);
733 TODO(F1): When ZOrder someday becomes usable enable this
735 double getAPIShapePrio( const uno::Reference< drawing::XShape >& xShape )
737 uno::Reference< beans::XPropertySet > xPropSet( xShape,
738 uno::UNO_QUERY_THROW );
741 if( !(xPropSet->getPropertyValue(
742 OUString("ZOrder") ) >>= nPrio) )
744 ENSURE_OR_THROW( false,
745 "getAPIShapePrio(): Could not get \"ZOrder\" property from shape" );
748 // TODO(F2): Check and adapt the range of possible values here.
749 // Maybe we can also take the total number of shapes here
750 return nPrio / 65535.0;
754 basegfx::B2IVector
getSlideSizePixel( const basegfx::B2DVector
& rSlideSize
,
755 const UnoViewSharedPtr
& pView
)
757 ENSURE_OR_THROW(pView
, "getSlideSizePixel(): invalid view");
759 // determine transformed page bounds
760 const basegfx::B2DRange
aRect( 0,0,
763 basegfx::B2DRange aTmpRect
;
764 canvas::tools::calcTransformedRectBounds( aTmpRect
,
766 pView
->getTransformation() );
768 // #i42440# Returned slide size is one pixel too small, as
769 // rendering happens one pixel to the right and below the
770 // actual bound rect.
771 return basegfx::B2IVector(
772 basegfx::fround( aTmpRect
.getRange().getX() ) + 1,
773 basegfx::fround( aTmpRect
.getRange().getY() ) + 1 );
778 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */