tdf#126293 Use lazy-loading stock Image to simplify fwk image lists

Project stock names through XGraphic via origin URL.

(cherry picked from commit 77b88eebaadebb626108172e4f2de36c60960051)

Reviewed-on: https://gerrit.libreoffice.org/79420
Tested-by: Jenkins
Reviewed-by: Michael Meeks <michael.meeks@collabora.com>
(cherry picked from commit ac6fe36ac6ccebf3bf891b891bd47c8e3744a056)

Change-Id: Ib445694f7c142a163ef7e7bc0beea39b88b99e14
Reviewed-on: https://gerrit.libreoffice.org/81965
Tested-by: Jenkins
Reviewed-by: Michael Meeks <michael.meeks@collabora.com>
Reviewed-by: Adolfo Jayme Barrientos <fitojb@ubuntu.com>
diff --git a/framework/source/uiconfiguration/ImageList.cxx b/framework/source/uiconfiguration/ImageList.cxx
index 100e152..c287adc 100644
--- a/framework/source/uiconfiguration/ImageList.cxx
+++ b/framework/source/uiconfiguration/ImageList.cxx
@@ -44,9 +44,7 @@ ImageList::ImageList(const std::vector< OUString >& rNameVector,

    maPrefix = rPrefix;
    for( size_t i = 0; i < rNameVector.size(); ++i )
    {
        ImplAddImage( rNameVector[ i ], static_cast< sal_uInt16 >( i ) + 1, BitmapEx() );
    }
        ImplAddImage( rPrefix, rNameVector[ i ], static_cast< sal_uInt16 >( i ) + 1, Image() );
}

// FIXME: Rather a performance hazard
@@ -57,15 +55,7 @@ BitmapEx ImageList::GetAsHorizontalStrip() const
        return BitmapEx();
    Size aSize( maImageSize.Width() * nCount, maImageSize.Height() );

    // Load any stragglers
    for (sal_uInt16 nIdx = 0; nIdx < nCount; nIdx++)
    {
        ImageAryData *pData = maImages[ nIdx ].get();
        if( pData->IsLoadable() )
            ImplLoad(*pData);
    }

    BitmapEx aTempl = maImages[ 0 ]->maBitmapEx;
    BitmapEx aTempl = maImages[ 0 ]->maImage.GetBitmapEx();
    BitmapEx aResult( aTempl, Point(), aSize );

    tools::Rectangle aSrcRect( Point( 0, 0 ), maImageSize );
@@ -74,7 +64,8 @@ BitmapEx ImageList::GetAsHorizontalStrip() const
        tools::Rectangle aDestRect( Point( nIdx * maImageSize.Width(), 0 ),
                             maImageSize );
        ImageAryData *pData = maImages[ nIdx ].get();
        aResult.CopyPixel( aDestRect, aSrcRect, &pData->maBitmapEx);
        BitmapEx aTmp = pData->maImage.GetBitmapEx();
        aResult.CopyPixel( aDestRect, aSrcRect, &aTmp);
    }

    return aResult;
@@ -101,7 +92,7 @@ void ImageList::InsertFromHorizontalStrip( const BitmapEx &rBitmapEx,
    for (sal_uInt16 nIdx = 0; nIdx < nItems; nIdx++)
    {
        BitmapEx aBitmap( rBitmapEx, Point( nIdx * aSize.Width(), 0 ), aSize );
        ImplAddImage( rNameVector[ nIdx ], nIdx + 1, aBitmap );
        ImplAddImage( maPrefix, rNameVector[ nIdx ], nIdx + 1, Image( aBitmap ) );
    }
}

@@ -117,8 +108,7 @@ void ImageList::AddImage( const OUString& rImageName, const Image& rImage )
{
    SAL_WARN_IF( GetImagePos( rImageName ) != IMAGELIST_IMAGE_NOTFOUND, "vcl", "ImageList::AddImage() - ImageName already exists" );

    ImplAddImage( rImageName, GetImageCount() + 1,
                          rImage.GetBitmapEx() );
    ImplAddImage( maPrefix, rImageName, GetImageCount() + 1, rImage );
}

void ImageList::ReplaceImage( const OUString& rImageName, const Image& rImage )
@@ -127,10 +117,10 @@ void ImageList::ReplaceImage( const OUString& rImageName, const Image& rImage )

    if( nId )
    {
        //Just replace the bitmap rather than doing RemoveImage / AddImage
        //which breaks index-based iteration.
        // Just replace the bitmap rather than doing RemoveImage / AddImage
        // which breaks index-based iteration.
        ImageAryData *pImg = maNameHash[ rImageName ];
        pImg->maBitmapEx = rImage.GetBitmapEx();
        pImg->maImage = rImage;
    }
}

@@ -151,10 +141,7 @@ Image ImageList::GetImage( const OUString& rImageName ) const
    auto it = maNameHash.find( rImageName );
    if (it == maNameHash.end())
        return Image();
    ImageAryData *pImg = it->second;
    if( pImg->IsLoadable() )
        ImplLoad( *pImg );
    return Image( pImg->maBitmapEx );
    return it->second->maImage;
}

sal_uInt16 ImageList::GetImageCount() const
@@ -200,10 +187,14 @@ void ImageList::GetImageNames( std::vector< OUString >& rNames ) const
    }
}

void ImageList::ImplAddImage( const OUString &aName,
                              sal_uInt16 nId, const BitmapEx &aBitmapEx )
void ImageList::ImplAddImage( const OUString &aPrefix, const OUString &aName,
                              sal_uInt16 nId, const Image &aImage )
{
    ImageAryData *pImg = new ImageAryData{ aName, nId, aBitmapEx };
    Image aInsert = aImage;
    if (!aInsert)
        aInsert = Image( "private:graphicrepository/" + aPrefix + aName );

    ImageAryData *pImg = new ImageAryData{ aName, nId, aInsert };
    maImages.emplace_back( pImg );
    if( !aName.isEmpty() )
        maNameHash [ aName ] = pImg;
@@ -217,29 +208,4 @@ void ImageList::ImplRemoveImage( sal_uInt16 nPos )
    maImages.erase( maImages.begin() + nPos );
}

void ImageList::ImplLoad(ImageAryData& rImageData) const
{
    OUString aIconTheme = Application::GetSettings().GetStyleSettings().DetermineIconTheme();

    OUString aFileName = maPrefix + rImageData.maName;

    bool bSuccess = ImageTree::get().loadImage(aFileName, aIconTheme, rImageData.maBitmapEx, true);

    /* If the uno command has parameters, passed in from a toolbar,
     * recover from failure by removing the parameters from the file name
     */
    if (!bSuccess && aFileName.indexOf("%3f") > 0)
    {
        sal_Int32 nStart = aFileName.indexOf("%3f");
        sal_Int32 nEnd = aFileName.lastIndexOf(".");

        aFileName = aFileName.replaceAt(nStart, nEnd - nStart, "");
        bSuccess = ImageTree::get().loadImage(aFileName, aIconTheme, rImageData.maBitmapEx, true);
    }

    SAL_WARN_IF(!bSuccess, "fwk.uiconfiguration", "Failed to load image '" << aFileName
              << "' from icon theme '" << aIconTheme << "'");
}


/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/framework/source/uiconfiguration/ImageList.hxx b/framework/source/uiconfiguration/ImageList.hxx
index 604624e..fae0445 100644
--- a/framework/source/uiconfiguration/ImageList.hxx
+++ b/framework/source/uiconfiguration/ImageList.hxx
@@ -20,18 +20,16 @@
#ifndef FRAMEWORK_SOURCE_UICONFIGURATION_IMAGELIST_HXX
#define FRAMEWORK_SOURCE_UICONFIGURATION_IMAGELIST_HXX

#include <vcl/bitmapex.hxx>
#include <vcl/image.hxx>
#include <unordered_map>
#include <vector>

// Images identified by either name, or by id
struct ImageAryData
{
    OUString                maName;
    sal_uInt16              mnId;
    BitmapEx                maBitmapEx;

    bool IsLoadable() { return maBitmapEx.IsEmpty() && !maName.isEmpty(); }
    OUString    maName;
    sal_uInt16  mnId;
    Image       maImage;
};


@@ -70,7 +68,7 @@ private:
    Size                   maImageSize;

    sal_uInt16  ImplGetImageId( const OUString& rImageName ) const;
    void ImplAddImage( const OUString &aName, sal_uInt16 nId, const BitmapEx &aBitmapEx );
    void ImplAddImage( const OUString &aPrefix, const OUString &aName, sal_uInt16 nId, const Image &aImage );
    void ImplRemoveImage( sal_uInt16 nPos );
    void ImplLoad(ImageAryData&) const;
};
diff --git a/framework/source/uiconfiguration/imagemanagerimpl.cxx b/framework/source/uiconfiguration/imagemanagerimpl.cxx
index 393757d..73541b3 100644
--- a/framework/source/uiconfiguration/imagemanagerimpl.cxx
+++ b/framework/source/uiconfiguration/imagemanagerimpl.cxx
@@ -711,7 +711,7 @@ namespace
{
    css::uno::Reference< css::graphic::XGraphic > GetXGraphic(const Image &rImage)
    {
        return Graphic(rImage.GetBitmapEx()).GetXGraphic();
        return Graphic(rImage).GetXGraphic();
    }
}

diff --git a/include/vcl/graph.hxx b/include/vcl/graph.hxx
index d49a8a2..3749d85 100644
--- a/include/vcl/graph.hxx
+++ b/include/vcl/graph.hxx
@@ -101,6 +101,7 @@ public:
    bool            getSnapHorVerLines() const { return mbSnapHorVerLines; }
};

class Image;
class VCL_DLLPUBLIC Graphic
{
private:
@@ -118,6 +119,7 @@ public:
                    Graphic( const Graphic& rGraphic );
                    Graphic( Graphic&& rGraphic );
                    Graphic( const Bitmap& rBmp );
                    Graphic( const Image& rImage );
                    Graphic( const BitmapEx& rBmpEx );
                    Graphic( const VectorGraphicDataPtr& rVectorGraphicDataPtr );
                    Graphic( const Animation& rAnimation );
diff --git a/include/vcl/image.hxx b/include/vcl/image.hxx
index 4c50475..1267e1a 100644
--- a/include/vcl/image.hxx
+++ b/include/vcl/image.hxx
@@ -51,7 +51,6 @@ enum class StockImage { Yes };
class SAL_WARN_UNUSED VCL_DLLPUBLIC Image
{
    friend class ::OutputDevice;

public:
    Image();
    explicit Image(BitmapEx const & rBitmapEx);
@@ -72,6 +71,8 @@ public:
        return !(Image::operator==(rImage));
    }

    SAL_DLLPRIVATE  OUString GetStock() const;

    void Draw(OutputDevice* pOutDev, const Point& rPos, DrawImageFlags nStyle, const Size* pSize = nullptr);

private:
diff --git a/sfx2/source/sidebar/Tools.cxx b/sfx2/source/sidebar/Tools.cxx
index 0f0f596..cebfc93 100644
--- a/sfx2/source/sidebar/Tools.cxx
+++ b/sfx2/source/sidebar/Tools.cxx
@@ -53,14 +53,13 @@ Image Tools::GetImage (
{
    if (rsURL.getLength() > 0)
    {
        OUString sPath;

        if (rsURL.startsWith(".uno:"))
        {
            return vcl::CommandInfoProvider::GetImageForCommand(rsURL, rxFrame);
        }
        else
        {

        else if (rsURL.startsWith("private:graphicrepository/", &sPath))
            return Image(rsURL);
        }
    }
    return Image();
}
diff --git a/vcl/inc/image.h b/vcl/inc/image.h
index c5acc06..f4b2c97 100644
--- a/vcl/inc/image.h
+++ b/vcl/inc/image.h
@@ -51,6 +51,11 @@ public:
        return maStockName.getLength() > 0;
    }

    OUString getStock() const
    {
        return maStockName;
    }

    /// get size in co-ordinates not scaled for HiDPI
    Size getSizePixel();
    /// Legacy - the original bitmap
diff --git a/vcl/source/gdi/graph.cxx b/vcl/source/gdi/graph.cxx
index 8d63a4a..306eca4 100644
--- a/vcl/source/gdi/graph.cxx
+++ b/vcl/source/gdi/graph.cxx
@@ -21,6 +21,7 @@
#include <vcl/outdev.hxx>
#include <vcl/svapp.hxx>
#include <vcl/graph.hxx>
#include <vcl/image.hxx>
#include <vcl/metaact.hxx>
#include <impgraph.hxx>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
@@ -33,6 +34,7 @@
#include <graphic/UnoGraphic.hxx>
#include <vcl/GraphicExternalLink.hxx>

#include <image.h>

using namespace ::com::sun::star;

@@ -216,6 +218,17 @@ Graphic::Graphic(const BitmapEx& rBmpEx)
{
}

// We use XGraphic for passing toolbar images across app UNO aps
// and we need to be able to see and preserve 'stock' images too.
Graphic::Graphic(const Image& rImage)
    // FIXME: should really defer the BitmapEx load.
    : mxImpGraphic(new ImpGraphic(rImage.GetBitmapEx()))
{
    OUString aStock = rImage.GetStock();
    if (aStock.getLength())
        mxImpGraphic->setOriginURL("private:graphicrepository/" + aStock);
}

Graphic::Graphic(const VectorGraphicDataPtr& rVectorGraphicDataPtr)
    : mxImpGraphic(vcl::graphic::Manager::get().newInstance(rVectorGraphicDataPtr))
{
diff --git a/vcl/source/helper/commandinfoprovider.cxx b/vcl/source/helper/commandinfoprovider.cxx
index 3fd2672..a5ae538 100644
--- a/vcl/source/helper/commandinfoprovider.cxx
+++ b/vcl/source/helper/commandinfoprovider.cxx
@@ -353,20 +353,11 @@ Reference<graphic::XGraphic> GetXGraphicForCommand(const OUString& rsCommandName
    return nullptr;
}

static BitmapEx GetBitmapForCommand(const OUString& rsCommandName,
                             const Reference<frame::XFrame>& rxFrame,
                             vcl::ImageType eImageType)
{
    const Graphic aGraphic(GetXGraphicForCommand(rsCommandName, rxFrame, eImageType));
    BitmapEx aBitmap(aGraphic.GetBitmapEx());
    return aBitmap;
}

Image GetImageForCommand(const OUString& rsCommandName,
                         const Reference<frame::XFrame>& rxFrame,
                         vcl::ImageType eImageType)
{
    return Image(GetBitmapForCommand(rsCommandName, rxFrame, eImageType));
    return Image(GetXGraphicForCommand(rsCommandName, rxFrame, eImageType));
}

sal_Int32 GetPropertiesForCommand (
diff --git a/vcl/source/image/Image.cxx b/vcl/source/image/Image.cxx
index 4316f91..b1d96ed 100644
--- a/vcl/source/image/Image.cxx
+++ b/vcl/source/image/Image.cxx
@@ -51,24 +51,28 @@ Image::Image(const BitmapEx& rBitmapEx)

Image::Image(uno::Reference<graphic::XGraphic> const & rxGraphic)
{
    const Graphic aGraphic(rxGraphic);
    ImplInit(aGraphic.GetBitmapEx());
    if (rxGraphic.is())
    {
        const Graphic aGraphic(rxGraphic);

        OUString aPath;
        if (aGraphic.getOriginURL().startsWith("private:graphicrepository/", &aPath))
            mpImplData = std::make_shared<ImplImage>(aPath, Size());
        else
            ImplInit(aGraphic.GetBitmapEx());
    }
}

Image::Image(const OUString & rFileUrl)
{
    OUString sImageName;
    if (rFileUrl.startsWith("private:graphicrepository/", &sImageName))
    {
        mpImplData = std::make_shared<ImplImage>(sImageName, Size());
    }
    else
    {
        Graphic aGraphic;
        if (ERRCODE_NONE == GraphicFilter::LoadGraphic(rFileUrl, IMP_PNG, aGraphic))
        {
            ImplInit(aGraphic.GetBitmapEx());
        }
    }
}

@@ -83,6 +87,13 @@ void Image::ImplInit(const BitmapEx& rBitmapEx)
        mpImplData = std::make_shared<ImplImage>(rBitmapEx);
}

OUString Image::GetStock() const
{
    if (mpImplData)
        return mpImplData->getStock();
    return OUString();
}

Size Image::GetSizePixel() const
{
    if (mpImplData)
diff --git a/vcl/source/image/ImplImage.cxx b/vcl/source/image/ImplImage.cxx
index 7605f88..0f9ab36 100644
--- a/vcl/source/image/ImplImage.cxx
+++ b/vcl/source/image/ImplImage.cxx
@@ -55,13 +55,37 @@ ImplImage::ImplImage(const OUString &aStockName, Size const & rPreferedSize)
bool ImplImage::loadStockAtScale(double fScale, BitmapEx &rBitmapEx)
{
    BitmapEx aBitmapEx;

    ImageLoadFlags eScalingFlags = ImageLoadFlags::NONE;
    sal_Int32 nScalePercentage = -1;

    if (comphelper::LibreOfficeKit::isActive()) // scale at the surface
    {
        nScalePercentage = fScale * 100.0;
        eScalingFlags = ImageLoadFlags::IgnoreScalingFactor;
    }

    OUString aIconTheme = Application::GetSettings().GetStyleSettings().DetermineIconTheme();
    if (!ImageTree::get().loadImage(maStockName, aIconTheme, aBitmapEx, true,
                                    fScale * 100.0,
                                    ImageLoadFlags::IgnoreScalingFactor))
                                    nScalePercentage, eScalingFlags))
    {
        SAL_WARN("vcl", "Failed to load scaled image from " << maStockName << " at " << fScale);
        return false;
        /* If the uno command has parameters, passed in from a toolbar,
         * recover from failure by removing the parameters from the file name
         */
        if (maStockName.indexOf("%3f") > 0)
        {
            sal_Int32 nStart = maStockName.indexOf("%3f");
            sal_Int32 nEnd = maStockName.lastIndexOf(".");

            OUString aFileName = maStockName.replaceAt(nStart, nEnd - nStart, "");
            if (!ImageTree::get().loadImage(aFileName, aIconTheme, aBitmapEx, true,
                                            nScalePercentage, eScalingFlags))
            {
                SAL_WARN("vcl", "Failed to load scaled image from " << maStockName <<
                         " and " << aFileName << " at " << fScale);
                return false;
            }
        }
    }
    if (maPreferedSizePixel != Size())
    {
@@ -130,7 +154,7 @@ BitmapEx ImplImage::getBitmapExForHiDPI(bool bDisabled)
        // FIXME: DPI scaling should be tied to the outdev really ...
        double fScale = comphelper::LibreOfficeKit::getDPIScale();
        Size aTarget(maSizePixel.Width()*fScale,
                         maSizePixel.Height()*fScale);
                     maSizePixel.Height()*fScale);
        if (maBitmapEx.GetSizePixel() != aTarget)
            loadStockAtScale(fScale, maBitmapEx);
    }