1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_drawinglayer.hxx"
26 
27 #include <drawinglayer/primitive2d/mediaprimitive2d.hxx>
28 #include <basegfx/polygon/b2dpolygon.hxx>
29 #include <basegfx/polygon/b2dpolygontools.hxx>
30 #include <drawinglayer/primitive2d/polypolygonprimitive2d.hxx>
31 #include <avmedia/mediawindow.hxx>
32 #include <svtools/grfmgr.hxx>
33 #include <drawinglayer/primitive2d/graphicprimitive2d.hxx>
34 #include <drawinglayer/geometry/viewinformation2d.hxx>
35 #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
36 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
37 #include <drawinglayer/primitive2d/hiddengeometryprimitive2d.hxx>
38 
39 //////////////////////////////////////////////////////////////////////////////
40 
41 namespace drawinglayer
42 {
43 	namespace primitive2d
44 	{
create2DDecomposition(const geometry::ViewInformation2D & rViewInformation) const45 		Primitive2DSequence MediaPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
46 		{
47 			Primitive2DSequence xRetval(1);
48 
49 			// create background object
50 			basegfx::B2DPolygon aBackgroundPolygon(basegfx::tools::createUnitPolygon());
51 			aBackgroundPolygon.transform(getTransform());
52 			const Primitive2DReference xRefBackground(
53 				new PolyPolygonColorPrimitive2D(
54 					basegfx::B2DPolyPolygon(aBackgroundPolygon),
55 					getBackgroundColor()));
56 			xRetval[0] = xRefBackground;
57 
58 			// try to get graphic snapshot
59 			const Graphic aGraphic(avmedia::MediaWindow::grabFrame(getURL(), true));
60 
61 			if(GRAPHIC_BITMAP == aGraphic.GetType() || GRAPHIC_GDIMETAFILE == aGraphic.GetType())
62 			{
63 				const GraphicObject aGraphicObject(aGraphic);
64 				const GraphicAttr aGraphicAttr;
65 				xRetval.realloc(2);
66 				xRetval[0] = xRefBackground;
67 				xRetval[1] = Primitive2DReference(new GraphicPrimitive2D(getTransform(), aGraphicObject, aGraphicAttr));
68 			}
69 
70 			if(getDiscreteBorder())
71 			{
72 				const basegfx::B2DVector aDiscreteInLogic(rViewInformation.getInverseObjectToViewTransformation() *
73                     basegfx::B2DVector((double)getDiscreteBorder(), (double)getDiscreteBorder()));
74 				const double fDiscreteSize(aDiscreteInLogic.getX() + aDiscreteInLogic.getY());
75 
76 				basegfx::B2DRange aSourceRange(0.0, 0.0, 1.0, 1.0);
77 				aSourceRange.transform(getTransform());
78 
79 				basegfx::B2DRange aDestRange(aSourceRange);
80 				aDestRange.grow(-0.5 * fDiscreteSize);
81 
82 				if(basegfx::fTools::equalZero(aDestRange.getWidth()) || basegfx::fTools::equalZero(aDestRange.getHeight()))
83 				{
84 					// shrunk primitive has no content (zero size in X or Y), nothing to display. Still create
85 					// invisible content for HitTest and BoundRect
86 					const Primitive2DReference xHiddenLines(new HiddenGeometryPrimitive2D(xRetval));
87 
88 					xRetval = Primitive2DSequence(&xHiddenLines, 1);
89 				}
90 				else
91 				{
92 					// create transformation matrix from original range to shrunk range
93 					basegfx::B2DHomMatrix aTransform;
94 					aTransform.translate(-aSourceRange.getMinX(), -aSourceRange.getMinY());
95 					aTransform.scale(aDestRange.getWidth() / aSourceRange.getWidth(), aDestRange.getHeight() / aSourceRange.getHeight());
96 					aTransform.translate(aDestRange.getMinX(), aDestRange.getMinY());
97 
98 					// add transform primitive
99 					const Primitive2DReference aScaled(new TransformPrimitive2D(aTransform, xRetval));
100 					xRetval = Primitive2DSequence(&aScaled, 1L);
101 				}
102 			}
103 
104 			return xRetval;
105 		}
106 
MediaPrimitive2D(const basegfx::B2DHomMatrix & rTransform,const rtl::OUString & rURL,const basegfx::BColor & rBackgroundColor,sal_uInt32 nDiscreteBorder)107 		MediaPrimitive2D::MediaPrimitive2D(
108 			const basegfx::B2DHomMatrix& rTransform,
109 			const rtl::OUString& rURL,
110 			const basegfx::BColor& rBackgroundColor,
111 			sal_uInt32 nDiscreteBorder)
112 		:	BufferedDecompositionPrimitive2D(),
113 			maTransform(rTransform),
114 			maURL(rURL),
115 			maBackgroundColor(rBackgroundColor),
116 			mnDiscreteBorder(nDiscreteBorder)
117 		{
118 		}
119 
operator ==(const BasePrimitive2D & rPrimitive) const120 		bool MediaPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
121 		{
122 			if(BufferedDecompositionPrimitive2D::operator==(rPrimitive))
123 			{
124 				const MediaPrimitive2D& rCompare = (MediaPrimitive2D&)rPrimitive;
125 
126 				return (getTransform() == rCompare.getTransform()
127 					&& getURL() == rCompare.getURL()
128 					&& getBackgroundColor() == rCompare.getBackgroundColor()
129 					&& getDiscreteBorder() == rCompare.getDiscreteBorder());
130 			}
131 
132 			return false;
133 		}
134 
getB2DRange(const geometry::ViewInformation2D & rViewInformation) const135 		basegfx::B2DRange MediaPrimitive2D::getB2DRange(const geometry::ViewInformation2D& rViewInformation) const
136 		{
137 			basegfx::B2DRange aRetval(0.0, 0.0, 1.0, 1.0);
138 			aRetval.transform(getTransform());
139 
140 			if(getDiscreteBorder())
141 			{
142 				const basegfx::B2DVector aDiscreteInLogic(rViewInformation.getInverseObjectToViewTransformation() *
143                     basegfx::B2DVector((double)getDiscreteBorder(), (double)getDiscreteBorder()));
144 				const double fDiscreteSize(aDiscreteInLogic.getX() + aDiscreteInLogic.getY());
145 
146 				aRetval.grow(-0.5 * fDiscreteSize);
147 			}
148 
149 			return aRetval;
150 		}
151 
152 		// provide unique ID
153 		ImplPrimitrive2DIDBlock(MediaPrimitive2D, PRIMITIVE2D_ID_MEDIAPRIMITIVE2D)
154 
155 	} // end of namespace primitive2d
156 } // end of namespace drawinglayer
157 
158 //////////////////////////////////////////////////////////////////////////////
159 // eof
160