Avoid potential negative array index access to cached text.
[LibreOffice.git] / vcl / inc / quartz / salgdi.h
blobc8befcc502806166ccaabd5b1962c6574021f25d
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 <vector>
26 #include <tools/long.hxx>
28 #include <premac.h>
29 #ifdef MACOSX
30 #include <ApplicationServices/ApplicationServices.h>
31 #include <osx/osxvcltypes.h>
32 #include <osx/salframe.h>
33 #else
34 #include <CoreGraphics/CoreGraphics.h>
35 #include <CoreText/CoreText.h>
36 #endif
37 #include <postmac.h>
39 #ifdef IOS
40 // iOS defines a different Point class so include salgeom.hxx after postmac.h
41 // so that it will use the Point class in tools/gen.hxx
42 #include "salgeom.hxx"
43 #endif
45 #include <vcl/fontcapabilities.hxx>
46 #include <vcl/metric.hxx>
49 #include <font/LogicalFontInstance.hxx>
50 #include <font/FontMetricData.hxx>
51 #include <salgdi.hxx>
53 #include <quartz/salgdicommon.hxx>
55 #include <quartz/CGHelpers.hxx>
57 class AquaSalFrame;
58 class XorEmulation;
59 class CoreTextFont;
61 namespace sal::aqua
63 #ifdef MACOSX
64 NSRect getTotalScreenBounds();
65 void resetTotalScreenBounds();
66 #endif
67 float getWindowScaling();
68 void resetWindowScaling();
71 struct AquaSharedAttributes
73 /// path representing current clip region
74 CGMutablePathRef mxClipPath;
76 /// Drawing colors
77 /// pen color RGBA
78 RGBAColor maLineColor;
80 /// brush color RGBA
81 RGBAColor maFillColor;
83 // Graphics types
84 #ifdef MACOSX
85 AquaSalFrame* mpFrame;
86 /// is this a window graphics
87 bool mbWindow;
88 #else // IOS
89 // mirror AquaSalVirtualDevice::mbForeignContext for SvpSalGraphics objects related to such
90 bool mbForeignContext;
91 #endif
92 /// is this a printer graphics
93 bool mbPrinter;
94 /// is this a virtual device graphics
95 bool mbVirDev;
97 CGLayerHolder maLayer; // Quartz graphics layer
98 CGContextHolder maContextHolder; // Quartz drawing context
99 CGContextHolder maBGContextHolder; // Quartz drawing context for CGLayer
100 CGContextHolder maCSContextHolder; // Quartz drawing context considering the color space
101 int mnWidth;
102 int mnHeight;
103 int mnXorMode; // 0: off 1: on 2: invert only
104 int mnBitmapDepth; // zero unless bitmap
106 Color maTextColor;
107 /// allows text to be rendered without antialiasing
108 bool mbNonAntialiasedText;
110 std::unique_ptr<XorEmulation> mpXorEmulation;
112 AquaSharedAttributes()
113 : mxClipPath(nullptr)
114 , maLineColor(COL_WHITE)
115 , maFillColor(COL_BLACK)
116 #ifdef MACOSX
117 , mpFrame(nullptr)
118 , mbWindow(false)
119 #else
120 , mbForeignContext(false)
121 #endif
122 , mbPrinter(false)
123 , mbVirDev(false)
124 , mnWidth(0)
125 , mnHeight(0)
126 , mnXorMode(0)
127 , mnBitmapDepth(0)
128 , maTextColor( COL_BLACK )
129 , mbNonAntialiasedText( false )
132 void unsetClipPath()
134 if (mxClipPath)
136 CGPathRelease(mxClipPath);
137 mxClipPath = nullptr;
141 void unsetState()
143 unsetClipPath();
146 bool checkContext();
147 void setState();
149 bool isPenVisible() const
151 return maLineColor.IsVisible();
153 bool isBrushVisible() const
155 return maFillColor.IsVisible();
158 void refreshRect(float lX, float lY, float lWidth, float lHeight)
160 #ifdef MACOSX
161 if (!mbWindow) // view only on Window graphics
162 return;
164 if (mpFrame)
166 // update a little more around the designated rectangle
167 // this helps with antialiased rendering
168 // Rounding down x and width can accumulate a rounding error of up to 2
169 // The decrementing of x, the rounding error and the antialiasing border
170 // require that the width and the height need to be increased by four
171 const tools::Rectangle aVclRect(
172 Point(tools::Long(lX - 1), tools::Long(lY - 1)),
173 Size(tools::Long(lWidth + 4), tools::Long(lHeight + 4)));
175 mpFrame->maInvalidRect.Union(aVclRect);
177 #else
178 (void) lX;
179 (void) lY;
180 (void) lWidth;
181 (void) lHeight;
182 return;
183 #endif
186 // apply the XOR mask to the target context if active and dirty
187 void applyXorContext()
189 if (!mpXorEmulation)
190 return;
191 if (mpXorEmulation->UpdateTarget())
193 refreshRect(0, 0, mnWidth, mnHeight); // TODO: refresh minimal changerect
197 // differences between VCL, Quartz and kHiThemeOrientation coordinate systems
198 // make some graphics seem to be vertically-mirrored from a VCL perspective
199 bool isFlipped() const
201 #ifdef MACOSX
202 return mbWindow;
203 #else
204 return false;
205 #endif
209 class AquaGraphicsBackendBase
211 public:
212 virtual ~AquaGraphicsBackendBase() = 0;
213 AquaSharedAttributes& GetShared() { return mrShared; }
214 SalGraphicsImpl* GetImpl()
216 return mpImpl;
218 virtual void UpdateGeometryProvider(SalGeometryProvider*) {};
219 virtual bool drawNativeControl(ControlType nType,
220 ControlPart nPart,
221 const tools::Rectangle &rControlRegion,
222 ControlState nState,
223 const ImplControlValue &aValue) = 0;
224 virtual void drawTextLayout(const GenericSalLayout& layout) = 0;
225 virtual void Flush() {}
226 virtual void Flush( const tools::Rectangle& ) {}
227 virtual void WindowBackingPropertiesChanged() {};
228 protected:
229 AquaGraphicsBackendBase(AquaSharedAttributes& rShared, SalGraphicsImpl * impl)
230 : mrShared( rShared ), mpImpl(impl)
232 static bool performDrawNativeControl(ControlType nType,
233 ControlPart nPart,
234 const tools::Rectangle &rControlRegion,
235 ControlState nState,
236 const ImplControlValue &aValue,
237 CGContextRef context,
238 AquaSalFrame* mpFrame);
239 AquaSharedAttributes& mrShared;
240 private:
241 SalGraphicsImpl* mpImpl;
244 inline AquaGraphicsBackendBase::~AquaGraphicsBackendBase() {}
246 class AquaGraphicsBackend final : public SalGraphicsImpl, public AquaGraphicsBackendBase
248 private:
249 void drawPixelImpl( tools::Long nX, tools::Long nY, const RGBAColor& rColor); // helper to draw single pixels
251 #ifdef MACOSX
252 void refreshRect(const NSRect& rRect)
254 mrShared.refreshRect(rRect.origin.x, rRect.origin.y, rRect.size.width, rRect.size.height);
256 #else
257 void refreshRect(const CGRect& /*rRect*/)
259 #endif
261 void pattern50Fill();
263 #ifdef MACOSX
264 void copyScaledArea(tools::Long nDestX, tools::Long nDestY, tools::Long nSrcX, tools::Long nSrcY,
265 tools::Long nSrcWidth, tools::Long nSrcHeight, AquaSharedAttributes* pSrcShared);
266 #endif
268 public:
269 AquaGraphicsBackend(AquaSharedAttributes & rShared);
270 ~AquaGraphicsBackend() override;
272 void Init() override;
274 void freeResources() override;
276 OUString getRenderBackendName() const override
278 return "aqua";
281 void setClipRegion(vcl::Region const& rRegion) override;
282 void ResetClipRegion() override;
284 sal_uInt16 GetBitCount() const override;
286 tools::Long GetGraphicsWidth() const override;
288 void SetLineColor() override;
289 void SetLineColor(Color nColor) override;
290 void SetFillColor() override;
291 void SetFillColor(Color nColor) override;
292 void SetXORMode(bool bSet, bool bInvertOnly) override;
293 void SetROPLineColor(SalROPColor nROPColor) override;
294 void SetROPFillColor(SalROPColor nROPColor) override;
296 void drawPixel(tools::Long nX, tools::Long nY) override;
297 void drawPixel(tools::Long nX, tools::Long nY, Color nColor) override;
299 void drawLine(tools::Long nX1, tools::Long nY1, tools::Long nX2, tools::Long nY2) override;
300 void drawRect(tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight) override;
301 void drawPolyLine(sal_uInt32 nPoints, const Point* pPointArray) override;
302 void drawPolygon(sal_uInt32 nPoints, const Point* pPointArray) override;
303 void drawPolyPolygon(sal_uInt32 nPoly, const sal_uInt32* pPoints,
304 const Point** pPointArray) override;
306 void drawPolyPolygon(const basegfx::B2DHomMatrix& rObjectToDevice,
307 const basegfx::B2DPolyPolygon&, double fTransparency) override;
309 bool drawPolyLine(const basegfx::B2DHomMatrix& rObjectToDevice, const basegfx::B2DPolygon&,
310 double fTransparency, double fLineWidth, const std::vector<double>* pStroke,
311 basegfx::B2DLineJoin, css::drawing::LineCap, double fMiterMinimumAngle,
312 bool bPixelSnapHairline) override;
314 bool drawPolyLineBezier(sal_uInt32 nPoints, const Point* pPointArray,
315 const PolyFlags* pFlagArray) override;
317 bool drawPolygonBezier(sal_uInt32 nPoints, const Point* pPointArray,
318 const PolyFlags* pFlagArray) override;
320 bool drawPolyPolygonBezier(sal_uInt32 nPoly, const sal_uInt32* pPoints,
321 const Point* const* pPointArray,
322 const PolyFlags* const* pFlagArray) override;
324 void copyArea(tools::Long nDestX, tools::Long nDestY, tools::Long nSrcX, tools::Long nSrcY,
325 tools::Long nSrcWidth, tools::Long nSrcHeight, bool bWindowInvalidate) override;
327 void copyBits(const SalTwoRect& rPosAry, SalGraphics* pSrcGraphics) override;
329 void drawBitmap(const SalTwoRect& rPosAry, const SalBitmap& rSalBitmap) override;
331 void drawBitmap(const SalTwoRect& rPosAry, const SalBitmap& rSalBitmap,
332 const SalBitmap& rMaskBitmap) override;
334 void drawMask(const SalTwoRect& rPosAry, const SalBitmap& rSalBitmap,
335 Color nMaskColor) override;
337 std::shared_ptr<SalBitmap> getBitmap(tools::Long nX, tools::Long nY, tools::Long nWidth,
338 tools::Long nHeight) override;
340 Color getPixel(tools::Long nX, tools::Long nY) override;
342 void invert(tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight,
343 SalInvert nFlags) override;
345 void invert(sal_uInt32 nPoints, const Point* pPtAry, SalInvert nFlags) override;
347 bool drawEPS(tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight,
348 void* pPtr, sal_uInt32 nSize) override;
350 bool blendBitmap(const SalTwoRect&, const SalBitmap& rBitmap) override;
352 bool blendAlphaBitmap(const SalTwoRect&, const SalBitmap& rSrcBitmap,
353 const SalBitmap& rMaskBitmap, const SalBitmap& rAlphaBitmap) override;
355 bool drawAlphaBitmap(const SalTwoRect&, const SalBitmap& rSourceBitmap,
356 const SalBitmap& rAlphaBitmap) override;
358 bool drawTransformedBitmap(const basegfx::B2DPoint& rNull, const basegfx::B2DPoint& rX,
359 const basegfx::B2DPoint& rY, const SalBitmap& rSourceBitmap,
360 const SalBitmap* pAlphaBitmap, double fAlpha) override;
362 bool hasFastDrawTransformedBitmap() const override;
364 bool drawAlphaRect(tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight,
365 sal_uInt8 nTransparency) override;
367 bool drawGradient(const tools::PolyPolygon& rPolygon, const Gradient& rGradient) override;
368 bool implDrawGradient(basegfx::B2DPolyPolygon const& rPolyPolygon,
369 SalGradient const& rGradient) override;
371 virtual bool drawNativeControl(ControlType nType,
372 ControlPart nPart,
373 const tools::Rectangle &rControlRegion,
374 ControlState nState,
375 const ImplControlValue &aValue) override;
377 virtual void drawTextLayout(const GenericSalLayout& layout) override;
379 bool supportsOperation(OutDevSupportType eType) const override;
382 class AquaSalGraphics : public SalGraphicsAutoDelegateToImpl
384 AquaSharedAttributes maShared;
385 std::unique_ptr<AquaGraphicsBackendBase> mpBackend;
387 /// device resolution of this graphics
388 sal_Int32 mnRealDPIX;
389 sal_Int32 mnRealDPIY;
391 // Device Font settings
392 rtl::Reference<CoreTextFont> mpFont[MAX_FALLBACK];
394 public:
395 AquaSalGraphics(bool bPrinter = false);
396 virtual ~AquaSalGraphics() override;
398 void SetVirDevGraphics(SalVirtualDevice* pVirDev,CGLayerHolder const &rLayer, CGContextRef, int nBitDepth = 0);
399 #ifdef MACOSX
400 void initResolution( NSWindow* );
401 void copyResolution( AquaSalGraphics& );
402 void updateResolution();
404 void SetWindowGraphics( AquaSalFrame* pFrame );
405 bool IsWindowGraphics() const { return maShared.mbWindow; }
406 void SetPrinterGraphics(CGContextRef, sal_Int32 nRealDPIX, sal_Int32 nRealDPIY);
407 AquaSalFrame* getGraphicsFrame() const { return maShared.mpFrame; }
408 void setGraphicsFrame( AquaSalFrame* pFrame ) { maShared.mpFrame = pFrame; }
409 #endif
411 #ifdef MACOSX
412 void UpdateWindow( NSRect& ); // delivered in NSView coordinates
413 void RefreshRect(const NSRect& rRect)
415 maShared.refreshRect(rRect.origin.x, rRect.origin.y, rRect.size.width, rRect.size.height);
417 #else
418 void RefreshRect( const CGRect& ) {}
419 #endif
421 void Flush();
422 void Flush( const tools::Rectangle& );
423 void WindowBackingPropertiesChanged();
425 void UnsetState();
426 // InvalidateContext does an UnsetState and sets mrContext to 0
427 void InvalidateContext();
429 AquaGraphicsBackendBase* getAquaGraphicsBackend() const
431 return mpBackend.get();
434 virtual SalGraphicsImpl* GetImpl() const override;
436 #ifdef MACOSX
438 protected:
440 // native widget rendering methods that require mirroring
442 virtual bool isNativeControlSupported( ControlType nType, ControlPart nPart ) override;
444 virtual bool hitTestNativeControl( ControlType nType, ControlPart nPart, const tools::Rectangle& rControlRegion,
445 const Point& aPos, bool& rIsInside ) override;
446 virtual bool drawNativeControl( ControlType nType, ControlPart nPart, const tools::Rectangle& rControlRegion,
447 ControlState nState, const ImplControlValue& aValue,
448 const OUString& aCaption, const Color& rBackgroundColor ) override;
449 virtual bool getNativeControlRegion( ControlType nType, ControlPart nPart, const tools::Rectangle& rControlRegion, ControlState nState,
450 const ImplControlValue& aValue, const OUString& aCaption,
451 tools::Rectangle &rNativeBoundingRegion, tools::Rectangle &rNativeContentRegion ) override;
452 #endif
454 public:
455 // get device resolution
456 virtual void GetResolution( sal_Int32& rDPIX, sal_Int32& rDPIY ) override;
457 // set the text color to a specific color
458 virtual void SetTextColor( Color nColor ) override;
459 // set the font
460 virtual void SetFont( LogicalFontInstance*, int nFallbackLevel ) override;
461 // get the current font's metrics
462 virtual void GetFontMetric( FontMetricDataRef&, int nFallbackLevel ) override;
463 // get the repertoire of the current font
464 virtual FontCharMapRef GetFontCharMap() const override;
465 virtual bool GetFontCapabilities(vcl::FontCapabilities &rFontCapabilities) const override;
466 // graphics must fill supplied font list
467 virtual void GetDevFontList( vcl::font::PhysicalFontCollection* ) override;
468 // graphics must drop any cached font info
469 virtual void ClearDevFontCache() override;
470 virtual bool AddTempDevFont( vcl::font::PhysicalFontCollection*, const OUString& rFileURL, const OUString& rFontName ) override;
472 virtual std::unique_ptr<GenericSalLayout>
473 GetTextLayout(int nFallbackLevel) override;
474 virtual void DrawTextLayout( const GenericSalLayout& ) override;
476 virtual SystemGraphicsData
477 GetGraphicsData() const override;
480 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */