tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / drawinglayer / source / primitive2d / wallpaperprimitive2d.cxx
blob9e3af10415ddeb5b5a0b9ffb90cc8a841ccb3a3b
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 <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>
29 #include <toolkit/helper/vclunohelper.hxx>
32 namespace drawinglayer::primitive2d
34 Primitive2DReference WallpaperBitmapPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& /*rViewInformation*/) const
37 if(getLocalObjectRange().isEmpty() || getBitmapEx().IsEmpty())
38 return nullptr;
40 Primitive2DReference aRetval;
42 // get bitmap PIXEL size
43 const Size& rPixelSize = getBitmapEx().GetSizePixel();
45 if(rPixelSize.Width() <= 0 || rPixelSize.Height() <= 0)
46 return nullptr;
48 if(WallpaperStyle::Scale == getWallpaperStyle())
50 // shortcut for scale; use simple BitmapPrimitive2D
51 basegfx::B2DHomMatrix aObjectTransform;
53 aObjectTransform.set(0, 0, getLocalObjectRange().getWidth());
54 aObjectTransform.set(1, 1, getLocalObjectRange().getHeight());
55 aObjectTransform.set(0, 2, getLocalObjectRange().getMinX());
56 aObjectTransform.set(1, 2, getLocalObjectRange().getMinY());
58 aRetval.set(
59 new BitmapPrimitive2D(
60 getBitmapEx(),
61 aObjectTransform));
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 aRetval.set(
156 new BitmapPrimitive2D(
157 getBitmapEx(),
158 aObjectTransform));
160 // clip when not completely inside object range
161 bNeedsClipping = !getLocalObjectRange().isInside(aTargetRange);
163 else
165 // WallpaperStyle::Tile, WallpaperStyle::NONE, WallpaperStyle::ApplicationGradient
166 // convert to relative positions
167 const basegfx::B2DVector aRelativeSize(
168 aLogicSize.getX() / (getLocalObjectRange().getWidth() ? getLocalObjectRange().getWidth() : 1.0),
169 aLogicSize.getY() / (getLocalObjectRange().getHeight() ? getLocalObjectRange().getHeight() : 1.0));
170 basegfx::B2DPoint aRelativeTopLeft(0.0, 0.0);
172 if(WallpaperStyle::Tile != getWallpaperStyle())
174 aRelativeTopLeft.setX(0.5 - aRelativeSize.getX());
175 aRelativeTopLeft.setY(0.5 - aRelativeSize.getY());
178 // prepare FillGraphicAttribute
179 const attribute::FillGraphicAttribute aFillGraphicAttribute(
180 Graphic(getBitmapEx()),
181 basegfx::B2DRange(aRelativeTopLeft, aRelativeTopLeft+ aRelativeSize),
182 true);
184 // create ObjectTransform
185 const basegfx::B2DHomMatrix aObjectTransform(
186 basegfx::utils::createScaleTranslateB2DHomMatrix(
187 getLocalObjectRange().getRange(),
188 getLocalObjectRange().getMinimum()));
190 // create FillBitmapPrimitive
191 aRetval.set(
192 new drawinglayer::primitive2d::FillGraphicPrimitive2D(
193 aObjectTransform,
194 aFillGraphicAttribute));
196 // always embed tiled fill to clipping
197 bNeedsClipping = true;
200 if(bNeedsClipping)
202 // embed to clipping; this is necessary for tiled fills
203 basegfx::B2DPolyPolygon aPolyPolygon(
204 basegfx::utils::createPolygonFromRect(getLocalObjectRange()));
205 const drawinglayer::primitive2d::Primitive2DReference xClippedFill(
206 new drawinglayer::primitive2d::MaskPrimitive2D(
207 std::move(aPolyPolygon),
208 { aRetval }));
209 aRetval = xClippedFill;
213 return aRetval;
216 WallpaperBitmapPrimitive2D::WallpaperBitmapPrimitive2D(
217 const basegfx::B2DRange& rObjectRange,
218 const BitmapEx& rBitmapEx,
219 WallpaperStyle eWallpaperStyle)
220 : maObjectRange(rObjectRange),
221 maBitmapEx(rBitmapEx),
222 meWallpaperStyle(eWallpaperStyle)
226 bool WallpaperBitmapPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
228 if(ViewTransformationDependentPrimitive2D::operator==(rPrimitive))
230 const WallpaperBitmapPrimitive2D& rCompare = static_cast<const WallpaperBitmapPrimitive2D&>(rPrimitive);
232 return (getLocalObjectRange() == rCompare.getLocalObjectRange()
233 && getBitmapEx() == rCompare.getBitmapEx()
234 && getWallpaperStyle() == rCompare.getWallpaperStyle());
237 return false;
240 basegfx::B2DRange WallpaperBitmapPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation*/) const
242 return getLocalObjectRange();
245 // provide unique ID
246 sal_uInt32 WallpaperBitmapPrimitive2D::getPrimitive2DID() const
248 return PRIMITIVE2D_ID_WALLPAPERBITMAPPRIMITIVE2D;
251 } // end of namespace
253 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */