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 <slideshowexceptions.hxx>
47 #include <smilfunctionparser.hxx>
53 using namespace ::com::sun::star
;
55 namespace slideshow::internal
59 class NamedValueComparator
62 explicit NamedValueComparator( const beans::NamedValue
& rKey
) :
67 bool operator()( const beans::NamedValue
& rValue
) const
69 return rValue
.Name
== mrKey
.Name
&& rValue
.Value
== mrKey
.Value
;
73 const beans::NamedValue
& mrKey
;
76 ::basegfx::B2DHomMatrix
getAttributedShapeTransformation( const ::basegfx::B2DRectangle
& rShapeBounds
,
77 const ShapeAttributeLayerSharedPtr
& pAttr
)
79 ::basegfx::B2DHomMatrix aTransform
;
80 const ::basegfx::B2DSize
& rSize( rShapeBounds
.getRange() );
82 const double nShearX( pAttr
->isShearXAngleValid() ?
83 pAttr
->getShearXAngle() :
85 const double nShearY( pAttr
->isShearYAngleValid() ?
86 pAttr
->getShearYAngle() :
88 const double nRotation( pAttr
->isRotationAngleValid() ?
89 basegfx::deg2rad(pAttr
->getRotationAngle()) :
92 // scale, shear and rotation pivot point is the shape
93 // center - adapt origin accordingly
94 aTransform
.translate( -0.5, -0.5 );
96 // ensure valid size (zero size will inevitably lead
97 // to a singular transformation matrix)
98 aTransform
.scale( ::basegfx::pruneScaleValue(
100 ::basegfx::pruneScaleValue(
103 const bool bNeedShearX( !::basegfx::fTools::equalZero(nShearX
) );
104 const bool bNeedShearY( !::basegfx::fTools::equalZero(nShearY
) );
105 const bool bNeedRotation( !::basegfx::fTools::equalZero(nRotation
) );
107 if( bNeedRotation
|| bNeedShearX
|| bNeedShearY
)
110 aTransform
.shearX( nShearX
);
113 aTransform
.shearY( nShearY
);
116 aTransform
.rotate( nRotation
);
119 // move left, top corner back to position of the
120 // shape. Since we've already translated the
121 // center of the shape to the origin (the
122 // translate( -0.5, -0.5 ) above), translate to
123 // center of final shape position here.
124 aTransform
.translate( rShapeBounds
.getCenterX(),
125 rShapeBounds
.getCenterY() );
131 // Value extraction from Any
132 // =========================
134 /// extract unary double value from Any
135 bool extractValue( double& o_rValue
,
136 const uno::Any
& rSourceAny
,
137 const ShapeSharedPtr
& rShape
,
138 const ::basegfx::B2DVector
& rSlideBounds
)
140 // try to extract numeric value (double, or smaller POD, like float or int)
141 if( rSourceAny
>>= o_rValue
)
147 // try to extract string
149 if( !(rSourceAny
>>= aString
) )
150 return false; // nothing left to try
152 // parse the string into an ExpressionNode
155 // Parse string into ExpressionNode, eval node at time 0.0
156 o_rValue
= (*SmilFunctionParser::parseSmilValue(
158 calcRelativeShapeBounds(rSlideBounds
,
159 rShape
->getBounds()) ))(0.0);
169 /// extract enum/constant group value from Any
170 bool extractValue( sal_Int32
& o_rValue
,
171 const uno::Any
& rSourceAny
,
172 const ShapeSharedPtr
& /*rShape*/,
173 const ::basegfx::B2DVector
& /*rSlideBounds*/ )
175 // try to extract numeric value (int, or smaller POD, like byte)
176 if( rSourceAny
>>= o_rValue
)
182 // okay, no plain int. Maybe one of the domain-specific enums?
183 drawing::FillStyle eFillStyle
;
184 if( rSourceAny
>>= eFillStyle
)
186 o_rValue
= sal::static_int_cast
<sal_Int16
>(eFillStyle
);
192 drawing::LineStyle eLineStyle
;
193 if( rSourceAny
>>= eLineStyle
)
195 o_rValue
= sal::static_int_cast
<sal_Int16
>(eLineStyle
);
201 awt::FontSlant eFontSlant
;
202 if( rSourceAny
>>= eFontSlant
)
204 o_rValue
= sal::static_int_cast
<sal_Int16
>(eFontSlant
);
210 // nothing left to try. Failure
214 /// extract enum/constant group value from Any
215 bool extractValue( sal_Int16
& o_rValue
,
216 const uno::Any
& rSourceAny
,
217 const ShapeSharedPtr
& rShape
,
218 const ::basegfx::B2DVector
& rSlideBounds
)
221 if( !extractValue(aValue
,rSourceAny
,rShape
,rSlideBounds
) )
224 if( std::numeric_limits
<sal_Int16
>::max() < aValue
||
225 std::numeric_limits
<sal_Int16
>::min() > aValue
)
230 o_rValue
= static_cast<sal_Int16
>(aValue
);
235 /// extract color value from Any
236 bool extractValue( RGBColor
& o_rValue
,
237 const uno::Any
& rSourceAny
,
238 const ShapeSharedPtr
& /*rShape*/,
239 const ::basegfx::B2DVector
& /*rSlideBounds*/ )
241 // try to extract numeric value (double, or smaller POD, like float or int)
244 if( rSourceAny
>>= nTmp
)
246 sal_uInt32
aIntColor( static_cast< sal_uInt32
>(nTmp
) );
248 // TODO(F2): Handle color values correctly, here
249 o_rValue
= unoColor2RGBColor( aIntColor
);
256 // try double sequence
258 uno::Sequence
< double > aTmp
;
259 if( rSourceAny
>>= aTmp
)
261 ENSURE_OR_THROW( aTmp
.getLength() == 3,
262 "extractValue(): inappropriate length for RGB color value" );
264 o_rValue
= RGBColor( aTmp
[0], aTmp
[1], aTmp
[2] );
271 // try sal_Int32 sequence
273 uno::Sequence
< sal_Int32
> aTmp
;
274 if( rSourceAny
>>= aTmp
)
276 ENSURE_OR_THROW( aTmp
.getLength() == 3,
277 "extractValue(): inappropriate length for RGB color value" );
280 o_rValue
= RGBColor( ::cppcanvas::makeColor(
281 static_cast<sal_uInt8
>(aTmp
[0]),
282 static_cast<sal_uInt8
>(aTmp
[1]),
283 static_cast<sal_uInt8
>(aTmp
[2]),
291 // try sal_Int8 sequence
293 uno::Sequence
< sal_Int8
> aTmp
;
294 if( rSourceAny
>>= aTmp
)
296 ENSURE_OR_THROW( aTmp
.getLength() == 3,
297 "extractValue(): inappropriate length for RGB color value" );
299 o_rValue
= RGBColor( ::cppcanvas::makeColor( aTmp
[0], aTmp
[1], aTmp
[2], 255 ) );
306 // try to extract string
308 if( !(rSourceAny
>>= aString
) )
309 return false; // nothing left to try
311 // TODO(F2): Provide symbolic color values here
312 o_rValue
= RGBColor( 0.5, 0.5, 0.5 );
317 /// extract color value from Any
318 bool extractValue( HSLColor
& o_rValue
,
319 const uno::Any
& rSourceAny
,
320 const ShapeSharedPtr
& /*rShape*/,
321 const ::basegfx::B2DVector
& /*rSlideBounds*/ )
323 // try double sequence
325 uno::Sequence
< double > aTmp
;
326 if( rSourceAny
>>= aTmp
)
328 ENSURE_OR_THROW( aTmp
.getLength() == 3,
329 "extractValue(): inappropriate length for HSL color value" );
331 o_rValue
= HSLColor( aTmp
[0], aTmp
[1], aTmp
[2] );
338 // try sal_Int8 sequence
340 uno::Sequence
< sal_Int8
> aTmp
;
341 if( rSourceAny
>>= aTmp
)
343 ENSURE_OR_THROW( aTmp
.getLength() == 3,
344 "extractValue(): inappropriate length for HSL color value" );
346 o_rValue
= HSLColor( aTmp
[0]*360.0/255.0, aTmp
[1]/255.0, aTmp
[2]/255.0 );
353 return false; // nothing left to try
356 /// extract plain string from Any
357 bool extractValue( OUString
& o_rValue
,
358 const uno::Any
& rSourceAny
,
359 const ShapeSharedPtr
& /*rShape*/,
360 const ::basegfx::B2DVector
& /*rSlideBounds*/ )
362 // try to extract string
363 return rSourceAny
>>= o_rValue
;
366 /// extract bool value from Any
367 bool extractValue( bool& o_rValue
,
368 const uno::Any
& rSourceAny
,
369 const ShapeSharedPtr
& /*rShape*/,
370 const ::basegfx::B2DVector
& /*rSlideBounds*/ )
373 // try to extract bool value
374 if( rSourceAny
>>= bTmp
)
382 // try to extract string
384 if( !(rSourceAny
>>= aString
) )
385 return false; // nothing left to try
387 // we also take the strings "true" and "false",
388 // as well as "on" and "off" here
389 if( aString
.equalsIgnoreAsciiCase("true") ||
390 aString
.equalsIgnoreAsciiCase("on") )
395 if( aString
.equalsIgnoreAsciiCase("false") ||
396 aString
.equalsIgnoreAsciiCase("off") )
402 // ultimately failed.
406 /// extract double 2-tuple from Any
407 bool extractValue( ::basegfx::B2DTuple
& o_rPair
,
408 const uno::Any
& rSourceAny
,
409 const ShapeSharedPtr
& rShape
,
410 const ::basegfx::B2DVector
& rSlideBounds
)
412 animations::ValuePair aPair
;
414 if( !(rSourceAny
>>= aPair
) )
418 if( !extractValue( nFirst
, aPair
.First
, rShape
, rSlideBounds
) )
422 if( !extractValue( nSecond
, aPair
.Second
, rShape
, rSlideBounds
) )
425 o_rPair
.setX( nFirst
);
426 o_rPair
.setY( nSecond
);
431 bool findNamedValue( uno::Sequence
< beans::NamedValue
> const& rSequence
,
432 const beans::NamedValue
& rSearchKey
)
434 return ::std::any_of( rSequence
.begin(), rSequence
.end(),
435 NamedValueComparator( rSearchKey
) );
438 basegfx::B2DRange
calcRelativeShapeBounds( const basegfx::B2DVector
& rPageSize
,
439 const basegfx::B2DRange
& rShapeBounds
)
441 return basegfx::B2DRange( rShapeBounds
.getMinX() / rPageSize
.getX(),
442 rShapeBounds
.getMinY() / rPageSize
.getY(),
443 rShapeBounds
.getMaxX() / rPageSize
.getX(),
444 rShapeBounds
.getMaxY() / rPageSize
.getY() );
447 // TODO(F2): Currently, the positional attributes DO NOT mirror the XShape properties.
448 // First and foremost, this is because we must operate with the shape boundrect,
449 // not position and size (the conversion between logic rect, snap rect and boundrect
450 // are non-trivial for draw shapes, and I won't duplicate them here). Thus, shapes
451 // rotated on the page will still have 0.0 rotation angle, as the metafile
452 // representation fetched over the API is our default zero case.
454 ::basegfx::B2DHomMatrix
getShapeTransformation( const ::basegfx::B2DRectangle
& rShapeBounds
,
455 const ShapeAttributeLayerSharedPtr
& pAttr
)
459 const basegfx::B2DHomMatrix
aTransform(basegfx::utils::createScaleTranslateB2DHomMatrix(
460 rShapeBounds
.getWidth(), rShapeBounds
.getHeight(),
461 rShapeBounds
.getMinX(), rShapeBounds
.getMinY()));
467 return getAttributedShapeTransformation( rShapeBounds
,
472 ::basegfx::B2DHomMatrix
getSpriteTransformation( const ::basegfx::B2DVector
& rPixelSize
,
473 const ::basegfx::B2DVector
& rOrigSize
,
474 const ShapeAttributeLayerSharedPtr
& pAttr
)
476 ::basegfx::B2DHomMatrix aTransform
;
480 const double nShearX( pAttr
->isShearXAngleValid() ?
481 pAttr
->getShearXAngle() :
483 const double nShearY( pAttr
->isShearYAngleValid() ?
484 pAttr
->getShearYAngle() :
486 const double nRotation( pAttr
->isRotationAngleValid() ?
487 basegfx::deg2rad(pAttr
->getRotationAngle()) :
490 // scale, shear and rotation pivot point is the
491 // sprite's pixel center - adapt origin accordingly
492 aTransform
.translate( -0.5*rPixelSize
.getX(),
493 -0.5*rPixelSize
.getY() );
495 const ::basegfx::B2DSize
aSize(
496 pAttr
->isWidthValid() ? pAttr
->getWidth() : rOrigSize
.getX(),
497 pAttr
->isHeightValid() ? pAttr
->getHeight() : rOrigSize
.getY() );
499 // ensure valid size (zero size will inevitably lead
500 // to a singular transformation matrix).
501 aTransform
.scale( ::basegfx::pruneScaleValue(
503 ::basegfx::pruneScaleValue(
504 rOrigSize
.getX() ) ),
505 ::basegfx::pruneScaleValue(
507 ::basegfx::pruneScaleValue(
508 rOrigSize
.getY() ) ) );
510 const bool bNeedShearX( !::basegfx::fTools::equalZero(nShearX
) );
511 const bool bNeedShearY( !::basegfx::fTools::equalZero(nShearY
) );
512 const bool bNeedRotation( !::basegfx::fTools::equalZero(nRotation
) );
514 if( bNeedRotation
|| bNeedShearX
|| bNeedShearY
)
517 aTransform
.shearX( nShearX
);
520 aTransform
.shearY( nShearY
);
523 aTransform
.rotate( nRotation
);
526 // move left, top corner back to original position of
527 // the sprite (we've translated the center of the
528 // sprite to the origin above).
529 aTransform
.translate( 0.5*rPixelSize
.getX(),
530 0.5*rPixelSize
.getY() );
533 // return identity transform for un-attributed
534 // shapes. This renders the sprite as-is, in its
535 // document-supplied size.
539 ::basegfx::B2DRectangle
getShapeUpdateArea( const ::basegfx::B2DRectangle
& rUnitBounds
,
540 const ::basegfx::B2DHomMatrix
& rShapeTransform
,
541 const ShapeAttributeLayerSharedPtr
& pAttr
)
543 ::basegfx::B2DHomMatrix aTransform
;
546 pAttr
->isCharScaleValid() &&
547 fabs(pAttr
->getCharScale()) > 1.0 )
549 // enlarge shape bounds. Have to consider the worst
550 // case here (the text fully fills the shape)
552 const double nCharScale( pAttr
->getCharScale() );
554 // center of scaling is the middle of the shape
555 aTransform
.translate( -0.5, -0.5 );
556 aTransform
.scale( nCharScale
, nCharScale
);
557 aTransform
.translate( 0.5, 0.5 );
560 aTransform
*= rShapeTransform
;
562 ::basegfx::B2DRectangle aRes
;
564 // apply shape transformation to unit rect
565 return ::canvas::tools::calcTransformedRectBounds(
571 ::basegfx::B2DRange
getShapeUpdateArea( const ::basegfx::B2DRange
& rUnitBounds
,
572 const ::basegfx::B2DRange
& rShapeBounds
)
574 return ::basegfx::B2DRectangle(
575 basegfx::utils::lerp( rShapeBounds
.getMinX(),
576 rShapeBounds
.getMaxX(),
577 rUnitBounds
.getMinX() ),
578 basegfx::utils::lerp( rShapeBounds
.getMinY(),
579 rShapeBounds
.getMaxY(),
580 rUnitBounds
.getMinY() ),
581 basegfx::utils::lerp( rShapeBounds
.getMinX(),
582 rShapeBounds
.getMaxX(),
583 rUnitBounds
.getMaxX() ),
584 basegfx::utils::lerp( rShapeBounds
.getMinY(),
585 rShapeBounds
.getMaxY(),
586 rUnitBounds
.getMaxY() ) );
589 ::basegfx::B2DRectangle
getShapePosSize( const ::basegfx::B2DRectangle
& rOrigBounds
,
590 const ShapeAttributeLayerSharedPtr
& pAttr
)
592 // an already empty shape bound need no further
593 // treatment. In fact, any changes applied below would
594 // actually remove the special empty state, thus, don't
597 rOrigBounds
.isEmpty() )
603 // cannot use maBounds anymore, attributes might have been
605 // Have to use absolute values here, as negative sizes
606 // (aka mirrored shapes) _still_ have the same bounds,
607 // only with mirrored content.
608 ::basegfx::B2DSize aSize
;
609 aSize
.setX( fabs( pAttr
->isWidthValid() ?
611 rOrigBounds
.getWidth() ) );
612 aSize
.setY( fabs( pAttr
->isHeightValid() ?
614 rOrigBounds
.getHeight() ) );
616 ::basegfx::B2DPoint aPos
;
617 aPos
.setX( pAttr
->isPosXValid() ?
619 rOrigBounds
.getCenterX() );
620 aPos
.setY( pAttr
->isPosYValid() ?
622 rOrigBounds
.getCenterY() );
624 // the positional attribute retrieved from the
625 // ShapeAttributeLayer actually denotes the _middle_
626 // of the shape (do it as the PPTs do...)
627 return ::basegfx::B2DRectangle( aPos
- 0.5*aSize
,
632 RGBColor
unoColor2RGBColor( sal_Int32 nColor
)
635 ::cppcanvas::makeColor(
636 // convert from API color to IntSRGBA color
637 // (0xAARRGGBB -> 0xRRGGBBAA)
638 static_cast< sal_uInt8
>( nColor
>> 16U ),
639 static_cast< sal_uInt8
>( nColor
>> 8U ),
640 static_cast< sal_uInt8
>( nColor
),
641 static_cast< sal_uInt8
>( nColor
>> 24U ) ) );
644 sal_Int32
RGBAColor2UnoColor( ::cppcanvas::IntSRGBA aColor
)
646 return ::cppcanvas::makeColorARGB(
647 // convert from IntSRGBA color to API color
648 // (0xRRGGBBAA -> 0xAARRGGBB)
649 static_cast< sal_uInt8
>(0),
650 ::cppcanvas::getRed(aColor
),
651 ::cppcanvas::getGreen(aColor
),
652 ::cppcanvas::getBlue(aColor
));
655 void fillRect( const ::cppcanvas::CanvasSharedPtr
& rCanvas
,
656 const ::basegfx::B2DRectangle
& rRect
,
657 ::cppcanvas::IntSRGBA aFillColor
)
659 const ::basegfx::B2DPolygon
aPoly(
660 ::basegfx::utils::createPolygonFromRect( rRect
));
662 ::cppcanvas::PolyPolygonSharedPtr
pPolyPoly(
663 ::cppcanvas::BaseGfxFactory::createPolyPolygon( rCanvas
, aPoly
) );
667 pPolyPoly
->setRGBAFillColor( aFillColor
);
672 void initSlideBackground( const ::cppcanvas::CanvasSharedPtr
& rCanvas
,
673 const ::basegfx::B2ISize
& rSize
)
675 ::cppcanvas::CanvasSharedPtr
pCanvas( rCanvas
->clone() );
677 // set transformation to identity (->device pixel)
678 pCanvas
->setTransformation( ::basegfx::B2DHomMatrix() );
680 // #i42440# Fill the _full_ background in
681 // black. Since we had to extend the bitmap by one
682 // pixel, and the bitmap is initialized white,
683 // depending on the slide content a one pixel wide
684 // line will show to the bottom and the right.
686 ::basegfx::B2DRectangle( 0.0, 0.0,
691 // fill the bounds rectangle in white. Subtract one pixel
692 // from both width and height, because the slide size is
693 // chosen one pixel larger than given by the drawing
694 // layer. This is because shapes with line style, that
695 // have the size of the slide would otherwise be cut
696 // off. OTOH, every other slide background (solid fill,
697 // gradient, bitmap) render one pixel less, thus revealing
698 // ugly white pixel to the right and the bottom.
700 ::basegfx::B2DRectangle( 0.0, 0.0,
706 ::basegfx::B2DRectangle
getAPIShapeBounds( const uno::Reference
< drawing::XShape
>& xShape
)
708 uno::Reference
< beans::XPropertySet
> xPropSet( xShape
,
709 uno::UNO_QUERY_THROW
);
711 awt::Rectangle aTmpRect
;
712 if( !(xPropSet
->getPropertyValue("BoundRect") >>= aTmpRect
) )
714 ENSURE_OR_THROW( false,
715 "getAPIShapeBounds(): Could not get \"BoundRect\" property from shape" );
718 return ::basegfx::B2DRectangle( aTmpRect
.X
,
720 aTmpRect
.X
+aTmpRect
.Width
,
721 aTmpRect
.Y
+aTmpRect
.Height
);
725 TODO(F1): When ZOrder someday becomes usable enable this
727 double getAPIShapePrio( const uno::Reference< drawing::XShape >& xShape )
729 uno::Reference< beans::XPropertySet > xPropSet( xShape,
730 uno::UNO_QUERY_THROW );
733 if( !(xPropSet->getPropertyValue(
734 OUString("ZOrder") ) >>= nPrio) )
736 ENSURE_OR_THROW( false,
737 "getAPIShapePrio(): Could not get \"ZOrder\" property from shape" );
740 // TODO(F2): Check and adapt the range of possible values here.
741 // Maybe we can also take the total number of shapes here
742 return nPrio / 65535.0;
746 basegfx::B2IVector
getSlideSizePixel( const basegfx::B2DVector
& rSlideSize
,
747 const UnoViewSharedPtr
& pView
)
749 ENSURE_OR_THROW(pView
, "getSlideSizePixel(): invalid view");
751 // determine transformed page bounds
752 const basegfx::B2DRange
aRect( 0,0,
755 basegfx::B2DRange aTmpRect
;
756 canvas::tools::calcTransformedRectBounds( aTmpRect
,
758 pView
->getTransformation() );
760 // #i42440# Returned slide size is one pixel too small, as
761 // rendering happens one pixel to the right and below the
762 // actual bound rect.
763 return basegfx::B2IVector(
764 basegfx::fround( aTmpRect
.getRange().getX() ) + 1,
765 basegfx::fround( aTmpRect
.getRange().getY() ) + 1 );
769 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */