Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / drawinglayer / source / primitive2d / wallpaperprimitive2d.cxx
blob6bab9e92255f14be42b1fadcaacc59a3a8272776
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <drawinglayer/primitive2d/wallpaperprimitive2d.hxx>
21 #include <drawinglayer/primitive2d/bitmapprimitive2d.hxx>
22 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
23 #include <drawinglayer/primitive2d/fillgraphicprimitive2d.hxx>
24 #include <basegfx/polygon/b2dpolygontools.hxx>
25 #include <basegfx/polygon/b2dpolygon.hxx>
26 #include <drawinglayer/primitive2d/maskprimitive2d.hxx>
27 #include <basegfx/matrix/b2dhommatrixtools.hxx>
28 #include <vcl/graph.hxx>
31 namespace drawinglayer
33 namespace primitive2d
35 void WallpaperBitmapPrimitive2D::create2DDecomposition(Primitive2DContainer& rContainer, const geometry::ViewInformation2D& /*rViewInformation*/) const
37 Primitive2DReference aRetval;
39 if(!getLocalObjectRange().isEmpty() && !getBitmapEx().IsEmpty())
41 // get bitmap PIXEL size
42 const Size& rPixelSize = getBitmapEx().GetSizePixel();
44 if(rPixelSize.Width() > 0 && rPixelSize.Height() > 0)
46 if(WallpaperStyle::Scale == getWallpaperStyle())
48 // shortcut for scale; use simple BitmapPrimitive2D
49 basegfx::B2DHomMatrix aObjectTransform;
51 aObjectTransform.set(0, 0, getLocalObjectRange().getWidth());
52 aObjectTransform.set(1, 1, getLocalObjectRange().getHeight());
53 aObjectTransform.set(0, 2, getLocalObjectRange().getMinX());
54 aObjectTransform.set(1, 2, getLocalObjectRange().getMinY());
56 Primitive2DReference xReference(
57 new BitmapPrimitive2D(
58 getBitmapEx(),
59 aObjectTransform));
61 aRetval = xReference;
63 else
65 // transform to logic size
66 basegfx::B2DHomMatrix aInverseViewTransformation(getViewTransformation());
67 aInverseViewTransformation.invert();
68 basegfx::B2DVector aLogicSize(rPixelSize.Width(), rPixelSize.Height());
69 aLogicSize = aInverseViewTransformation * aLogicSize;
71 // apply layout
72 basegfx::B2DPoint aTargetTopLeft(getLocalObjectRange().getMinimum());
73 bool bUseTargetTopLeft(true);
74 bool bNeedsClipping(false);
76 switch(getWallpaperStyle())
78 default: //case WallpaperStyle::Tile :, also WallpaperStyle::NONE and WallpaperStyle::ApplicationGradient
80 bUseTargetTopLeft = false;
81 break;
83 case WallpaperStyle::Scale :
85 // handled by shortcut above
86 break;
88 case WallpaperStyle::TopLeft :
90 // nothing to do
91 break;
93 case WallpaperStyle::Top :
95 const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
96 aTargetTopLeft.setX(aCenter.getX() - (aLogicSize.getX() * 0.5));
97 break;
99 case WallpaperStyle::TopRight :
101 aTargetTopLeft.setX(getLocalObjectRange().getMaxX() - aLogicSize.getX());
102 break;
104 case WallpaperStyle::Left :
106 const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
107 aTargetTopLeft.setY(aCenter.getY() - (aLogicSize.getY() * 0.5));
108 break;
110 case WallpaperStyle::Center :
112 const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
113 aTargetTopLeft = aCenter - (aLogicSize * 0.5);
114 break;
116 case WallpaperStyle::Right :
118 const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
119 aTargetTopLeft.setX(getLocalObjectRange().getMaxX() - aLogicSize.getX());
120 aTargetTopLeft.setY(aCenter.getY() - (aLogicSize.getY() * 0.5));
121 break;
123 case WallpaperStyle::BottomLeft :
125 aTargetTopLeft.setY(getLocalObjectRange().getMaxY() - aLogicSize.getY());
126 break;
128 case WallpaperStyle::Bottom :
130 const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter());
131 aTargetTopLeft.setX(aCenter.getX() - (aLogicSize.getX() * 0.5));
132 aTargetTopLeft.setY(getLocalObjectRange().getMaxY() - aLogicSize.getY());
133 break;
135 case WallpaperStyle::BottomRight :
137 aTargetTopLeft = getLocalObjectRange().getMaximum() - aLogicSize;
138 break;
142 if(bUseTargetTopLeft)
144 // fill target range
145 const basegfx::B2DRange aTargetRange(aTargetTopLeft, aTargetTopLeft + aLogicSize);
147 // create aligned, single BitmapPrimitive2D
148 basegfx::B2DHomMatrix aObjectTransform;
150 aObjectTransform.set(0, 0, aTargetRange.getWidth());
151 aObjectTransform.set(1, 1, aTargetRange.getHeight());
152 aObjectTransform.set(0, 2, aTargetRange.getMinX());
153 aObjectTransform.set(1, 2, aTargetRange.getMinY());
155 Primitive2DReference xReference(
156 new BitmapPrimitive2D(
157 getBitmapEx(),
158 aObjectTransform));
159 aRetval = xReference;
161 // clip when not completely inside object range
162 bNeedsClipping = !getLocalObjectRange().isInside(aTargetRange);
164 else
166 // WallpaperStyle::Tile, WallpaperStyle::NONE, WallpaperStyle::ApplicationGradient
167 // convert to relative positions
168 const basegfx::B2DVector aRelativeSize(
169 aLogicSize.getX() / (getLocalObjectRange().getWidth() ? getLocalObjectRange().getWidth() : 1.0),
170 aLogicSize.getY() / (getLocalObjectRange().getHeight() ? getLocalObjectRange().getHeight() : 1.0));
171 basegfx::B2DPoint aRelativeTopLeft(0.0, 0.0);
173 if(WallpaperStyle::Tile != getWallpaperStyle())
175 aRelativeTopLeft.setX(0.5 - aRelativeSize.getX());
176 aRelativeTopLeft.setY(0.5 - aRelativeSize.getY());
179 // prepare FillGraphicAttribute
180 const attribute::FillGraphicAttribute aFillGraphicAttribute(
181 Graphic(getBitmapEx()),
182 basegfx::B2DRange(aRelativeTopLeft, aRelativeTopLeft+ aRelativeSize),
183 true);
185 // create ObjectTransform
186 const basegfx::B2DHomMatrix aObjectTransform(
187 basegfx::utils::createScaleTranslateB2DHomMatrix(
188 getLocalObjectRange().getRange(),
189 getLocalObjectRange().getMinimum()));
191 // create FillBitmapPrimitive
192 const drawinglayer::primitive2d::Primitive2DReference xFillBitmap(
193 new drawinglayer::primitive2d::FillGraphicPrimitive2D(
194 aObjectTransform,
195 aFillGraphicAttribute));
196 aRetval = xFillBitmap;
198 // always embed tiled fill to clipping
199 bNeedsClipping = true;
202 if(bNeedsClipping)
204 // embed to clipping; this is necessary for tiled fills
205 const basegfx::B2DPolyPolygon aPolyPolygon(
206 basegfx::utils::createPolygonFromRect(getLocalObjectRange()));
207 const drawinglayer::primitive2d::Primitive2DReference xClippedFill(
208 new drawinglayer::primitive2d::MaskPrimitive2D(
209 aPolyPolygon,
210 { aRetval }));
211 aRetval = xClippedFill;
217 if (aRetval.is())
218 rContainer.push_back(aRetval);
221 WallpaperBitmapPrimitive2D::WallpaperBitmapPrimitive2D(
222 const basegfx::B2DRange& rObjectRange,
223 const BitmapEx& rBitmapEx,
224 WallpaperStyle eWallpaperStyle)
225 : ViewTransformationDependentPrimitive2D(),
226 maObjectRange(rObjectRange),
227 maBitmapEx(rBitmapEx),
228 meWallpaperStyle(eWallpaperStyle)
232 bool WallpaperBitmapPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
234 if(ViewTransformationDependentPrimitive2D::operator==(rPrimitive))
236 const WallpaperBitmapPrimitive2D& rCompare = static_cast<const WallpaperBitmapPrimitive2D&>(rPrimitive);
238 return (getLocalObjectRange() == rCompare.getLocalObjectRange()
239 && getBitmapEx() == rCompare.getBitmapEx()
240 && getWallpaperStyle() == rCompare.getWallpaperStyle());
243 return false;
246 basegfx::B2DRange WallpaperBitmapPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation*/) const
248 return getLocalObjectRange();
251 // provide unique ID
252 ImplPrimitive2DIDBlock(WallpaperBitmapPrimitive2D, PRIMITIVE2D_ID_WALLPAPERBITMAPPRIMITIVE2D)
253 } // end of namespace primitive2d
254 } // end of namespace drawinglayer
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */