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 <canvas/debug.hxx>
22 #include <tools/diagnose_ex.h>
23 #include <canvas/canvastools.hxx>
27 #include <com/sun/star/beans/NamedValue.hpp>
28 #include <com/sun/star/awt/Rectangle.hpp>
29 #include <com/sun/star/animations/ValuePair.hpp>
30 #include <com/sun/star/drawing/FillStyle.hpp>
31 #include <com/sun/star/drawing/LineStyle.hpp>
32 #include <com/sun/star/awt/FontSlant.hpp>
34 #include <basegfx/polygon/b2dpolygon.hxx>
35 #include <basegfx/polygon/b2dpolygontools.hxx>
36 #include <basegfx/range/b2drange.hxx>
37 #include <basegfx/vector/b2dvector.hxx>
38 #include <basegfx/vector/b2ivector.hxx>
39 #include <basegfx/matrix/b2dhommatrix.hxx>
40 #include <basegfx/numeric/ftools.hxx>
41 #include <basegfx/tools/lerp.hxx>
42 #include <basegfx/matrix/b2dhommatrixtools.hxx>
44 #include <cppcanvas/basegfxfactory.hxx>
46 #include "unoview.hxx"
47 #include "smilfunctionparser.hxx"
53 using namespace ::com::sun::star
;
61 class NamedValueComparator
64 NamedValueComparator( const beans::NamedValue
& rKey
) :
69 bool operator()( const beans::NamedValue
& rValue
) const
71 return rValue
.Name
== mrKey
.Name
&& rValue
.Value
== mrKey
.Value
;
75 const beans::NamedValue
& mrKey
;
78 ::basegfx::B2DHomMatrix
getAttributedShapeTransformation( const ::basegfx::B2DRectangle
& rShapeBounds
,
79 const ShapeAttributeLayerSharedPtr
& pAttr
)
81 ::basegfx::B2DHomMatrix aTransform
;
82 const ::basegfx::B2DSize
& rSize( rShapeBounds
.getRange() );
84 const double nShearX( pAttr
->isShearXAngleValid() ?
85 pAttr
->getShearXAngle() :
87 const double nShearY( pAttr
->isShearYAngleValid() ?
88 pAttr
->getShearYAngle() :
90 const double nRotation( pAttr
->isRotationAngleValid() ?
91 pAttr
->getRotationAngle()*M_PI
/180.0 :
94 // scale, shear and rotation pivot point is the shape
95 // center - adapt origin accordingly
96 aTransform
.translate( -0.5, -0.5 );
98 // ensure valid size (zero size will inevitably lead
99 // to a singular transformation matrix)
100 aTransform
.scale( ::basegfx::pruneScaleValue(
102 ::basegfx::pruneScaleValue(
105 const bool bNeedShearX( !::basegfx::fTools::equalZero(nShearX
) );
106 const bool bNeedShearY( !::basegfx::fTools::equalZero(nShearY
) );
107 const bool bNeedRotation( !::basegfx::fTools::equalZero(nRotation
) );
109 if( bNeedRotation
|| bNeedShearX
|| bNeedShearY
)
112 aTransform
.shearX( nShearX
);
115 aTransform
.shearY( nShearY
);
118 aTransform
.rotate( nRotation
);
121 // move left, top corner back to position of the
122 // shape. Since we've already translated the
123 // center of the shape to the origin (the
124 // translate( -0.5, -0.5 ) above), translate to
125 // center of final shape position here.
126 aTransform
.translate( rShapeBounds
.getCenterX(),
127 rShapeBounds
.getCenterY() );
133 // Value extraction from Any
134 // =========================
136 /// extract unary double value from Any
137 bool extractValue( double& o_rValue
,
138 const uno::Any
& rSourceAny
,
139 const ShapeSharedPtr
& rShape
,
140 const ::basegfx::B2DVector
& rSlideBounds
)
142 // try to extract numeric value (double, or smaller POD, like float or int)
143 if( (rSourceAny
>>= o_rValue
) )
149 // try to extract string
151 if( !(rSourceAny
>>= aString
) )
152 return false; // nothing left to try
154 // parse the string into an ExpressionNode
157 // Parse string into ExpressionNode, eval node at time 0.0
158 o_rValue
= (*SmilFunctionParser::parseSmilValue(
160 calcRelativeShapeBounds(rSlideBounds
,
161 rShape
->getBounds()) ))(0.0);
171 /// extract enum/constant group value from Any
172 bool extractValue( sal_Int32
& o_rValue
,
173 const uno::Any
& rSourceAny
,
174 const ShapeSharedPtr
& /*rShape*/,
175 const ::basegfx::B2DVector
& /*rSlideBounds*/ )
177 // try to extract numeric value (int, or smaller POD, like byte)
178 if( (rSourceAny
>>= o_rValue
) )
184 // okay, no plain int. Maybe one of the domain-specific enums?
185 drawing::FillStyle eFillStyle
;
186 if( (rSourceAny
>>= eFillStyle
) )
188 o_rValue
= sal::static_int_cast
<sal_Int16
>(eFillStyle
);
194 drawing::LineStyle eLineStyle
;
195 if( (rSourceAny
>>= eLineStyle
) )
197 o_rValue
= sal::static_int_cast
<sal_Int16
>(eLineStyle
);
203 awt::FontSlant eFontSlant
;
204 if( (rSourceAny
>>= eFontSlant
) )
206 o_rValue
= sal::static_int_cast
<sal_Int16
>(eFontSlant
);
212 // nothing left to try. Failure
216 /// extract enum/constant group value from Any
217 bool extractValue( sal_Int16
& o_rValue
,
218 const uno::Any
& rSourceAny
,
219 const ShapeSharedPtr
& rShape
,
220 const ::basegfx::B2DVector
& rSlideBounds
)
223 if( !extractValue(aValue
,rSourceAny
,rShape
,rSlideBounds
) )
226 if( std::numeric_limits
<sal_Int16
>::max() < aValue
||
227 std::numeric_limits
<sal_Int16
>::min() > aValue
)
232 o_rValue
= static_cast<sal_Int16
>(aValue
);
237 /// extract color value from Any
238 bool extractValue( RGBColor
& o_rValue
,
239 const uno::Any
& rSourceAny
,
240 const ShapeSharedPtr
& /*rShape*/,
241 const ::basegfx::B2DVector
& /*rSlideBounds*/ )
243 // try to extract numeric value (double, or smaller POD, like float or int)
246 if( (rSourceAny
>>= nTmp
) )
248 sal_uInt32
aIntColor( static_cast< sal_uInt32
>(nTmp
) );
250 // TODO(F2): Handle color values correctly, here
251 o_rValue
= unoColor2RGBColor( aIntColor
);
258 // try double sequence
260 uno::Sequence
< double > aTmp
;
261 if( (rSourceAny
>>= aTmp
) )
263 ENSURE_OR_THROW( aTmp
.getLength() == 3,
264 "extractValue(): inappropriate length for RGB color value" );
266 o_rValue
= RGBColor( aTmp
[0], aTmp
[1], aTmp
[2] );
273 // try sal_Int32 sequence
275 uno::Sequence
< sal_Int32
> aTmp
;
276 if( (rSourceAny
>>= aTmp
) )
278 ENSURE_OR_THROW( aTmp
.getLength() == 3,
279 "extractValue(): inappropriate length for RGB color value" );
282 o_rValue
= RGBColor( ::cppcanvas::makeColor(
283 static_cast<sal_uInt8
>(aTmp
[0]),
284 static_cast<sal_uInt8
>(aTmp
[1]),
285 static_cast<sal_uInt8
>(aTmp
[2]),
293 // try sal_Int8 sequence
295 uno::Sequence
< sal_Int8
> aTmp
;
296 if( (rSourceAny
>>= aTmp
) )
298 ENSURE_OR_THROW( aTmp
.getLength() == 3,
299 "extractValue(): inappropriate length for RGB color value" );
301 o_rValue
= RGBColor( ::cppcanvas::makeColor( aTmp
[0], aTmp
[1], aTmp
[2], 255 ) );
308 // try to extract string
310 if( !(rSourceAny
>>= aString
) )
311 return false; // nothing left to try
313 // TODO(F2): Provide symbolic color values here
314 o_rValue
= RGBColor( 0.5, 0.5, 0.5 );
319 /// extract color value from Any
320 bool extractValue( HSLColor
& o_rValue
,
321 const uno::Any
& rSourceAny
,
322 const ShapeSharedPtr
& /*rShape*/,
323 const ::basegfx::B2DVector
& /*rSlideBounds*/ )
325 // try double sequence
327 uno::Sequence
< double > aTmp
;
328 if( (rSourceAny
>>= aTmp
) )
330 ENSURE_OR_THROW( aTmp
.getLength() == 3,
331 "extractValue(): inappropriate length for HSL color value" );
333 o_rValue
= HSLColor( aTmp
[0], aTmp
[1], aTmp
[2] );
340 // try sal_Int8 sequence
342 uno::Sequence
< sal_Int8
> aTmp
;
343 if( (rSourceAny
>>= aTmp
) )
345 ENSURE_OR_THROW( aTmp
.getLength() == 3,
346 "extractValue(): inappropriate length for HSL color value" );
348 o_rValue
= HSLColor( aTmp
[0]*360.0/255.0, aTmp
[1]/255.0, aTmp
[2]/255.0 );
355 return false; // nothing left to try
358 /// extract plain string from Any
359 bool extractValue( OUString
& o_rValue
,
360 const uno::Any
& rSourceAny
,
361 const ShapeSharedPtr
& /*rShape*/,
362 const ::basegfx::B2DVector
& /*rSlideBounds*/ )
364 // try to extract string
365 if( !(rSourceAny
>>= o_rValue
) )
366 return false; // nothing left to try
371 /// extract bool value from Any
372 bool extractValue( bool& o_rValue
,
373 const uno::Any
& rSourceAny
,
374 const ShapeSharedPtr
& /*rShape*/,
375 const ::basegfx::B2DVector
& /*rSlideBounds*/ )
378 // try to extract bool value
379 if( (rSourceAny
>>= nTmp
) )
387 // try to extract string
389 if( !(rSourceAny
>>= aString
) )
390 return false; // nothing left to try
392 // we also take the strings "true" and "false",
393 // as well as "on" and "off" here
394 if( aString
.equalsIgnoreAsciiCase("true") ||
395 aString
.equalsIgnoreAsciiCase("on") )
400 if( aString
.equalsIgnoreAsciiCase("false") ||
401 aString
.equalsIgnoreAsciiCase("off") )
407 // ultimately failed.
411 /// extract double 2-tuple from Any
412 bool extractValue( ::basegfx::B2DTuple
& o_rPair
,
413 const uno::Any
& rSourceAny
,
414 const ShapeSharedPtr
& rShape
,
415 const ::basegfx::B2DVector
& rSlideBounds
)
417 animations::ValuePair aPair
;
419 if( !(rSourceAny
>>= aPair
) )
423 if( !extractValue( nFirst
, aPair
.First
, rShape
, rSlideBounds
) )
427 if( !extractValue( nSecond
, aPair
.Second
, rShape
, rSlideBounds
) )
430 o_rPair
.setX( nFirst
);
431 o_rPair
.setY( nSecond
);
436 bool findNamedValue( uno::Sequence
< beans::NamedValue
> const& rSequence
,
437 const beans::NamedValue
& rSearchKey
)
439 const beans::NamedValue
* pArray
= rSequence
.getConstArray();
440 const size_t nLen( rSequence
.getLength() );
442 return ::std::any_of( pArray
,
444 NamedValueComparator( rSearchKey
) );
447 basegfx::B2DRange
calcRelativeShapeBounds( const basegfx::B2DVector
& rPageSize
,
448 const basegfx::B2DRange
& rShapeBounds
)
450 return basegfx::B2DRange( rShapeBounds
.getMinX() / rPageSize
.getX(),
451 rShapeBounds
.getMinY() / rPageSize
.getY(),
452 rShapeBounds
.getMaxX() / rPageSize
.getX(),
453 rShapeBounds
.getMaxY() / rPageSize
.getY() );
456 // TODO(F2): Currently, the positional attributes DO NOT mirror the XShape properties.
457 // First and foremost, this is because we must operate with the shape boundrect,
458 // not position and size (the conversion between logic rect, snap rect and boundrect
459 // are non-trivial for draw shapes, and I won't duplicate them here). Thus, shapes
460 // rotated on the page will still have 0.0 rotation angle, as the metafile
461 // representation fetched over the API is our default zero case.
463 ::basegfx::B2DHomMatrix
getShapeTransformation( const ::basegfx::B2DRectangle
& rShapeBounds
,
464 const ShapeAttributeLayerSharedPtr
& pAttr
)
468 const basegfx::B2DHomMatrix
aTransform(basegfx::tools::createScaleTranslateB2DHomMatrix(
469 rShapeBounds
.getWidth(), rShapeBounds
.getHeight(),
470 rShapeBounds
.getMinX(), rShapeBounds
.getMinY()));
476 return getAttributedShapeTransformation( rShapeBounds
,
481 ::basegfx::B2DHomMatrix
getSpriteTransformation( const ::basegfx::B2DVector
& rPixelSize
,
482 const ::basegfx::B2DVector
& rOrigSize
,
483 const ShapeAttributeLayerSharedPtr
& pAttr
)
485 ::basegfx::B2DHomMatrix aTransform
;
489 const double nShearX( pAttr
->isShearXAngleValid() ?
490 pAttr
->getShearXAngle() :
492 const double nShearY( pAttr
->isShearYAngleValid() ?
493 pAttr
->getShearYAngle() :
495 const double nRotation( pAttr
->isRotationAngleValid() ?
496 pAttr
->getRotationAngle()*M_PI
/180.0 :
499 // scale, shear and rotation pivot point is the
500 // sprite's pixel center - adapt origin accordingly
501 aTransform
.translate( -0.5*rPixelSize
.getX(),
502 -0.5*rPixelSize
.getY() );
504 const ::basegfx::B2DSize
aSize(
505 pAttr
->isWidthValid() ? pAttr
->getWidth() : rOrigSize
.getX(),
506 pAttr
->isHeightValid() ? pAttr
->getHeight() : rOrigSize
.getY() );
508 // ensure valid size (zero size will inevitably lead
509 // to a singular transformation matrix).
510 aTransform
.scale( ::basegfx::pruneScaleValue(
512 ::basegfx::pruneScaleValue(
513 rOrigSize
.getX() ) ),
514 ::basegfx::pruneScaleValue(
516 ::basegfx::pruneScaleValue(
517 rOrigSize
.getY() ) ) );
519 const bool bNeedShearX( !::basegfx::fTools::equalZero(nShearX
) );
520 const bool bNeedShearY( !::basegfx::fTools::equalZero(nShearY
) );
521 const bool bNeedRotation( !::basegfx::fTools::equalZero(nRotation
) );
523 if( bNeedRotation
|| bNeedShearX
|| bNeedShearY
)
526 aTransform
.shearX( nShearX
);
529 aTransform
.shearY( nShearY
);
532 aTransform
.rotate( nRotation
);
535 // move left, top corner back to original position of
536 // the sprite (we've translated the center of the
537 // sprite to the origin above).
538 aTransform
.translate( 0.5*rPixelSize
.getX(),
539 0.5*rPixelSize
.getY() );
542 // return identity transform for un-attributed
543 // shapes. This renders the sprite as-is, in it's
544 // document-supplied size.
548 ::basegfx::B2DRectangle
getShapeUpdateArea( const ::basegfx::B2DRectangle
& rUnitBounds
,
549 const ::basegfx::B2DHomMatrix
& rShapeTransform
,
550 const ShapeAttributeLayerSharedPtr
& pAttr
)
552 ::basegfx::B2DHomMatrix aTransform
;
555 pAttr
->isCharScaleValid() &&
556 fabs(pAttr
->getCharScale()) > 1.0 )
558 // enlarge shape bounds. Have to consider the worst
559 // case here (the text fully fills the shape)
561 const double nCharScale( pAttr
->getCharScale() );
563 // center of scaling is the middle of the shape
564 aTransform
.translate( -0.5, -0.5 );
565 aTransform
.scale( nCharScale
, nCharScale
);
566 aTransform
.translate( 0.5, 0.5 );
569 aTransform
*= rShapeTransform
;
571 ::basegfx::B2DRectangle aRes
;
573 // apply shape transformation to unit rect
574 return ::canvas::tools::calcTransformedRectBounds(
580 ::basegfx::B2DRange
getShapeUpdateArea( const ::basegfx::B2DRange
& rUnitBounds
,
581 const ::basegfx::B2DRange
& rShapeBounds
)
583 return ::basegfx::B2DRectangle(
584 basegfx::tools::lerp( rShapeBounds
.getMinX(),
585 rShapeBounds
.getMaxX(),
586 rUnitBounds
.getMinX() ),
587 basegfx::tools::lerp( rShapeBounds
.getMinY(),
588 rShapeBounds
.getMaxY(),
589 rUnitBounds
.getMinY() ),
590 basegfx::tools::lerp( rShapeBounds
.getMinX(),
591 rShapeBounds
.getMaxX(),
592 rUnitBounds
.getMaxX() ),
593 basegfx::tools::lerp( rShapeBounds
.getMinY(),
594 rShapeBounds
.getMaxY(),
595 rUnitBounds
.getMaxY() ) );
598 ::basegfx::B2DRectangle
getShapePosSize( const ::basegfx::B2DRectangle
& rOrigBounds
,
599 const ShapeAttributeLayerSharedPtr
& pAttr
)
601 // an already empty shape bound need no further
602 // treatment. In fact, any changes applied below would
603 // actually remove the special empty state, thus, don't
606 rOrigBounds
.isEmpty() )
612 // cannot use maBounds anymore, attributes might have been
614 // Have to use absolute values here, as negative sizes
615 // (aka mirrored shapes) _still_ have the same bounds,
616 // only with mirrored content.
617 ::basegfx::B2DSize aSize
;
618 aSize
.setX( fabs( pAttr
->isWidthValid() ?
620 rOrigBounds
.getWidth() ) );
621 aSize
.setY( fabs( pAttr
->isHeightValid() ?
623 rOrigBounds
.getHeight() ) );
625 ::basegfx::B2DPoint aPos
;
626 aPos
.setX( pAttr
->isPosXValid() ?
628 rOrigBounds
.getCenterX() );
629 aPos
.setY( pAttr
->isPosYValid() ?
631 rOrigBounds
.getCenterY() );
633 // the positional attribute retrieved from the
634 // ShapeAttributeLayer actually denotes the _middle_
635 // of the shape (do it as the PPTs do...)
636 return ::basegfx::B2DRectangle( aPos
- 0.5*aSize
,
641 RGBColor
unoColor2RGBColor( sal_Int32 nColor
)
644 ::cppcanvas::makeColor(
645 // convert from API color to IntSRGBA color
646 // (0xAARRGGBB -> 0xRRGGBBAA)
647 static_cast< sal_uInt8
>( nColor
>> 16U ),
648 static_cast< sal_uInt8
>( nColor
>> 8U ),
649 static_cast< sal_uInt8
>( nColor
),
650 static_cast< sal_uInt8
>( nColor
>> 24U ) ) );
653 sal_Int32
RGBAColor2UnoColor( ::cppcanvas::Color::IntSRGBA aColor
)
655 return ::cppcanvas::makeColorARGB(
656 // convert from IntSRGBA color to API color
657 // (0xRRGGBBAA -> 0xAARRGGBB)
658 static_cast< sal_uInt8
>(0),
659 ::cppcanvas::getRed(aColor
),
660 ::cppcanvas::getGreen(aColor
),
661 ::cppcanvas::getBlue(aColor
));
664 void fillRect( const ::cppcanvas::CanvasSharedPtr
& rCanvas
,
665 const ::basegfx::B2DRectangle
& rRect
,
666 ::cppcanvas::Color::IntSRGBA aFillColor
)
668 const ::basegfx::B2DPolygon
aPoly(
669 ::basegfx::tools::createPolygonFromRect( rRect
));
671 ::cppcanvas::PolyPolygonSharedPtr
pPolyPoly(
672 ::cppcanvas::BaseGfxFactory::createPolyPolygon( rCanvas
, aPoly
) );
676 pPolyPoly
->setRGBAFillColor( aFillColor
);
681 void initSlideBackground( const ::cppcanvas::CanvasSharedPtr
& rCanvas
,
682 const ::basegfx::B2ISize
& rSize
)
684 ::cppcanvas::CanvasSharedPtr
pCanvas( rCanvas
->clone() );
686 // set transformation to identitiy (->device pixel)
687 pCanvas
->setTransformation( ::basegfx::B2DHomMatrix() );
689 // #i42440# Fill the _full_ background in
690 // black. Since we had to extend the bitmap by one
691 // pixel, and the bitmap is initialized white,
692 // depending on the slide content a one pixel wide
693 // line will show to the bottom and the right.
695 ::basegfx::B2DRectangle( 0.0, 0.0,
700 // fill the bounds rectangle in white. Subtract one pixel
701 // from both width and height, because the slide size is
702 // chosen one pixel larger than given by the drawing
703 // layer. This is because shapes with line style, that
704 // have the size of the slide would otherwise be cut
705 // off. OTOH, every other slide background (solid fill,
706 // gradient, bitmap) render one pixel less, thus revealing
707 // ugly white pixel to the right and the bottom.
709 ::basegfx::B2DRectangle( 0.0, 0.0,
715 ::basegfx::B2DRectangle
getAPIShapeBounds( const uno::Reference
< drawing::XShape
>& xShape
)
717 uno::Reference
< beans::XPropertySet
> xPropSet( xShape
,
718 uno::UNO_QUERY_THROW
);
720 awt::Rectangle aTmpRect
;
721 if( !(xPropSet
->getPropertyValue(
722 OUString("BoundRect") ) >>= aTmpRect
) )
724 ENSURE_OR_THROW( false,
725 "getAPIShapeBounds(): Could not get \"BoundRect\" property from shape" );
728 return ::basegfx::B2DRectangle( aTmpRect
.X
,
730 aTmpRect
.X
+aTmpRect
.Width
,
731 aTmpRect
.Y
+aTmpRect
.Height
);
735 TODO(F1): When ZOrder someday becomes usable enable this
737 double getAPIShapePrio( const uno::Reference< drawing::XShape >& xShape )
739 uno::Reference< beans::XPropertySet > xPropSet( xShape,
740 uno::UNO_QUERY_THROW );
743 if( !(xPropSet->getPropertyValue(
744 OUString("ZOrder") ) >>= nPrio) )
746 ENSURE_OR_THROW( false,
747 "getAPIShapePrio(): Could not get \"ZOrder\" property from shape" );
750 // TODO(F2): Check and adapt the range of possible values here.
751 // Maybe we can also take the total number of shapes here
752 return nPrio / 65535.0;
756 basegfx::B2IVector
getSlideSizePixel( const basegfx::B2DVector
& rSlideSize
,
757 const UnoViewSharedPtr
& pView
)
759 ENSURE_OR_THROW(pView
, "getSlideSizePixel(): invalid view");
761 // determine transformed page bounds
762 const basegfx::B2DRange
aRect( 0,0,
765 basegfx::B2DRange aTmpRect
;
766 canvas::tools::calcTransformedRectBounds( aTmpRect
,
768 pView
->getTransformation() );
770 // #i42440# Returned slide size is one pixel too small, as
771 // rendering happens one pixel to the right and below the
772 // actual bound rect.
773 return basegfx::B2IVector(
774 basegfx::fround( aTmpRect
.getRange().getX() ) + 1,
775 basegfx::fround( aTmpRect
.getRange().getY() ) + 1 );
780 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */