Avoid potential negative array index access to cached text.
[LibreOffice.git] / vcl / inc / impgraph.hxx
blob2e2b00640a20d629a8a8d6140b49b9b0fd457bae
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 #pragma once
22 #include <vcl/dllapi.h>
23 #include <vcl/GraphicExternalLink.hxx>
24 #include <vcl/gdimtf.hxx>
25 #include <vcl/graph.hxx>
26 #include "graphic/Manager.hxx"
27 #include "graphic/GraphicID.hxx"
28 #include <optional>
30 struct ImpSwapInfo
32 MapMode maPrefMapMode;
33 Size maPrefSize;
34 Size maSizePixel;
36 bool mbIsAnimated;
37 bool mbIsEPS;
38 bool mbIsTransparent;
39 bool mbIsAlpha;
41 sal_uInt32 mnAnimationLoopCount;
42 sal_Int32 mnPageIndex;
45 class OutputDevice;
46 class GfxLink;
47 class ImpSwapFile;
48 class GraphicConversionParameters;
49 class ImpGraphic;
50 namespace rtl { class OStringBuffer; }
52 enum class GraphicContentType : sal_Int32
54 Bitmap,
55 Animation,
56 Vector
59 class VCL_DLLPUBLIC ImpGraphic final
61 friend class Graphic;
62 friend class GraphicID;
63 friend class vcl::graphic::Manager;
65 private:
67 GDIMetaFile maMetaFile;
68 BitmapEx maBitmapEx;
69 /// If maBitmapEx is empty, this preferred size will be set on it when it gets initialized.
70 Size maExPrefSize;
71 ImpSwapInfo maSwapInfo;
72 std::unique_ptr<Animation> mpAnimation;
73 std::shared_ptr<GraphicReader> mpContext;
74 std::shared_ptr<ImpSwapFile> mpSwapFile;
75 std::shared_ptr<GfxLink> mpGfxLink;
76 GraphicType meType;
77 mutable sal_uLong mnSizeBytes;
78 bool mbSwapOut;
79 bool mbDummyContext;
80 std::shared_ptr<VectorGraphicData> maVectorGraphicData;
81 // cache checksum computation
82 mutable BitmapChecksum mnChecksum = 0;
84 std::optional<GraphicID> mxGraphicID;
85 GraphicExternalLink maGraphicExternalLink;
87 std::chrono::high_resolution_clock::time_point maLastUsed;
88 bool mbPrepared;
90 public:
91 ImpGraphic();
92 ImpGraphic( const ImpGraphic& rImpGraphic );
93 ImpGraphic( ImpGraphic&& rImpGraphic ) noexcept;
94 ImpGraphic( GraphicExternalLink aExternalLink);
95 ImpGraphic(std::shared_ptr<GfxLink> xGfxLink, sal_Int32 nPageIndex = 0);
96 ImpGraphic( const BitmapEx& rBmpEx );
97 ImpGraphic(const std::shared_ptr<VectorGraphicData>& rVectorGraphicDataPtr);
98 ImpGraphic( const Animation& rAnimation );
99 ImpGraphic( const GDIMetaFile& rMtf );
100 ~ImpGraphic();
102 void setPrepared(bool bAnimated, const Size* pSizeHint);
104 private:
106 ImpGraphic& operator=( const ImpGraphic& rImpGraphic );
107 ImpGraphic& operator=( ImpGraphic&& rImpGraphic );
108 bool operator==( const ImpGraphic& rImpGraphic ) const;
109 bool operator!=( const ImpGraphic& rImpGraphic ) const { return !( *this == rImpGraphic ); }
111 OUString const & getOriginURL() const
113 return maGraphicExternalLink.msURL;
116 void setOriginURL(OUString const & rOriginURL)
118 maGraphicExternalLink.msURL = rOriginURL;
121 OString getUniqueID()
123 if (!mxGraphicID)
124 mxGraphicID.emplace(*this);
125 return mxGraphicID->getIDString();
128 void createSwapInfo();
129 void restoreFromSwapInfo();
131 void clearGraphics();
132 void clear();
134 GraphicType getType() const { return meType;}
135 void setDefaultType();
136 bool isSupportedGraphic() const;
138 bool isTransparent() const;
139 bool isAlpha() const;
140 bool isAnimated() const;
141 bool isEPS() const;
143 bool isAvailable() const;
144 bool makeAvailable();
146 Bitmap getBitmap(const GraphicConversionParameters& rParameters) const;
147 BitmapEx getBitmapEx(const GraphicConversionParameters& rParameters) const;
148 /// Gives direct access to the contained BitmapEx.
149 const BitmapEx& getBitmapExRef() const;
150 Animation getAnimation() const;
151 const GDIMetaFile& getGDIMetaFile() const;
153 Size getSizePixel() const;
155 Size getPrefSize() const;
156 void setPrefSize( const Size& rPrefSize );
158 MapMode getPrefMapMode() const;
159 void setPrefMapMode( const MapMode& rPrefMapMode );
161 sal_uLong getSizeBytes() const;
163 void draw(OutputDevice& rOutDev, const Point& rDestPt) const;
164 void draw(OutputDevice& rOutDev, const Point& rDestPt,
165 const Size& rDestSize) const;
167 void startAnimation(OutputDevice& rOutDev,
168 const Point& rDestPt,
169 const Size& rDestSize,
170 tools::Long nRendererId,
171 OutputDevice* pFirstFrameOutDev);
172 void stopAnimation( const OutputDevice* pOutputDevice,
173 tools::Long nRendererId );
175 void setAnimationNotifyHdl( const Link<Animation*,void>& rLink );
176 Link<Animation*,void> getAnimationNotifyHdl() const;
178 sal_uInt32 getAnimationLoopCount() const;
180 private:
181 // swapping methods
182 bool swapInFromStream(SvStream& rStream);
183 bool swapInGraphic(SvStream& rStream);
185 bool swapInContent(SvStream& rStream);
186 bool swapOutContent(SvStream& rStream);
187 bool swapOutGraphic(SvStream& rStream);
188 // end swapping
190 std::shared_ptr<GraphicReader>& getContext() { return mpContext;}
191 void setContext( const std::shared_ptr<GraphicReader>& pReader );
192 void setDummyContext( bool value ) { mbDummyContext = value; }
193 bool isDummyContext() const { return mbDummyContext; }
194 void setGfxLink( const std::shared_ptr<GfxLink>& );
195 const std::shared_ptr<GfxLink> & getSharedGfxLink() const;
196 GfxLink getGfxLink() const;
197 bool isGfxLink() const;
199 BitmapChecksum getChecksum() const;
201 const std::shared_ptr<VectorGraphicData>& getVectorGraphicData() const;
203 /// Gets the bitmap replacement for a vector graphic.
204 BitmapEx getVectorGraphicReplacement() const;
206 bool ensureAvailable () const;
208 sal_Int32 getPageNumber() const;
210 // Set the pref size, but don't force swap-in
211 void setValuesForPrefSize(const Size& rPrefSize);
212 // Set the pref map mode, but don't force swap-in
213 void setValuesForPrefMapMod(const MapMode& rPrefMapMode);
215 public:
216 void resetChecksum() { mnChecksum = 0; }
217 bool swapIn();
218 bool swapOut();
219 bool isSwappedOut() const { return mbSwapOut; }
220 SvStream* getSwapFileStream() const;
221 // public only because of use in GraphicFilter
222 void updateFromLoadedGraphic(const ImpGraphic* graphic);
223 void dumpState(rtl::OStringBuffer &rState);
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */