/src/libreoffice/drawinglayer/source/primitive2d/wallpaperprimitive2d.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
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/. |
8 | | * |
9 | | * This file incorporates work covered by the following license notice: |
10 | | * |
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 . |
18 | | */ |
19 | | |
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 | | |
30 | | |
31 | | namespace drawinglayer::primitive2d |
32 | | { |
33 | | Primitive2DReference WallpaperBitmapPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& /*rViewInformation*/) const |
34 | 0 | { |
35 | |
|
36 | 0 | if(getLocalObjectRange().isEmpty() || getBitmap().IsEmpty()) |
37 | 0 | return nullptr; |
38 | | |
39 | 0 | Primitive2DReference aRetval; |
40 | | |
41 | | // get bitmap PIXEL size |
42 | 0 | const Size aPixelSize = getBitmap().GetSizePixel(); |
43 | |
|
44 | 0 | if(aPixelSize.Width() <= 0 || aPixelSize.Height() <= 0) |
45 | 0 | return nullptr; |
46 | | |
47 | 0 | if(WallpaperStyle::Scale == getWallpaperStyle()) |
48 | 0 | { |
49 | | // shortcut for scale; use simple BitmapPrimitive2D |
50 | 0 | basegfx::B2DHomMatrix aObjectTransform; |
51 | |
|
52 | 0 | aObjectTransform.set(0, 0, getLocalObjectRange().getWidth()); |
53 | 0 | aObjectTransform.set(1, 1, getLocalObjectRange().getHeight()); |
54 | 0 | aObjectTransform.set(0, 2, getLocalObjectRange().getMinX()); |
55 | 0 | aObjectTransform.set(1, 2, getLocalObjectRange().getMinY()); |
56 | |
|
57 | 0 | aRetval.set( |
58 | 0 | new BitmapPrimitive2D( |
59 | 0 | getBitmap(), |
60 | 0 | aObjectTransform)); |
61 | 0 | } |
62 | 0 | else |
63 | 0 | { |
64 | | // transform to logic size |
65 | 0 | basegfx::B2DHomMatrix aInverseViewTransformation(getViewTransformation()); |
66 | 0 | aInverseViewTransformation.invert(); |
67 | 0 | basegfx::B2DVector aLogicSize(aPixelSize.Width(), aPixelSize.Height()); |
68 | 0 | aLogicSize = aInverseViewTransformation * aLogicSize; |
69 | | |
70 | | // apply layout |
71 | 0 | basegfx::B2DPoint aTargetTopLeft(getLocalObjectRange().getMinimum()); |
72 | 0 | bool bUseTargetTopLeft(true); |
73 | 0 | bool bNeedsClipping(false); |
74 | |
|
75 | 0 | switch(getWallpaperStyle()) |
76 | 0 | { |
77 | 0 | default: //case WallpaperStyle::Tile :, also WallpaperStyle::NONE and WallpaperStyle::ApplicationGradient |
78 | 0 | { |
79 | 0 | bUseTargetTopLeft = false; |
80 | 0 | break; |
81 | 0 | } |
82 | 0 | case WallpaperStyle::Scale : |
83 | 0 | { |
84 | | // handled by shortcut above |
85 | 0 | break; |
86 | 0 | } |
87 | 0 | case WallpaperStyle::TopLeft : |
88 | 0 | { |
89 | | // nothing to do |
90 | 0 | break; |
91 | 0 | } |
92 | 0 | case WallpaperStyle::Top : |
93 | 0 | { |
94 | 0 | const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter()); |
95 | 0 | aTargetTopLeft.setX(aCenter.getX() - (aLogicSize.getX() * 0.5)); |
96 | 0 | break; |
97 | 0 | } |
98 | 0 | case WallpaperStyle::TopRight : |
99 | 0 | { |
100 | 0 | aTargetTopLeft.setX(getLocalObjectRange().getMaxX() - aLogicSize.getX()); |
101 | 0 | break; |
102 | 0 | } |
103 | 0 | case WallpaperStyle::Left : |
104 | 0 | { |
105 | 0 | const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter()); |
106 | 0 | aTargetTopLeft.setY(aCenter.getY() - (aLogicSize.getY() * 0.5)); |
107 | 0 | break; |
108 | 0 | } |
109 | 0 | case WallpaperStyle::Center : |
110 | 0 | { |
111 | 0 | const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter()); |
112 | 0 | aTargetTopLeft = aCenter - (aLogicSize * 0.5); |
113 | 0 | break; |
114 | 0 | } |
115 | 0 | case WallpaperStyle::Right : |
116 | 0 | { |
117 | 0 | const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter()); |
118 | 0 | aTargetTopLeft.setX(getLocalObjectRange().getMaxX() - aLogicSize.getX()); |
119 | 0 | aTargetTopLeft.setY(aCenter.getY() - (aLogicSize.getY() * 0.5)); |
120 | 0 | break; |
121 | 0 | } |
122 | 0 | case WallpaperStyle::BottomLeft : |
123 | 0 | { |
124 | 0 | aTargetTopLeft.setY(getLocalObjectRange().getMaxY() - aLogicSize.getY()); |
125 | 0 | break; |
126 | 0 | } |
127 | 0 | case WallpaperStyle::Bottom : |
128 | 0 | { |
129 | 0 | const basegfx::B2DPoint aCenter(getLocalObjectRange().getCenter()); |
130 | 0 | aTargetTopLeft.setX(aCenter.getX() - (aLogicSize.getX() * 0.5)); |
131 | 0 | aTargetTopLeft.setY(getLocalObjectRange().getMaxY() - aLogicSize.getY()); |
132 | 0 | break; |
133 | 0 | } |
134 | 0 | case WallpaperStyle::BottomRight : |
135 | 0 | { |
136 | 0 | aTargetTopLeft = getLocalObjectRange().getMaximum() - aLogicSize; |
137 | 0 | break; |
138 | 0 | } |
139 | 0 | } |
140 | | |
141 | 0 | if(bUseTargetTopLeft) |
142 | 0 | { |
143 | | // fill target range |
144 | 0 | const basegfx::B2DRange aTargetRange(aTargetTopLeft, aTargetTopLeft + aLogicSize); |
145 | | |
146 | | // create aligned, single BitmapPrimitive2D |
147 | 0 | basegfx::B2DHomMatrix aObjectTransform; |
148 | |
|
149 | 0 | aObjectTransform.set(0, 0, aTargetRange.getWidth()); |
150 | 0 | aObjectTransform.set(1, 1, aTargetRange.getHeight()); |
151 | 0 | aObjectTransform.set(0, 2, aTargetRange.getMinX()); |
152 | 0 | aObjectTransform.set(1, 2, aTargetRange.getMinY()); |
153 | |
|
154 | 0 | aRetval.set( |
155 | 0 | new BitmapPrimitive2D( |
156 | 0 | getBitmap(), |
157 | 0 | aObjectTransform)); |
158 | | |
159 | | // clip when not completely inside object range |
160 | 0 | bNeedsClipping = !getLocalObjectRange().isInside(aTargetRange); |
161 | 0 | } |
162 | 0 | else |
163 | 0 | { |
164 | | // WallpaperStyle::Tile, WallpaperStyle::NONE, WallpaperStyle::ApplicationGradient |
165 | | // convert to relative positions |
166 | 0 | const basegfx::B2DVector aRelativeSize( |
167 | 0 | aLogicSize.getX() / (getLocalObjectRange().getWidth() ? getLocalObjectRange().getWidth() : 1.0), |
168 | 0 | aLogicSize.getY() / (getLocalObjectRange().getHeight() ? getLocalObjectRange().getHeight() : 1.0)); |
169 | 0 | basegfx::B2DPoint aRelativeTopLeft(0.0, 0.0); |
170 | |
|
171 | 0 | if(WallpaperStyle::Tile != getWallpaperStyle()) |
172 | 0 | { |
173 | 0 | aRelativeTopLeft.setX(0.5 - aRelativeSize.getX()); |
174 | 0 | aRelativeTopLeft.setY(0.5 - aRelativeSize.getY()); |
175 | 0 | } |
176 | | |
177 | | // prepare FillGraphicAttribute |
178 | 0 | const attribute::FillGraphicAttribute aFillGraphicAttribute( |
179 | 0 | Graphic(getBitmap()), |
180 | 0 | basegfx::B2DRange(aRelativeTopLeft, aRelativeTopLeft+ aRelativeSize), |
181 | 0 | true); |
182 | | |
183 | | // create ObjectTransform |
184 | 0 | const basegfx::B2DHomMatrix aObjectTransform( |
185 | 0 | basegfx::utils::createScaleTranslateB2DHomMatrix( |
186 | 0 | getLocalObjectRange().getRange(), |
187 | 0 | getLocalObjectRange().getMinimum())); |
188 | | |
189 | | // create FillBitmapPrimitive |
190 | 0 | aRetval.set( |
191 | 0 | new drawinglayer::primitive2d::FillGraphicPrimitive2D( |
192 | 0 | aObjectTransform, |
193 | 0 | aFillGraphicAttribute)); |
194 | | |
195 | | // always embed tiled fill to clipping |
196 | 0 | bNeedsClipping = true; |
197 | 0 | } |
198 | |
|
199 | 0 | if(bNeedsClipping) |
200 | 0 | { |
201 | | // embed to clipping; this is necessary for tiled fills |
202 | 0 | basegfx::B2DPolyPolygon aPolyPolygon( |
203 | 0 | basegfx::utils::createPolygonFromRect(getLocalObjectRange())); |
204 | 0 | const drawinglayer::primitive2d::Primitive2DReference xClippedFill( |
205 | 0 | new drawinglayer::primitive2d::MaskPrimitive2D( |
206 | 0 | std::move(aPolyPolygon), |
207 | 0 | { aRetval })); |
208 | 0 | aRetval = xClippedFill; |
209 | 0 | } |
210 | 0 | } |
211 | | |
212 | 0 | return aRetval; |
213 | 0 | } |
214 | | |
215 | | WallpaperBitmapPrimitive2D::WallpaperBitmapPrimitive2D( |
216 | | const basegfx::B2DRange& rObjectRange, |
217 | | const Bitmap& rBitmap, |
218 | | WallpaperStyle eWallpaperStyle) |
219 | 0 | : maObjectRange(rObjectRange), |
220 | 0 | maBitmap(rBitmap), |
221 | 0 | meWallpaperStyle(eWallpaperStyle) |
222 | 0 | { |
223 | 0 | } |
224 | | |
225 | | bool WallpaperBitmapPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const |
226 | 0 | { |
227 | 0 | if(ViewTransformationDependentPrimitive2D::operator==(rPrimitive)) |
228 | 0 | { |
229 | 0 | const WallpaperBitmapPrimitive2D& rCompare = static_cast<const WallpaperBitmapPrimitive2D&>(rPrimitive); |
230 | |
|
231 | 0 | return (getLocalObjectRange() == rCompare.getLocalObjectRange() |
232 | 0 | && getBitmap() == rCompare.getBitmap() |
233 | 0 | && getWallpaperStyle() == rCompare.getWallpaperStyle()); |
234 | 0 | } |
235 | | |
236 | 0 | return false; |
237 | 0 | } |
238 | | |
239 | | basegfx::B2DRange WallpaperBitmapPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation*/) const |
240 | 0 | { |
241 | 0 | return getLocalObjectRange(); |
242 | 0 | } |
243 | | |
244 | | // provide unique ID |
245 | | sal_uInt32 WallpaperBitmapPrimitive2D::getPrimitive2DID() const |
246 | 0 | { |
247 | 0 | return PRIMITIVE2D_ID_WALLPAPERBITMAPPRIMITIVE2D; |
248 | 0 | } |
249 | | |
250 | | } // end of namespace |
251 | | |
252 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |