tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / drawinglayer / source / texture / texture3d.cxx
blob4cbcab9d1c5d42c43f8f9bcf7859dd2d7a2a5486
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 <sal/config.h>
22 #include <algorithm>
24 #include <texture/texture3d.hxx>
25 #include <vcl/BitmapReadAccess.hxx>
26 #include <vcl/BitmapTools.hxx>
27 #include <primitive3d/hatchtextureprimitive3d.hxx>
28 #include <sal/log.hxx>
29 #include <osl/diagnose.h>
31 namespace drawinglayer::texture
33 GeoTexSvxMono::GeoTexSvxMono(
34 const basegfx::BColor& rSingleColor,
35 double fOpacity)
36 : maSingleColor(rSingleColor),
37 mfOpacity(fOpacity)
41 bool GeoTexSvxMono::operator==(const GeoTexSvx& rGeoTexSvx) const
43 const GeoTexSvxMono* pCompare = dynamic_cast< const GeoTexSvxMono* >(&rGeoTexSvx);
45 return (pCompare
46 && maSingleColor == pCompare->maSingleColor
47 && mfOpacity == pCompare->mfOpacity);
50 void GeoTexSvxMono::modifyBColor(const basegfx::B2DPoint& /*rUV*/, basegfx::BColor& rBColor, double& /*rfOpacity*/) const
52 rBColor = maSingleColor;
55 void GeoTexSvxMono::modifyOpacity(const basegfx::B2DPoint& /*rUV*/, double& rfOpacity) const
57 rfOpacity = mfOpacity;
61 GeoTexSvxBitmapEx::GeoTexSvxBitmapEx(
62 const BitmapEx& rBitmapEx,
63 const basegfx::B2DRange& rRange)
64 : maBitmapEx(rBitmapEx),
65 maTopLeft(rRange.getMinimum()),
66 maSize(rRange.getRange()),
67 mfMulX(0.0),
68 mfMulY(0.0),
69 mbIsAlpha(maBitmapEx.IsAlpha())
71 if(vcl::bitmap::convertBitmap32To24Plus8(maBitmapEx,maBitmapEx))
72 mbIsAlpha = maBitmapEx.IsAlpha();
73 // #121194# Todo: use alpha channel, too (for 3d)
74 maBitmap = maBitmapEx.GetBitmap();
76 if(mbIsAlpha)
78 maTransparence = rBitmapEx.GetAlphaMask().GetBitmap();
79 mpReadTransparence = maTransparence;
82 if (!maBitmap.IsEmpty())
83 mpReadBitmap = maBitmap;
84 SAL_WARN_IF(!mpReadBitmap, "drawinglayer", "GeoTexSvxBitmapEx: Got no read access to Bitmap");
85 if (mpReadBitmap)
87 mfMulX = static_cast<double>(mpReadBitmap->Width()) / maSize.getX();
88 mfMulY = static_cast<double>(mpReadBitmap->Height()) / maSize.getY();
91 if(maSize.getX() <= 1.0)
93 maSize.setX(1.0);
96 if(maSize.getY() <= 1.0)
98 maSize.setY(1.0);
102 GeoTexSvxBitmapEx::~GeoTexSvxBitmapEx()
106 sal_uInt8 GeoTexSvxBitmapEx::impGetAlpha(sal_Int32 rX, sal_Int32 rY) const
108 if(mbIsAlpha)
110 OSL_ENSURE(mpReadTransparence, "OOps, transparence type Bitmap, but no read access created in the constructor (?)");
111 const BitmapColor aBitmapColor(mpReadTransparence->GetPixel(rY, rX));
112 return aBitmapColor.GetIndex();
114 return 0;
117 bool GeoTexSvxBitmapEx::impIsValid(const basegfx::B2DPoint& rUV, sal_Int32& rX, sal_Int32& rY) const
119 if(mpReadBitmap)
121 rX = static_cast<sal_Int32>((rUV.getX() - maTopLeft.getX()) * mfMulX);
123 if(rX >= 0 && rX < mpReadBitmap->Width())
125 rY = static_cast<sal_Int32>((rUV.getY() - maTopLeft.getY()) * mfMulY);
127 return (rY >= 0 && rY < mpReadBitmap->Height());
131 return false;
134 void GeoTexSvxBitmapEx::modifyBColor(const basegfx::B2DPoint& rUV, basegfx::BColor& rBColor, double& rfOpacity) const
136 sal_Int32 nX, nY;
138 if(impIsValid(rUV, nX, nY))
140 const double fConvertColor(1.0 / 255.0);
141 const BitmapColor aBMCol(mpReadBitmap->GetColor(nY, nX));
142 const basegfx::BColor aBSource(
143 static_cast<double>(aBMCol.GetRed()) * fConvertColor,
144 static_cast<double>(aBMCol.GetGreen()) * fConvertColor,
145 static_cast<double>(aBMCol.GetBlue()) * fConvertColor);
147 rBColor = aBSource;
149 if(mbIsAlpha)
151 // when we have alpha, make use of it
152 const sal_uInt8 aAlpha(impGetAlpha(nX, nY));
154 rfOpacity = (static_cast<double>(aAlpha) * (1.0 / 255.0));
156 else
158 rfOpacity = 1.0;
161 else
163 rfOpacity = 0.0;
167 void GeoTexSvxBitmapEx::modifyOpacity(const basegfx::B2DPoint& rUV, double& rfOpacity) const
169 sal_Int32 nX, nY;
171 if(impIsValid(rUV, nX, nY))
173 if(mbIsAlpha)
175 // this texture has an alpha part, use it
176 const sal_uInt8 aAlpha(impGetAlpha(nX, nY));
177 const double fNewOpacity(static_cast<double>(aAlpha) * (1.0 / 255.0));
179 rfOpacity = 1.0 - ((1.0 - fNewOpacity) * (1.0 - rfOpacity));
181 else
183 // this texture is a color bitmap used as transparence map
184 const BitmapColor aBMCol(mpReadBitmap->GetColor(nY, nX));
185 const Color aColor(aBMCol.GetRed(), aBMCol.GetGreen(), aBMCol.GetBlue());
187 rfOpacity = (static_cast<double>(0xff - aColor.GetLuminance()) * (1.0 / 255.0));
190 else
192 rfOpacity = 0.0;
197 basegfx::B2DPoint GeoTexSvxBitmapExTiled::impGetCorrected(const basegfx::B2DPoint& rUV) const
199 double fX(rUV.getX() - maTopLeft.getX());
200 double fY(rUV.getY() - maTopLeft.getY());
202 if(mbUseOffsetX)
204 const sal_Int32 nCol(static_cast< sal_Int32 >((fY < 0.0 ? maSize.getY() -fY : fY) / maSize.getY()));
206 if(nCol % 2)
208 fX += mfOffsetX * maSize.getX();
211 else if(mbUseOffsetY)
213 const sal_Int32 nRow(static_cast< sal_Int32 >((fX < 0.0 ? maSize.getX() -fX : fX) / maSize.getX()));
215 if(nRow % 2)
217 fY += mfOffsetY * maSize.getY();
221 fX = fmod(fX, maSize.getX());
222 fY = fmod(fY, maSize.getY());
224 if(fX < 0.0)
226 fX += maSize.getX();
229 if(fY < 0.0)
231 fY += maSize.getY();
234 return basegfx::B2DPoint(fX + maTopLeft.getX(), fY + maTopLeft.getY());
237 GeoTexSvxBitmapExTiled::GeoTexSvxBitmapExTiled(
238 const BitmapEx& rBitmapEx,
239 const basegfx::B2DRange& rRange,
240 double fOffsetX,
241 double fOffsetY)
242 : GeoTexSvxBitmapEx(rBitmapEx, rRange),
243 mfOffsetX(std::clamp(fOffsetX, 0.0, 1.0)),
244 mfOffsetY(std::clamp(fOffsetY, 0.0, 1.0)),
245 mbUseOffsetX(!basegfx::fTools::equalZero(mfOffsetX)),
246 mbUseOffsetY(!mbUseOffsetX && !basegfx::fTools::equalZero(mfOffsetY))
250 void GeoTexSvxBitmapExTiled::modifyBColor(const basegfx::B2DPoint& rUV, basegfx::BColor& rBColor, double& rfOpacity) const
252 if(mpReadBitmap)
254 GeoTexSvxBitmapEx::modifyBColor(impGetCorrected(rUV), rBColor, rfOpacity);
258 void GeoTexSvxBitmapExTiled::modifyOpacity(const basegfx::B2DPoint& rUV, double& rfOpacity) const
260 if(mpReadBitmap)
262 GeoTexSvxBitmapEx::modifyOpacity(impGetCorrected(rUV), rfOpacity);
267 GeoTexSvxMultiHatch::GeoTexSvxMultiHatch(
268 const primitive3d::HatchTexturePrimitive3D& rPrimitive,
269 double fLogicPixelSize)
270 : mfLogicPixelSize(fLogicPixelSize)
272 const attribute::FillHatchAttribute& rHatch(rPrimitive.getHatch());
273 const basegfx::B2DRange aOutlineRange(0.0, 0.0, rPrimitive.getTextureSize().getX(), rPrimitive.getTextureSize().getY());
274 const double fAngleA(rHatch.getAngle());
275 maColor = rHatch.getColor();
276 mbFillBackground = rHatch.isFillBackground();
277 mp0.reset( new GeoTexSvxHatch(
278 aOutlineRange,
279 aOutlineRange,
280 rHatch.getDistance(),
281 fAngleA) );
283 if(attribute::HatchStyle::Double == rHatch.getStyle() || attribute::HatchStyle::Triple == rHatch.getStyle())
285 mp1.reset( new GeoTexSvxHatch(
286 aOutlineRange,
287 aOutlineRange,
288 rHatch.getDistance(),
289 fAngleA + M_PI_2) );
292 if(attribute::HatchStyle::Triple == rHatch.getStyle())
294 mp2.reset( new GeoTexSvxHatch(
295 aOutlineRange,
296 aOutlineRange,
297 rHatch.getDistance(),
298 fAngleA + M_PI_4) );
302 GeoTexSvxMultiHatch::~GeoTexSvxMultiHatch()
306 bool GeoTexSvxMultiHatch::impIsOnHatch(const basegfx::B2DPoint& rUV) const
308 if(mp0->getDistanceToHatch(rUV) < mfLogicPixelSize)
310 return true;
313 if(mp1 && mp1->getDistanceToHatch(rUV) < mfLogicPixelSize)
315 return true;
318 if(mp2 && mp2->getDistanceToHatch(rUV) < mfLogicPixelSize)
320 return true;
323 return false;
326 void GeoTexSvxMultiHatch::modifyBColor(const basegfx::B2DPoint& rUV, basegfx::BColor& rBColor, double& rfOpacity) const
328 if(impIsOnHatch(rUV))
330 rBColor = maColor;
332 else if(!mbFillBackground)
334 rfOpacity = 0.0;
338 void GeoTexSvxMultiHatch::modifyOpacity(const basegfx::B2DPoint& rUV, double& rfOpacity) const
340 if(mbFillBackground || impIsOnHatch(rUV))
342 rfOpacity = 1.0;
344 else
346 rfOpacity = 0.0;
350 } // end of namespace
352 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */