Avoid potential negative array index access to cached text.
[LibreOffice.git] / include / vcl / GraphicObject.hxx
blob4ee6b4f5d3df74bf76637c1c154a46f8ddc62fdd
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 <memory>
23 #include <string_view>
24 #include <vcl/graph.hxx>
25 #include <vcl/dllapi.h>
26 #include <o3tl/typed_flags_set.hxx>
28 #include <vcl/GraphicAttributes.hxx>
30 namespace com::sun::star::graphic { class XGraphic; }
32 // Adjustment defines
33 enum class GraphicAdjustmentFlags
35 NONE = 0x00,
36 DRAWMODE = 0x01,
37 COLORS = 0x02,
38 MIRROR = 0x04,
39 ROTATE = 0x08,
40 TRANSPARENCY = 0x10,
41 ALL = 0x1f,
43 namespace o3tl
45 template<> struct typed_flags<GraphicAdjustmentFlags> : is_typed_flags<GraphicAdjustmentFlags, 0x1f> {};
48 class VirtualDevice;
49 struct GrfSimpleCacheObj;
50 struct ImplTileInfo;
52 class VCL_DLLPUBLIC GraphicObject
54 friend class SdrGrafObj;
56 private:
57 Graphic maGraphic;
58 GraphicAttr maAttr;
59 OUString maUserData;
60 std::unique_ptr<GrfSimpleCacheObj> mxSimpleCache;
62 bool VCL_DLLPRIVATE ImplGetCropParams(
63 const OutputDevice& rOut,
64 Point& rPt,
65 Size& rSz,
66 const GraphicAttr* pAttr,
67 tools::PolyPolygon& rClipPolyPoly,
68 bool& bRectClipRegion
69 ) const;
71 /** Render a given number of tiles in an optimized way
73 This method recursively subdivides the tile rendering problem
74 in smaller parts, i.e. rendering output size x with few tiles
75 of size y, which in turn are generated from the original
76 bitmap in a recursive fashion. The subdivision size can be
77 controlled by the exponent argument, which specifies the
78 minimal number of smaller tiles used in one recursion
79 step. The resulting tile size is given as the integer number
80 of repetitions of the original bitmap along x and y. As the
81 exponent need not necessarily divide these numbers without
82 remainder, the repetition counts are effectively converted to
83 base-exponent numbers, where each place denotes the number of
84 times the corresponding tile size is rendered.
86 @param rVDev
87 Virtual device to render everything into
89 @param nNumTilesX
90 Number of original tiles to generate in x direction
92 @param nNumTilesY
93 Number of original tiles to generate in y direction
95 @param rTileSizePixel
96 Size in pixel of the original tile bitmap to render it in
98 @param pAttr
99 Graphic attributes to be used for rendering
101 @param nFlags
102 Graphic flags to be used for rendering
104 @param rCurrPos
105 Current output point for this recursion level (should start with (0,0))
107 @return true, if everything was successfully rendered.
109 bool VCL_DLLPRIVATE ImplRenderTempTile(
110 VirtualDevice& rVDev,
111 int nNumTilesX,
112 int nNumTilesY,
113 const Size& rTileSizePixel,
114 const GraphicAttr* pAttr
117 /// internally called by ImplRenderTempTile()
118 bool VCL_DLLPRIVATE ImplRenderTileRecursive(
119 VirtualDevice& rVDev,
120 int nExponent,
121 int nMSBFactor,
122 int nNumOrigTilesX,
123 int nNumOrigTilesY,
124 int nRemainderTilesX,
125 int nRemainderTilesY,
126 const Size& rTileSizePixel,
127 const GraphicAttr* pAttr,
128 ImplTileInfo& rTileInfo
131 bool VCL_DLLPRIVATE ImplDrawTiled(
132 OutputDevice& rOut,
133 const tools::Rectangle& rArea,
134 const Size& rSizePixel,
135 const Size& rOffset,
136 const GraphicAttr* pAttr,
137 int nTileCacheSize1D
140 bool VCL_DLLPRIVATE ImplDrawTiled(
141 OutputDevice& rOut,
142 const Point& rPos,
143 int nNumTilesX,
144 int nNumTilesY,
145 const Size& rTileSize,
146 const GraphicAttr* pAttr
147 ) const;
149 void VCL_DLLPRIVATE ImplTransformBitmap(
150 BitmapEx& rBmpEx,
151 const GraphicAttr& rAttr,
152 const Size& rCropLeftTop,
153 const Size& rCropRightBottom,
154 const tools::Rectangle& rCropRect,
155 const Size& rDstSize,
156 bool bEnlarge
157 ) const;
159 public:
160 GraphicObject();
161 GraphicObject( Graphic aGraphic );
162 GraphicObject( const GraphicObject& rCacheObj );
163 ~GraphicObject();
165 GraphicObject& operator=( const GraphicObject& rCacheObj );
166 bool operator==( const GraphicObject& rCacheObj ) const;
167 bool operator!=( const GraphicObject& rCacheObj ) const { return !( *this == rCacheObj ); }
169 const Graphic& GetGraphic() const;
170 void SetGraphic( const Graphic& rGraphic);
172 /** Get graphic transformed according to given attributes
174 This method returns a Graphic transformed, cropped and scaled
175 to the given parameters, ready to be rendered to printer or
176 display. The returned graphic has the same visual appearance
177 as if it had been drawn via GraphicObject::Draw() to a
178 specific output device.
180 @param rDestSize
181 Desired output size in logical coordinates. The mapmode to
182 interpret these logical coordinates in is given by the second
183 parameter, rDestMap.
185 @param rDestMap
186 Mapmode the output should be interpreted in. This is used to
187 interpret rDestSize, to set the appropriate PrefMapMode on the
188 returned Graphic, and to deal correctly with metafile graphics.
190 @param rAttr
191 Graphic attributes used to transform the graphic. This
192 includes cropping, rotation, mirroring, and various color
193 adjustment parameters.
195 @return the readily transformed Graphic
197 Graphic GetTransformedGraphic(
198 const Size& rDestSize,
199 const MapMode& rDestMap,
200 const GraphicAttr& rAttr
201 ) const;
202 Graphic GetTransformedGraphic( const GraphicAttr* pAttr ) const; // TODO: Change to Impl
204 void SetAttr( const GraphicAttr& rAttr );
205 const GraphicAttr& GetAttr() const { return maAttr; }
207 bool HasUserData() const { return !maUserData.isEmpty(); }
208 void SetUserData();
209 void SetUserData( const OUString& rUserData );
210 const OUString& GetUserData() const { return maUserData; }
212 OString GetUniqueID() const;
214 GraphicType GetType() const;
215 Size GetPrefSize() const;
216 MapMode GetPrefMapMode() const;
217 bool IsTransparent() const;
218 bool IsAnimated() const;
219 bool IsEPS() const;
221 bool Draw(OutputDevice& rOut,
222 const Point& rPt,
223 const Size& rSz,
224 const GraphicAttr* pAttr = nullptr) const;
226 /** Draw the graphic repeatedly into the given output rectangle
228 @param pOut
229 OutputDevice where the rendering should take place
231 @param rArea
232 The output area that is filled with tiled instances of this graphic
234 @param rSize
235 The actual size of a single tile
237 @param rOffset
238 Offset from the left, top position of rArea, where to start
239 the tiling. The upper left corner of the graphic tilings will
240 virtually start at this position. Concretely, only that many
241 tiles are drawn to completely fill the given output area.
243 @param nFlags
244 Optional rendering flags
246 @param nTileCacheSize1D
247 Optional dimension of the generated cache tiles. The pOut sees
248 a number of tile draws, which have approximately
249 nTileCacheSize1D times nTileCacheSize1D bitmap sizes if the
250 tile bitmap is smaller. Otherwise, the tile is drawn as
251 is. This is useful if e.g. you want only a few, very large
252 bitmap drawings appear on the outdev.
254 void DrawTiled(
255 OutputDevice& rOut,
256 const tools::Rectangle& rArea,
257 const Size& rSize,
258 const Size& rOffset,
259 int nTileCacheSize1D=128
262 bool StartAnimation(
263 OutputDevice& rOut,
264 const Point& rPt,
265 const Size& rSz,
266 tools::Long nExtraData = 0,
267 OutputDevice* pFirstFrameOutDev = nullptr
270 void StopAnimation( const OutputDevice* pOut = nullptr, tools::Long nExtraData = 0 );
272 static bool isGraphicObjectUniqueIdURL(std::u16string_view rURL);
274 // create CropScaling information
275 // fWidth, fHeight: object size
276 // f*Crop: crop values relative to original bitmap size
277 basegfx::B2DVector calculateCropScaling(
278 double fWidth,
279 double fHeight,
280 double fLeftCrop,
281 double fTopCrop,
282 double fRightCrop,
283 double fBottomCrop) const;
286 namespace vcl::graphic
289 // Will search an object ( e.g. a control ) for any 'ImageURL' or 'Graphic'
290 // properties and return graphics from the properties in a vector. ImageURL
291 // will be loaded from the URL.
293 // Note: this implementation will cater for XNameContainer objects and deep inspect any containers
294 // if they exist
296 VCL_DLLPUBLIC void SearchForGraphics(css::uno::Reference<css::uno::XInterface> const & rxInterface,
297 std::vector<css::uno::Reference<css::graphic::XGraphic>> & raGraphicList);
300 } // end namespace vcl::graphic
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */