Avoid potential negative array index access to cached text.
[LibreOffice.git] / vcl / inc / win / salgdi.h
blob80fafdeba56ffda8d7811695b5ad56e3eed71c3e
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 <sal/config.h>
24 #include <sallayout.hxx>
25 #include <salgeom.hxx>
26 #include <salgdi.hxx>
27 #include <font/LogicalFontInstance.hxx>
28 #include <fontattributes.hxx>
29 #include <font/PhysicalFontFace.hxx>
30 #include <impfont.hxx>
31 #include <vcl/fontcapabilities.hxx>
32 #include <vcl/fontcharmap.hxx>
33 #include <systools/win32/comtools.hxx>
35 #include <memory>
36 #include <unordered_set>
38 #ifndef INCLUDED_PRE_POST_WIN_H
39 #define INCLUDED_PRE_POST_WIN_H
40 # include <prewin.h>
41 # include <postwin.h>
42 #endif
44 #include <hb-ot.h>
45 #include <dwrite.h>
47 namespace vcl::font
49 class PhysicalFontCollection;
50 class FontSelectPattern;
52 class WinFontInstance;
53 class ImplFontAttrCache;
54 class SalGraphicsImpl;
55 class WinSalGraphicsImplBase;
56 class FontMetricData;
58 #define RGB_TO_PALRGB(nRGB) ((nRGB)|0x02000000)
59 #define PALRGB_TO_RGB(nPalRGB) ((nPalRGB)&0x00ffffff)
61 // win32 specific physically available font face
62 class WinFontFace final : public vcl::font::PhysicalFontFace
64 public:
65 explicit WinFontFace(const ENUMLOGFONTEXW&, const NEWTEXTMETRICW&);
66 ~WinFontFace() override;
68 rtl::Reference<LogicalFontInstance> CreateFontInstance( const vcl::font::FontSelectPattern& ) const override;
69 sal_IntPtr GetFontId() const override;
70 void SetFontId( sal_IntPtr nId ) { mnId = nId; }
72 BYTE GetCharSet() const { return meWinCharSet; }
73 BYTE GetPitchAndFamily() const { return mnPitchAndFamily; }
75 hb_blob_t* GetHbTable(hb_tag_t nTag) const override;
77 const std::vector<hb_variation_t>& GetVariations(const LogicalFontInstance&) const override;
79 private:
80 sal_IntPtr mnId;
82 BYTE meWinCharSet;
83 BYTE mnPitchAndFamily;
84 LOGFONTW maLogFont;
85 mutable sal::systools::COMReference<IDWriteFontFace> mxDWFontFace;
88 /** Class that creates (and destroys) a compatible Device Context.
90 This is to be used for GDI drawing into a DIB that we later use for a different
91 drawing method, such as a texture for OpenGL drawing or surface for Skia drawing.
93 class CompatibleDC
95 protected:
96 /// The compatible DC that we create for our purposes.
97 HDC mhCompatibleDC;
99 /// DIBSection that we use for the GDI drawing, and later obtain.
100 HBITMAP mhBitmap;
102 /// Return the previous bitmap to undo the SelectObject.
103 HBITMAP mhOrigBitmap;
105 /// DIBSection data.
106 sal_uInt32 *mpData;
108 /// Mapping between the GDI position and OpenGL, to use for OpenGL drawing.
109 SalTwoRect maRects;
111 /// The SalGraphicsImpl where we will draw. If null, we ignore the drawing, it means it happened directly to the DC...
112 WinSalGraphicsImplBase *mpImpl;
114 // If 'disable' is true, this class is a simple wrapper for drawing directly. Subclasses should use true.
115 CompatibleDC(SalGraphics &rGraphics, int x, int y, int width, int height, bool disable=true);
117 public:
118 static std::unique_ptr< CompatibleDC > create(SalGraphics &rGraphics, int x, int y, int width, int height);
120 virtual ~CompatibleDC();
122 HDC getCompatibleHDC() { return mhCompatibleDC; }
124 SalTwoRect getTwoRect() const { return maRects; }
126 tools::Long getBitmapWidth() const { return maRects.mnSrcWidth; }
127 tools::Long getBitmapHeight() const { return maRects.mnSrcHeight; }
129 /// Reset the DC with the defined color.
130 void fill(sal_uInt32 color);
134 * WinSalGraphics never owns the HDC it uses to draw, because the HDC can have
135 * various origins with different ways to correctly free it. And WinSalGraphics
136 * stores all default values (mhDef*) of the HDC, which must be restored when
137 * the HDC changes (setHDC) or the SalGraphics is destructed. So think of the
138 * HDC in terms of Rust's Borrowing semantics.
140 class WinSalGraphics : public SalGraphics
142 friend class WinSalGraphicsImpl;
143 friend class ScopedFont;
145 protected:
146 std::unique_ptr<SalGraphicsImpl> mpImpl;
147 WinSalGraphicsImplBase * mWinSalGraphicsImplBase;
149 private:
150 HDC mhLocalDC; // HDC
151 bool mbPrinter : 1; // is Printer
152 bool mbVirDev : 1; // is VirDev
153 bool mbWindow : 1; // is Window
154 bool mbScreen : 1; // is Screen compatible
155 HWND mhWnd; // Window-Handle, when Window-Graphics
157 rtl::Reference<WinFontInstance>
158 mpWinFontEntry[ MAX_FALLBACK ]; // pointer to the most recent font instance
159 HRGN mhRegion; // vcl::Region Handle
160 HPEN mhDefPen; // DefaultPen
161 HBRUSH mhDefBrush; // DefaultBrush
162 HFONT mhDefFont; // DefaultFont
163 HPALETTE mhDefPal; // DefaultPalette
164 COLORREF mnTextColor; // TextColor
165 RGNDATA* mpClipRgnData; // ClipRegion-Data
166 RGNDATA* mpStdClipRgnData; // Cache Standard-ClipRegion-Data
167 int mnPenWidth; // line width
169 inline static sal::systools::COMReference<IDWriteFactory> mxDWriteFactory;
170 inline static sal::systools::COMReference<IDWriteGdiInterop> mxDWriteGdiInterop;
171 inline static bool bDWriteDone = false;
173 // just call both from setHDC!
174 void InitGraphics();
175 void DeInitGraphics();
177 public:
178 // Return HFONT, and whether the font is for vertical writing ( prefixed with '@' )
179 // and tmDescent value for adjusting offset in vertical writing mode.
180 std::tuple<HFONT,bool,sal_Int32> ImplDoSetFont(HDC hDC, vcl::font::FontSelectPattern const & i_rFont, const vcl::font::PhysicalFontFace * i_pFontFace, HFONT& o_rOldFont);
182 HDC getHDC() const { return mhLocalDC; }
183 // NOTE: this doesn't transfer ownership! See class comment.
184 void setHDC(HDC aNew);
186 HPALETTE getDefPal() const;
187 // returns the result from RealizePalette, otherwise 0 on success or GDI_ERROR
188 UINT setPalette(HPALETTE, BOOL bForceBkgd = TRUE);
190 HRGN getRegion() const;
193 enum Type
195 PRINTER,
196 VIRTUAL_DEVICE,
197 WINDOW,
198 SCREEN
201 static void getDWriteFactory(IDWriteFactory** pFactory, IDWriteGdiInterop** pInterop = nullptr);
203 public:
205 HWND gethWnd();
208 public:
209 explicit WinSalGraphics(WinSalGraphics::Type eType, bool bScreen, HWND hWnd,
210 SalGeometryProvider *pProvider);
211 virtual ~WinSalGraphics() override;
213 SalGraphicsImpl* GetImpl() const override;
214 WinSalGraphicsImplBase * getWinSalGraphicsImplBase() const { return mWinSalGraphicsImplBase; }
215 bool isPrinter() const;
216 bool isVirtualDevice() const;
217 bool isWindow() const;
218 bool isScreen() const;
220 void setHWND(HWND hWnd);
221 void Flush();
223 protected:
224 virtual void setClipRegion( const vcl::Region& ) override;
225 // draw --> LineColor and FillColor and RasterOp and ClipRegion
226 virtual void drawPixel( tools::Long nX, tools::Long nY ) override;
227 virtual void drawPixel( tools::Long nX, tools::Long nY, Color nColor ) override;
228 virtual void drawLine( tools::Long nX1, tools::Long nY1, tools::Long nX2, tools::Long nY2 ) override;
229 virtual void drawRect( tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight ) override;
230 virtual void drawPolyLine( sal_uInt32 nPoints, const Point* pPtAry ) override;
231 virtual void drawPolygon( sal_uInt32 nPoints, const Point* pPtAry ) override;
232 virtual void drawPolyPolygon( sal_uInt32 nPoly, const sal_uInt32* pPoints, const Point** pPtAry ) override;
233 virtual void drawPolyPolygon(
234 const basegfx::B2DHomMatrix& rObjectToDevice,
235 const basegfx::B2DPolyPolygon&,
236 double fTransparency) override;
237 virtual bool drawPolyLine(
238 const basegfx::B2DHomMatrix& rObjectToDevice,
239 const basegfx::B2DPolygon&,
240 double fTransparency,
241 double fLineWidth,
242 const std::vector< double >* pStroke, // MM01
243 basegfx::B2DLineJoin,
244 css::drawing::LineCap,
245 double fMiterMinimumAngle,
246 bool bPixelSnapHairline) override;
247 virtual bool drawPolyLineBezier( sal_uInt32 nPoints, const Point* pPtAry, const PolyFlags* pFlgAry ) override;
248 virtual bool drawPolygonBezier( sal_uInt32 nPoints, const Point* pPtAry, const PolyFlags* pFlgAry ) override;
249 virtual bool drawPolyPolygonBezier( sal_uInt32 nPoly, const sal_uInt32* pPoints, const Point* const* pPtAry, const PolyFlags* const* pFlgAry ) override;
250 virtual bool drawGradient( const tools::PolyPolygon&, const Gradient& ) override;
251 virtual bool implDrawGradient(basegfx::B2DPolyPolygon const & rPolyPolygon, SalGradient const & rGradient) override;
253 // CopyArea --> No RasterOp, but ClipRegion
254 virtual void copyArea( tools::Long nDestX, tools::Long nDestY, tools::Long nSrcX, tools::Long nSrcY, tools::Long nSrcWidth,
255 tools::Long nSrcHeight, bool bWindowInvalidate ) override;
257 // CopyBits and DrawBitmap --> RasterOp and ClipRegion
258 // CopyBits() --> pSrcGraphics == NULL, then CopyBits on same Graphics
259 virtual void copyBits( const SalTwoRect& rPosAry, SalGraphics* pSrcGraphics ) override;
260 virtual void drawBitmap( const SalTwoRect& rPosAry, const SalBitmap& rSalBitmap ) override;
261 virtual void drawBitmap( const SalTwoRect& rPosAry,
262 const SalBitmap& rSalBitmap,
263 const SalBitmap& rTransparentBitmap ) override;
264 virtual void drawMask( const SalTwoRect& rPosAry,
265 const SalBitmap& rSalBitmap,
266 Color nMaskColor ) override;
268 virtual std::shared_ptr<SalBitmap> getBitmap( tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight ) override;
269 virtual Color getPixel( tools::Long nX, tools::Long nY ) override;
271 // invert --> ClipRegion (only Windows or VirDevs)
272 virtual void invert( tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight, SalInvert nFlags) override;
273 virtual void invert( sal_uInt32 nPoints, const Point* pPtAry, SalInvert nFlags ) override;
275 virtual bool drawEPS( tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight, void* pPtr, sal_uInt32 nSize ) override;
277 // native widget rendering methods that require mirroring
278 protected:
279 virtual bool isNativeControlSupported( ControlType nType, ControlPart nPart ) override;
280 virtual bool hitTestNativeControl( ControlType nType, ControlPart nPart, const tools::Rectangle& rControlRegion,
281 const Point& aPos, bool& rIsInside ) override;
282 virtual bool drawNativeControl( ControlType nType, ControlPart nPart, const tools::Rectangle& rControlRegion,
283 ControlState nState, const ImplControlValue& aValue,
284 const OUString& aCaption, const Color& rBackgroundColor ) override;
285 virtual bool getNativeControlRegion( ControlType nType, ControlPart nPart, const tools::Rectangle& rControlRegion, ControlState nState,
286 const ImplControlValue& aValue, const OUString& aCaption,
287 tools::Rectangle &rNativeBoundingRegion, tools::Rectangle &rNativeContentRegion ) override;
289 public:
290 virtual bool blendBitmap( const SalTwoRect&,
291 const SalBitmap& rBitmap ) override;
293 virtual bool blendAlphaBitmap( const SalTwoRect&,
294 const SalBitmap& rSrcBitmap,
295 const SalBitmap& rMaskBitmap,
296 const SalBitmap& rAlphaBitmap ) override;
298 virtual bool drawAlphaBitmap( const SalTwoRect&,
299 const SalBitmap& rSourceBitmap,
300 const SalBitmap& rAlphaBitmap ) override;
301 virtual bool drawTransformedBitmap(
302 const basegfx::B2DPoint& rNull,
303 const basegfx::B2DPoint& rX,
304 const basegfx::B2DPoint& rY,
305 const SalBitmap& rSourceBitmap,
306 const SalBitmap* pAlphaBitmap,
307 double fAlpha) override;
309 virtual bool hasFastDrawTransformedBitmap() const override;
311 virtual bool drawAlphaRect( tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight, sal_uInt8 nTransparency ) override;
313 private:
314 // local helpers
316 void DrawTextLayout(const GenericSalLayout&, HDC, bool bUseDWrite, bool bRenderingModeNatural);
318 public:
319 // public SalGraphics methods, the interface to the independent vcl part
321 // get device resolution
322 virtual void GetResolution( sal_Int32& rDPIX, sal_Int32& rDPIY ) override;
323 // get the depth of the device
324 virtual sal_uInt16 GetBitCount() const override;
325 // get the width of the device
326 virtual tools::Long GetGraphicsWidth() const override;
328 // set the clip region to empty
329 virtual void ResetClipRegion() override;
331 // set the line color to transparent (= don't draw lines)
332 virtual void SetLineColor() override;
333 // set the line color to a specific color
334 virtual void SetLineColor( Color nColor ) override;
335 // set the fill color to transparent (= don't fill)
336 virtual void SetFillColor() override;
337 // set the fill color to a specific color, shapes will be
338 // filled accordingly
339 virtual void SetFillColor( Color nColor ) override;
340 // enable/disable XOR drawing
341 virtual void SetXORMode( bool bSet, bool ) override;
342 // set line color for raster operations
343 virtual void SetROPLineColor( SalROPColor nROPColor ) override;
344 // set fill color for raster operations
345 virtual void SetROPFillColor( SalROPColor nROPColor ) override;
346 // set the text color to a specific color
347 virtual void SetTextColor( Color nColor ) override;
348 // set the font
349 virtual void SetFont( LogicalFontInstance*, int nFallbackLevel ) override;
350 // get the current font's metrics
351 virtual void GetFontMetric( FontMetricDataRef&, int nFallbackLevel ) override;
352 // get the repertoire of the current font
353 virtual FontCharMapRef GetFontCharMap() const override;
354 // get the layout capabilities of the current font
355 virtual bool GetFontCapabilities(vcl::FontCapabilities &rGetFontCapabilities) const override;
356 // graphics must fill supplied font list
357 virtual void GetDevFontList( vcl::font::PhysicalFontCollection* ) override;
358 // graphics must drop any cached font info
359 virtual void ClearDevFontCache() override;
360 virtual bool AddTempDevFont( vcl::font::PhysicalFontCollection*, const OUString& rFileURL, const OUString& rFontName ) override;
362 virtual std::unique_ptr<GenericSalLayout>
363 GetTextLayout(int nFallbackLevel) override;
364 virtual void DrawTextLayout( const GenericSalLayout& ) override;
366 virtual bool supportsOperation( OutDevSupportType ) const override;
368 virtual SystemGraphicsData GetGraphicsData() const override;
370 /// Update settings based on the platform values
371 static void updateSettingsNative( AllSettings& rSettings );
374 // Init/Deinit Graphics
375 void ImplUpdateSysColorEntries();
376 int ImplIsSysColorEntry( Color nColor );
377 void ImplGetLogFontFromFontSelect( const vcl::font::FontSelectPattern&,
378 const vcl::font::PhysicalFontFace*, LOGFONTW& );
380 #define MAX_64KSALPOINTS ((((sal_uInt16)0xFFFF)-8)/sizeof(POINTS))
382 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */