xref: /trunk/main/drawinglayer/source/primitive2d/wallpaperprimitive2d.cxx (revision 4e007b9d0c24063a03864180ae5a35a0d455c4b7)
1464702f4SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3464702f4SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4464702f4SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5464702f4SAndrew Rist  * distributed with this work for additional information
6464702f4SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7464702f4SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8464702f4SAndrew Rist  * "License"); you may not use this file except in compliance
9464702f4SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11464702f4SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13464702f4SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14464702f4SAndrew Rist  * software distributed under the License is distributed on an
15464702f4SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16464702f4SAndrew Rist  * KIND, either express or implied.  See the License for the
17464702f4SAndrew Rist  * specific language governing permissions and limitations
18464702f4SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20464702f4SAndrew Rist  *************************************************************/
21464702f4SAndrew Rist 
22cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
23cdf0e10cSrcweir #include "precompiled_drawinglayer.hxx"
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include <drawinglayer/primitive2d/wallpaperprimitive2d.hxx>
26cdf0e10cSrcweir #include <drawinglayer/primitive2d/bitmapprimitive2d.hxx>
27cdf0e10cSrcweir #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
28035a2f44SArmin Le Grand #include <drawinglayer/primitive2d/fillgraphicprimitive2d.hxx>
29cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygontools.hxx>
30cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygon.hxx>
31cdf0e10cSrcweir #include <drawinglayer/primitive2d/maskprimitive2d.hxx>
32035a2f44SArmin Le Grand #include <basegfx/matrix/b2dhommatrixtools.hxx>
33035a2f44SArmin Le Grand #include <vcl/graph.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir namespace drawinglayer
36cdf0e10cSrcweir {
37cdf0e10cSrcweir     namespace primitive2d
38cdf0e10cSrcweir     {
create2DDecomposition(const geometry::ViewInformation2D &) const39cdf0e10cSrcweir         Primitive2DSequence WallpaperBitmapPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& /*rViewInformation*/) const
40cdf0e10cSrcweir         {
41cdf0e10cSrcweir             Primitive2DSequence aRetval;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir             if(!getLocalObjectRange().isEmpty() && !getBitmapEx().IsEmpty())
44cdf0e10cSrcweir             {
45cdf0e10cSrcweir                 // get bitmap PIXEL size
46cdf0e10cSrcweir                 const Size& rPixelSize = getBitmapEx().GetSizePixel();
47cdf0e10cSrcweir 
48cdf0e10cSrcweir                 if(rPixelSize.Width() > 0 && rPixelSize.Height() > 0)
49cdf0e10cSrcweir                 {
50cdf0e10cSrcweir                     if(WALLPAPER_SCALE == getWallpaperStyle())
51cdf0e10cSrcweir                     {
52cdf0e10cSrcweir                         // shortcut for scale; use simple BitmapPrimitive2D
53cdf0e10cSrcweir                         basegfx::B2DHomMatrix aObjectTransform;
54cdf0e10cSrcweir 
55cdf0e10cSrcweir                         aObjectTransform.set(0, 0, getLocalObjectRange().getWidth());
56cdf0e10cSrcweir                         aObjectTransform.set(1, 1, getLocalObjectRange().getHeight());
57cdf0e10cSrcweir                         aObjectTransform.set(0, 2, getLocalObjectRange().getMinX());
58cdf0e10cSrcweir                         aObjectTransform.set(1, 2, getLocalObjectRange().getMinY());
59cdf0e10cSrcweir 
60cdf0e10cSrcweir                         Primitive2DReference xReference(
61cdf0e10cSrcweir                             new BitmapPrimitive2D(
62cdf0e10cSrcweir                                 getBitmapEx(),
63cdf0e10cSrcweir                                 aObjectTransform));
64cdf0e10cSrcweir 
65cdf0e10cSrcweir                         aRetval = Primitive2DSequence(&xReference, 1);
66cdf0e10cSrcweir                     }
67cdf0e10cSrcweir                     else
68cdf0e10cSrcweir                     {
69cdf0e10cSrcweir                         // transform to logic size
70cdf0e10cSrcweir                         basegfx::B2DHomMatrix aInverseViewTransformation(getViewTransformation());
71cdf0e10cSrcweir                         aInverseViewTransformation.invert();
72cdf0e10cSrcweir                         basegfx::B2DVector aLogicSize(rPixelSize.Width(), rPixelSize.Height());
73cdf0e10cSrcweir                         aLogicSize = aInverseViewTransformation * aLogicSize;
74cdf0e10cSrcweir 
75*4e007b9dSmseidel                         // apply layout
76cdf0e10cSrcweir                         basegfx::B2DPoint aTargetTopLeft(getLocalObjectRange().getMinimum());
77cdf0e10cSrcweir                         bool bUseTargetTopLeft(true);
78cdf0e10cSrcweir                         bool bNeedsClipping(false);
79cdf0e10cSrcweir 
80cdf0e10cSrcweir                         switch(getWallpaperStyle())
81cdf0e10cSrcweir                         {
82cdf0e10cSrcweir                             default: //case WALLPAPER_TILE :, also WALLPAPER_NULL and WALLPAPER_APPLICATIONGRADIENT
83cdf0e10cSrcweir                             {
84cdf0e10cSrcweir                                 bUseTargetTopLeft = false;
85cdf0e10cSrcweir                                 break;
86cdf0e10cSrcweir                             }
87cdf0e10cSrcweir                             case WALLPAPER_SCALE :
88cdf0e10cSrcweir                             {
89cdf0e10cSrcweir                                 // handled by shortcut above
90cdf0e10cSrcweir                                 break;
91cdf0e10cSrcweir                             }
92cdf0e10cSrcweir                             case WALLPAPER_TOPLEFT :
93cdf0e10cSrcweir                             {
94cdf0e10cSrcweir                                 // nothing to do
95cdf0e10cSrcweir                                 break;
96cdf0e10cSrcweir                             }
97cdf0e10cSrcweir                             case WALLPAPER_TOP :
98cdf0e10cSrcweir                             {
99cdf0e10cSrcweir                                 const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
100cdf0e10cSrcweir                                 aTargetTopLeft.setX(aCenter.getX() - (aLogicSize.getX() * 0.5));
101cdf0e10cSrcweir                                 break;
102cdf0e10cSrcweir                             }
103cdf0e10cSrcweir                             case WALLPAPER_TOPRIGHT :
104cdf0e10cSrcweir                             {
105cdf0e10cSrcweir                                 aTargetTopLeft.setX(getLocalObjectRange().getMaxX() - aLogicSize.getX());
106cdf0e10cSrcweir                                 break;
107cdf0e10cSrcweir                             }
108cdf0e10cSrcweir                             case WALLPAPER_LEFT :
109cdf0e10cSrcweir                             {
110cdf0e10cSrcweir                                 const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
111cdf0e10cSrcweir                                 aTargetTopLeft.setY(aCenter.getY() - (aLogicSize.getY() * 0.5));
112cdf0e10cSrcweir                                 break;
113cdf0e10cSrcweir                             }
114cdf0e10cSrcweir                             case WALLPAPER_CENTER :
115cdf0e10cSrcweir                             {
116cdf0e10cSrcweir                                 const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
117cdf0e10cSrcweir                                 aTargetTopLeft = aCenter - (aLogicSize * 0.5);
118cdf0e10cSrcweir                                 break;
119cdf0e10cSrcweir                             }
120cdf0e10cSrcweir                             case WALLPAPER_RIGHT :
121cdf0e10cSrcweir                             {
122cdf0e10cSrcweir                                 const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
123cdf0e10cSrcweir                                 aTargetTopLeft.setX(getLocalObjectRange().getMaxX() - aLogicSize.getX());
124cdf0e10cSrcweir                                 aTargetTopLeft.setY(aCenter.getY() - (aLogicSize.getY() * 0.5));
125cdf0e10cSrcweir                                 break;
126cdf0e10cSrcweir                             }
127cdf0e10cSrcweir                             case WALLPAPER_BOTTOMLEFT :
128cdf0e10cSrcweir                             {
129cdf0e10cSrcweir                                 aTargetTopLeft.setY(getLocalObjectRange().getMaxY() - aLogicSize.getY());
130cdf0e10cSrcweir                                 break;
131cdf0e10cSrcweir                             }
132cdf0e10cSrcweir                             case WALLPAPER_BOTTOM :
133cdf0e10cSrcweir                             {
134cdf0e10cSrcweir                                 const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
135cdf0e10cSrcweir                                 aTargetTopLeft.setX(aCenter.getX() - (aLogicSize.getX() * 0.5));
136cdf0e10cSrcweir                                 aTargetTopLeft.setY(getLocalObjectRange().getMaxY() - aLogicSize.getY());
137cdf0e10cSrcweir                                 break;
138cdf0e10cSrcweir                             }
139cdf0e10cSrcweir                             case WALLPAPER_BOTTOMRIGHT :
140cdf0e10cSrcweir                             {
141cdf0e10cSrcweir                                 aTargetTopLeft = getLocalObjectRange().getMaximum() - aLogicSize;
142cdf0e10cSrcweir                                 break;
143cdf0e10cSrcweir                             }
144cdf0e10cSrcweir                         }
145cdf0e10cSrcweir 
146cdf0e10cSrcweir                         if(bUseTargetTopLeft)
147cdf0e10cSrcweir                         {
148cdf0e10cSrcweir                             // fill target range
149cdf0e10cSrcweir                             const basegfx::B2DRange aTargetRange(aTargetTopLeft, aTargetTopLeft + aLogicSize);
150cdf0e10cSrcweir 
151cdf0e10cSrcweir                             // create aligned, single BitmapPrimitive2D
152cdf0e10cSrcweir                             basegfx::B2DHomMatrix aObjectTransform;
153cdf0e10cSrcweir 
154cdf0e10cSrcweir                             aObjectTransform.set(0, 0, aTargetRange.getWidth());
155cdf0e10cSrcweir                             aObjectTransform.set(1, 1, aTargetRange.getHeight());
156cdf0e10cSrcweir                             aObjectTransform.set(0, 2, aTargetRange.getMinX());
157cdf0e10cSrcweir                             aObjectTransform.set(1, 2, aTargetRange.getMinY());
158cdf0e10cSrcweir 
159cdf0e10cSrcweir                             Primitive2DReference xReference(
160cdf0e10cSrcweir                                 new BitmapPrimitive2D(
161cdf0e10cSrcweir                                     getBitmapEx(),
162cdf0e10cSrcweir                                     aObjectTransform));
163cdf0e10cSrcweir                             aRetval = Primitive2DSequence(&xReference, 1);
164cdf0e10cSrcweir 
165cdf0e10cSrcweir                             // clip when not completely inside object range
166cdf0e10cSrcweir                             bNeedsClipping = !getLocalObjectRange().isInside(aTargetRange);
167cdf0e10cSrcweir                         }
168cdf0e10cSrcweir                         else
169cdf0e10cSrcweir                         {
170cdf0e10cSrcweir                             // WALLPAPER_TILE, WALLPAPER_NULL, WALLPAPER_APPLICATIONGRADIENT
171cdf0e10cSrcweir                             // convert to relative positions
172cdf0e10cSrcweir                             const basegfx::B2DVector aRelativeSize(
173cdf0e10cSrcweir                                 aLogicSize.getX() / (getLocalObjectRange().getWidth() ? getLocalObjectRange().getWidth() : 1.0),
174cdf0e10cSrcweir                                 aLogicSize.getY() / (getLocalObjectRange().getHeight() ? getLocalObjectRange().getHeight() : 1.0));
175cdf0e10cSrcweir                             basegfx::B2DPoint aRelativeTopLeft(0.0, 0.0);
176cdf0e10cSrcweir 
177cdf0e10cSrcweir                             if(WALLPAPER_TILE != getWallpaperStyle())
178cdf0e10cSrcweir                             {
179cdf0e10cSrcweir                                 aRelativeTopLeft.setX(0.5 - aRelativeSize.getX());
180cdf0e10cSrcweir                                 aRelativeTopLeft.setY(0.5 - aRelativeSize.getY());
181cdf0e10cSrcweir                             }
182cdf0e10cSrcweir 
183035a2f44SArmin Le Grand                             // prepare FillGraphicAttribute
184035a2f44SArmin Le Grand                             const attribute::FillGraphicAttribute aFillGraphicAttribute(
185035a2f44SArmin Le Grand                                 Graphic(getBitmapEx()),
186035a2f44SArmin Le Grand                                 basegfx::B2DRange(aRelativeTopLeft, aRelativeTopLeft+ aRelativeSize),
187cdf0e10cSrcweir                                 true);
188cdf0e10cSrcweir 
189cdf0e10cSrcweir                             // create ObjectTransform
190035a2f44SArmin Le Grand                             const basegfx::B2DHomMatrix aObjectTransform(
191035a2f44SArmin Le Grand                                 basegfx::tools::createScaleTranslateB2DHomMatrix(
192035a2f44SArmin Le Grand                                     getLocalObjectRange().getRange(),
193035a2f44SArmin Le Grand                                     getLocalObjectRange().getMinimum()));
194cdf0e10cSrcweir 
195cdf0e10cSrcweir                             // create FillBitmapPrimitive
196cdf0e10cSrcweir                             const drawinglayer::primitive2d::Primitive2DReference xFillBitmap(
197035a2f44SArmin Le Grand                                 new drawinglayer::primitive2d::FillGraphicPrimitive2D(
198cdf0e10cSrcweir                                     aObjectTransform,
199035a2f44SArmin Le Grand                                     aFillGraphicAttribute));
200cdf0e10cSrcweir                             aRetval = Primitive2DSequence(&xFillBitmap, 1);
201cdf0e10cSrcweir 
202cdf0e10cSrcweir                             // always embed tiled fill to clipping
203cdf0e10cSrcweir                             bNeedsClipping = true;
204cdf0e10cSrcweir                         }
205cdf0e10cSrcweir 
206cdf0e10cSrcweir                         if(bNeedsClipping)
207cdf0e10cSrcweir                         {
208cdf0e10cSrcweir                             // embed to clipping; this is necessary for tiled fills
209cdf0e10cSrcweir                             const basegfx::B2DPolyPolygon aPolyPolygon(
210cdf0e10cSrcweir                                 basegfx::tools::createPolygonFromRect(getLocalObjectRange()));
211cdf0e10cSrcweir                             const drawinglayer::primitive2d::Primitive2DReference xClippedFill(
212cdf0e10cSrcweir                                 new drawinglayer::primitive2d::MaskPrimitive2D(
213cdf0e10cSrcweir                                     aPolyPolygon,
214cdf0e10cSrcweir                                     aRetval));
215cdf0e10cSrcweir                             aRetval = Primitive2DSequence(&xClippedFill, 1);
216cdf0e10cSrcweir                         }
217cdf0e10cSrcweir                     }
218cdf0e10cSrcweir                 }
219cdf0e10cSrcweir             }
220cdf0e10cSrcweir 
221cdf0e10cSrcweir             return aRetval;
222cdf0e10cSrcweir         }
223cdf0e10cSrcweir 
WallpaperBitmapPrimitive2D(const basegfx::B2DRange & rObjectRange,const BitmapEx & rBitmapEx,WallpaperStyle eWallpaperStyle)224cdf0e10cSrcweir         WallpaperBitmapPrimitive2D::WallpaperBitmapPrimitive2D(
225cdf0e10cSrcweir             const basegfx::B2DRange& rObjectRange,
226cdf0e10cSrcweir             const BitmapEx& rBitmapEx,
227cdf0e10cSrcweir             WallpaperStyle eWallpaperStyle)
228cdf0e10cSrcweir         :   ViewTransformationDependentPrimitive2D(),
229cdf0e10cSrcweir             maObjectRange(rObjectRange),
230cdf0e10cSrcweir             maBitmapEx(rBitmapEx),
231cdf0e10cSrcweir             meWallpaperStyle(eWallpaperStyle)
232cdf0e10cSrcweir         {
233cdf0e10cSrcweir         }
234cdf0e10cSrcweir 
operator ==(const BasePrimitive2D & rPrimitive) const235cdf0e10cSrcweir         bool WallpaperBitmapPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
236cdf0e10cSrcweir         {
237cdf0e10cSrcweir             if(ViewTransformationDependentPrimitive2D::operator==(rPrimitive))
238cdf0e10cSrcweir             {
239cdf0e10cSrcweir                 const WallpaperBitmapPrimitive2D& rCompare = (WallpaperBitmapPrimitive2D&)rPrimitive;
240cdf0e10cSrcweir 
241cdf0e10cSrcweir                 return (getLocalObjectRange() == rCompare.getLocalObjectRange()
242cdf0e10cSrcweir                     && getBitmapEx() == rCompare.getBitmapEx()
243cdf0e10cSrcweir                     && getWallpaperStyle() == rCompare.getWallpaperStyle());
244cdf0e10cSrcweir             }
245cdf0e10cSrcweir 
246cdf0e10cSrcweir             return false;
247cdf0e10cSrcweir         }
248cdf0e10cSrcweir 
getB2DRange(const geometry::ViewInformation2D &) const249cdf0e10cSrcweir         basegfx::B2DRange WallpaperBitmapPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation*/) const
250cdf0e10cSrcweir         {
251cdf0e10cSrcweir             return getLocalObjectRange();
252cdf0e10cSrcweir         }
253cdf0e10cSrcweir 
254cdf0e10cSrcweir         // provide unique ID
255cdf0e10cSrcweir         ImplPrimitrive2DIDBlock(WallpaperBitmapPrimitive2D, PRIMITIVE2D_ID_WALLPAPERBITMAPPRIMITIVE2D)
256cdf0e10cSrcweir     } // end of namespace primitive2d
257cdf0e10cSrcweir } // end of namespace drawinglayer
258cdf0e10cSrcweir 
259*4e007b9dSmseidel /* vim: set noet sw=4 ts=4: */
260