bump product version to 7.6.3.2-android
[LibreOffice.git] / vcl / source / image / ImplImage.cxx
bloba63dd8ccb7ce1101981e580cffa1fd05acd7d2ed
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/log.hxx>
21 #include <utility>
22 #include <vcl/svapp.hxx>
23 #include <vcl/bitmapex.hxx>
24 #include <vcl/gdimtf.hxx>
25 #include <vcl/settings.hxx>
26 #include <vcl/virdev.hxx>
27 #include <vcl/BitmapFilter.hxx>
28 #include <vcl/ImageTree.hxx>
29 #include <bitmap/BitmapDisabledImageFilter.hxx>
30 #include <comphelper/lok.hxx>
32 #include <image.h>
33 #include <salgdi.hxx>
35 ImplImage::ImplImage(const BitmapEx &rBitmapEx)
36 : maBitmapChecksum(0)
37 , maSizePixel(rBitmapEx.GetSizePixel())
38 , maBitmapEx(rBitmapEx)
42 ImplImage::ImplImage(OUString aStockName)
43 : maBitmapChecksum(0)
44 , maStockName(std::move(aStockName))
48 ImplImage::ImplImage(const GDIMetaFile& rMetaFile)
49 : maBitmapChecksum(0)
50 , maSizePixel(rMetaFile.GetPrefSize())
51 , mxMetaFile(new GDIMetaFile(rMetaFile))
55 bool ImplImage::loadStockAtScale(SalGraphics* pGraphics, BitmapEx &rBitmapEx)
57 BitmapEx aBitmapEx;
59 ImageLoadFlags eScalingFlags = ImageLoadFlags::NONE;
60 sal_Int32 nScalePercentage = -1;
62 double fScale(1.0);
63 if (pGraphics && pGraphics->ShouldDownscaleIconsAtSurface(&fScale)) // scale at the surface
65 nScalePercentage = fScale * 100.0;
66 eScalingFlags = ImageLoadFlags::IgnoreScalingFactor;
69 OUString aIconTheme = Application::GetSettings().GetStyleSettings().DetermineIconTheme();
70 if (!ImageTree::get().loadImage(maStockName, aIconTheme, aBitmapEx, true,
71 nScalePercentage, eScalingFlags))
73 /* If the uno command has parameters, passed in from a toolbar,
74 * recover from failure by removing the parameters from the file name
76 if (maStockName.indexOf("%3f") > 0)
78 sal_Int32 nStart = maStockName.indexOf("%3f");
79 sal_Int32 nEnd = maStockName.lastIndexOf(".");
81 OUString aFileName = maStockName.replaceAt(nStart, nEnd - nStart, u"");
82 if (!ImageTree::get().loadImage(aFileName, aIconTheme, aBitmapEx, true,
83 nScalePercentage, eScalingFlags))
85 SAL_WARN("vcl", "Failed to load scaled image from " << maStockName <<
86 " and " << aFileName << " at " << fScale);
87 return false;
90 else
92 SAL_WARN("vcl", "Failed to load scaled image from " << maStockName <<
93 " at " << fScale);
94 return false;
97 rBitmapEx = aBitmapEx;
98 return true;
101 Size ImplImage::getSizePixel()
103 Size aRet;
104 if (!isSizeEmpty())
105 aRet = maSizePixel;
106 else if (isStock())
108 if (loadStockAtScale(nullptr, maBitmapEx))
110 assert(maDisabledBitmapEx.IsEmpty());
111 assert(maBitmapChecksum == 0);
112 maSizePixel = maBitmapEx.GetSizePixel();
113 aRet = maSizePixel;
115 else
116 SAL_WARN("vcl", "Failed to load stock icon " << maStockName);
118 return aRet;
121 /// non-HiDPI compatibility method.
122 BitmapEx const & ImplImage::getBitmapEx(bool bDisabled)
124 getSizePixel(); // force load, and at unity scale.
125 if (bDisabled)
127 // Changed since we last generated this.
128 BitmapChecksum aChecksum = maBitmapEx.GetChecksum();
129 if (maBitmapChecksum != aChecksum ||
130 maDisabledBitmapEx.GetSizePixel() != maBitmapEx.GetSizePixel())
132 maDisabledBitmapEx = maBitmapEx;
133 BitmapFilter::Filter(maDisabledBitmapEx, BitmapDisabledImageFilter());
134 maBitmapChecksum = aChecksum;
136 return maDisabledBitmapEx;
139 return maBitmapEx;
142 bool ImplImage::isEqual(const ImplImage &ref) const
144 if (isStock() != ref.isStock())
145 return false;
146 if (isStock())
147 return maStockName == ref.maStockName;
148 else
149 return maBitmapEx == ref.maBitmapEx;
152 BitmapEx const & ImplImage::getBitmapExForHiDPI(bool bDisabled, SalGraphics* pGraphics)
154 if ((isStock() || mxMetaFile) && pGraphics)
155 { // check we have the right bitmap cached.
156 double fScale = 1.0;
157 pGraphics->ShouldDownscaleIconsAtSurface(&fScale);
158 Size aTarget(maSizePixel.Width()*fScale,
159 maSizePixel.Height()*fScale);
160 if (maBitmapEx.GetSizePixel() != aTarget)
162 if (isStock())
163 loadStockAtScale(pGraphics, maBitmapEx);
164 else // if (mxMetaFile)
166 ScopedVclPtrInstance<VirtualDevice> aVDev(DeviceFormat::WITH_ALPHA);
167 aVDev->SetOutputSizePixel(aTarget);
168 mxMetaFile->WindStart();
169 mxMetaFile->Play(*aVDev, Point(), aTarget);
170 maBitmapEx = aVDev->GetBitmapEx(Point(), aTarget);
174 return getBitmapEx(bDisabled);
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */