1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include <sal/config.h>
26 #include <tools/long.hxx>
30 #include <ApplicationServices/ApplicationServices.h>
31 #include <osx/osxvcltypes.h>
32 #include <osx/salframe.h>
34 #include <CoreGraphics/CoreGraphics.h>
35 #include <CoreText/CoreText.h>
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"
45 #include <vcl/fontcapabilities.hxx>
46 #include <vcl/metric.hxx>
49 #include <font/LogicalFontInstance.hxx>
50 #include <font/FontMetricData.hxx>
53 #include <quartz/salgdicommon.hxx>
55 #include <quartz/CGHelpers.hxx>
64 NSRect
getTotalScreenBounds();
65 void resetTotalScreenBounds();
67 float getWindowScaling();
68 void resetWindowScaling();
71 struct AquaSharedAttributes
73 /// path representing current clip region
74 CGMutablePathRef mxClipPath
;
78 RGBAColor maLineColor
;
81 RGBAColor maFillColor
;
85 AquaSalFrame
* mpFrame
;
86 /// is this a window graphics
89 // mirror AquaSalVirtualDevice::mbForeignContext for SvpSalGraphics objects related to such
90 bool mbForeignContext
;
92 /// is this a printer graphics
94 /// is this a virtual device graphics
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
103 int mnXorMode
; // 0: off 1: on 2: invert only
104 int mnBitmapDepth
; // zero unless bitmap
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
)
120 , mbForeignContext(false)
128 , maTextColor( COL_BLACK
)
129 , mbNonAntialiasedText( false )
136 CGPathRelease(mxClipPath
);
137 mxClipPath
= nullptr;
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
)
161 if (!mbWindow
) // view only on Window graphics
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
);
186 // apply the XOR mask to the target context if active and dirty
187 void applyXorContext()
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
209 class AquaGraphicsBackendBase
212 virtual ~AquaGraphicsBackendBase() = 0;
213 AquaSharedAttributes
& GetShared() { return mrShared
; }
214 SalGraphicsImpl
* GetImpl()
218 virtual void UpdateGeometryProvider(SalGeometryProvider
*) {};
219 virtual bool drawNativeControl(ControlType nType
,
221 const tools::Rectangle
&rControlRegion
,
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() {};
229 AquaGraphicsBackendBase(AquaSharedAttributes
& rShared
, SalGraphicsImpl
* impl
)
230 : mrShared( rShared
), mpImpl(impl
)
232 static bool performDrawNativeControl(ControlType nType
,
234 const tools::Rectangle
&rControlRegion
,
236 const ImplControlValue
&aValue
,
237 CGContextRef context
,
238 AquaSalFrame
* mpFrame
);
239 AquaSharedAttributes
& mrShared
;
241 SalGraphicsImpl
* mpImpl
;
244 inline AquaGraphicsBackendBase::~AquaGraphicsBackendBase() {}
246 class AquaGraphicsBackend final
: public SalGraphicsImpl
, public AquaGraphicsBackendBase
249 void drawPixelImpl( tools::Long nX
, tools::Long nY
, const RGBAColor
& rColor
); // helper to draw single pixels
252 void refreshRect(const NSRect
& rRect
)
254 mrShared
.refreshRect(rRect
.origin
.x
, rRect
.origin
.y
, rRect
.size
.width
, rRect
.size
.height
);
257 void refreshRect(const CGRect
& /*rRect*/)
261 void pattern50Fill();
264 void copyScaledArea(tools::Long nDestX
, tools::Long nDestY
, tools::Long nSrcX
, tools::Long nSrcY
,
265 tools::Long nSrcWidth
, tools::Long nSrcHeight
, AquaSharedAttributes
* pSrcShared
);
269 AquaGraphicsBackend(AquaSharedAttributes
& rShared
);
270 ~AquaGraphicsBackend() override
;
272 OUString
getRenderBackendName() const override
277 void setClipRegion(vcl::Region
const& rRegion
) override
;
278 void ResetClipRegion() override
;
280 sal_uInt16
GetBitCount() const override
;
282 tools::Long
GetGraphicsWidth() const override
;
284 void SetLineColor() override
;
285 void SetLineColor(Color nColor
) override
;
286 void SetFillColor() override
;
287 void SetFillColor(Color nColor
) override
;
288 void SetXORMode(bool bSet
, bool bInvertOnly
) override
;
289 void SetROPLineColor(SalROPColor nROPColor
) override
;
290 void SetROPFillColor(SalROPColor nROPColor
) override
;
292 void drawPixel(tools::Long nX
, tools::Long nY
) override
;
293 void drawPixel(tools::Long nX
, tools::Long nY
, Color nColor
) override
;
295 void drawLine(tools::Long nX1
, tools::Long nY1
, tools::Long nX2
, tools::Long nY2
) override
;
296 void drawRect(tools::Long nX
, tools::Long nY
, tools::Long nWidth
, tools::Long nHeight
) override
;
297 void drawPolyLine(sal_uInt32 nPoints
, const Point
* pPointArray
) override
;
298 void drawPolygon(sal_uInt32 nPoints
, const Point
* pPointArray
) override
;
299 void drawPolyPolygon(sal_uInt32 nPoly
, const sal_uInt32
* pPoints
,
300 const Point
** pPointArray
) override
;
302 void drawPolyPolygon(const basegfx::B2DHomMatrix
& rObjectToDevice
,
303 const basegfx::B2DPolyPolygon
&, double fTransparency
) override
;
305 bool drawPolyLine(const basegfx::B2DHomMatrix
& rObjectToDevice
, const basegfx::B2DPolygon
&,
306 double fTransparency
, double fLineWidth
, const std::vector
<double>* pStroke
,
307 basegfx::B2DLineJoin
, css::drawing::LineCap
, double fMiterMinimumAngle
,
308 bool bPixelSnapHairline
) override
;
310 bool drawPolyLineBezier(sal_uInt32 nPoints
, const Point
* pPointArray
,
311 const PolyFlags
* pFlagArray
) override
;
313 bool drawPolygonBezier(sal_uInt32 nPoints
, const Point
* pPointArray
,
314 const PolyFlags
* pFlagArray
) override
;
316 bool drawPolyPolygonBezier(sal_uInt32 nPoly
, const sal_uInt32
* pPoints
,
317 const Point
* const* pPointArray
,
318 const PolyFlags
* const* pFlagArray
) override
;
320 void copyArea(tools::Long nDestX
, tools::Long nDestY
, tools::Long nSrcX
, tools::Long nSrcY
,
321 tools::Long nSrcWidth
, tools::Long nSrcHeight
, bool bWindowInvalidate
) override
;
323 void copyBits(const SalTwoRect
& rPosAry
, SalGraphics
* pSrcGraphics
) override
;
325 void drawBitmap(const SalTwoRect
& rPosAry
, const SalBitmap
& rSalBitmap
) override
;
327 void drawBitmap(const SalTwoRect
& rPosAry
, const SalBitmap
& rSalBitmap
,
328 const SalBitmap
& rMaskBitmap
) override
;
330 void drawMask(const SalTwoRect
& rPosAry
, const SalBitmap
& rSalBitmap
,
331 Color nMaskColor
) override
;
333 std::shared_ptr
<SalBitmap
> getBitmap(tools::Long nX
, tools::Long nY
, tools::Long nWidth
,
334 tools::Long nHeight
) override
;
336 Color
getPixel(tools::Long nX
, tools::Long nY
) override
;
338 void invert(tools::Long nX
, tools::Long nY
, tools::Long nWidth
, tools::Long nHeight
,
339 SalInvert nFlags
) override
;
341 void invert(sal_uInt32 nPoints
, const Point
* pPtAry
, SalInvert nFlags
) override
;
343 bool drawEPS(tools::Long nX
, tools::Long nY
, tools::Long nWidth
, tools::Long nHeight
,
344 void* pPtr
, sal_uInt32 nSize
) override
;
346 bool blendBitmap(const SalTwoRect
&, const SalBitmap
& rBitmap
) override
;
348 bool blendAlphaBitmap(const SalTwoRect
&, const SalBitmap
& rSrcBitmap
,
349 const SalBitmap
& rMaskBitmap
, const SalBitmap
& rAlphaBitmap
) override
;
351 bool drawAlphaBitmap(const SalTwoRect
&, const SalBitmap
& rSourceBitmap
,
352 const SalBitmap
& rAlphaBitmap
) override
;
354 bool drawTransformedBitmap(const basegfx::B2DPoint
& rNull
, const basegfx::B2DPoint
& rX
,
355 const basegfx::B2DPoint
& rY
, const SalBitmap
& rSourceBitmap
,
356 const SalBitmap
* pAlphaBitmap
, double fAlpha
) override
;
358 bool drawAlphaRect(tools::Long nX
, tools::Long nY
, tools::Long nWidth
, tools::Long nHeight
,
359 sal_uInt8 nTransparency
) override
;
361 bool drawGradient(const tools::PolyPolygon
& rPolygon
, const Gradient
& rGradient
) override
;
362 bool implDrawGradient(basegfx::B2DPolyPolygon
const& rPolyPolygon
,
363 SalGradient
const& rGradient
) override
;
365 virtual bool drawNativeControl(ControlType nType
,
367 const tools::Rectangle
&rControlRegion
,
369 const ImplControlValue
&aValue
) override
;
371 virtual void drawTextLayout(const GenericSalLayout
& layout
) override
;
373 bool supportsOperation(OutDevSupportType eType
) const override
;
376 class AquaSalGraphics
: public SalGraphicsAutoDelegateToImpl
378 AquaSharedAttributes maShared
;
379 std::unique_ptr
<AquaGraphicsBackendBase
> mpBackend
;
381 /// device resolution of this graphics
382 sal_Int32 mnRealDPIX
;
383 sal_Int32 mnRealDPIY
;
385 // Device Font settings
386 rtl::Reference
<CoreTextFont
> mpFont
[MAX_FALLBACK
];
389 AquaSalGraphics(bool bPrinter
= false);
390 virtual ~AquaSalGraphics() override
;
392 void SetVirDevGraphics(SalVirtualDevice
* pVirDev
,CGLayerHolder
const &rLayer
, CGContextRef
, int nBitDepth
= 0);
394 void initResolution( NSWindow
* );
395 void copyResolution( AquaSalGraphics
& );
396 void updateResolution();
398 void SetWindowGraphics( AquaSalFrame
* pFrame
);
399 bool IsWindowGraphics() const { return maShared
.mbWindow
; }
400 void SetPrinterGraphics(CGContextRef
, sal_Int32 nRealDPIX
, sal_Int32 nRealDPIY
);
401 AquaSalFrame
* getGraphicsFrame() const { return maShared
.mpFrame
; }
402 void setGraphicsFrame( AquaSalFrame
* pFrame
) { maShared
.mpFrame
= pFrame
; }
406 void UpdateWindow( NSRect
& ); // delivered in NSView coordinates
407 void RefreshRect(const NSRect
& rRect
)
409 maShared
.refreshRect(rRect
.origin
.x
, rRect
.origin
.y
, rRect
.size
.width
, rRect
.size
.height
);
412 void RefreshRect( const CGRect
& ) {}
416 void Flush( const tools::Rectangle
& );
417 void WindowBackingPropertiesChanged();
420 // InvalidateContext does an UnsetState and sets mrContext to 0
421 void InvalidateContext();
423 AquaGraphicsBackendBase
* getAquaGraphicsBackend() const
425 return mpBackend
.get();
428 virtual SalGraphicsImpl
* GetImpl() const override
;
434 // native widget rendering methods that require mirroring
436 virtual bool isNativeControlSupported( ControlType nType
, ControlPart nPart
) override
;
438 virtual bool hitTestNativeControl( ControlType nType
, ControlPart nPart
, const tools::Rectangle
& rControlRegion
,
439 const Point
& aPos
, bool& rIsInside
) override
;
440 virtual bool drawNativeControl( ControlType nType
, ControlPart nPart
, const tools::Rectangle
& rControlRegion
,
441 ControlState nState
, const ImplControlValue
& aValue
,
442 const OUString
& aCaption
, const Color
& rBackgroundColor
) override
;
444 virtual bool getNativeControlRegion( ControlType nType
, ControlPart nPart
, const tools::Rectangle
& rControlRegion
, ControlState nState
,
445 const ImplControlValue
& aValue
, const OUString
& aCaption
,
446 tools::Rectangle
&rNativeBoundingRegion
, tools::Rectangle
&rNativeContentRegion
) override
;
450 // get device resolution
451 virtual void GetResolution( sal_Int32
& rDPIX
, sal_Int32
& rDPIY
) override
;
452 // set the text color to a specific color
453 virtual void SetTextColor( Color nColor
) override
;
455 virtual void SetFont( LogicalFontInstance
*, int nFallbackLevel
) override
;
456 // get the current font's metrics
457 virtual void GetFontMetric( FontMetricDataRef
&, int nFallbackLevel
) override
;
458 // get the repertoire of the current font
459 virtual FontCharMapRef
GetFontCharMap() const override
;
460 virtual bool GetFontCapabilities(vcl::FontCapabilities
&rFontCapabilities
) const override
;
461 // graphics must fill supplied font list
462 virtual void GetDevFontList( vcl::font::PhysicalFontCollection
* ) override
;
463 // graphics must drop any cached font info
464 virtual void ClearDevFontCache() override
;
465 virtual bool AddTempDevFont( vcl::font::PhysicalFontCollection
*, const OUString
& rFileURL
, const OUString
& rFontName
) override
;
467 virtual std::unique_ptr
<GenericSalLayout
>
468 GetTextLayout(int nFallbackLevel
) override
;
469 virtual void DrawTextLayout( const GenericSalLayout
& ) override
;
472 virtual bool ShouldDownscaleIconsAtSurface(double& rScaleOut
) const override
;
475 virtual SystemGraphicsData
476 GetGraphicsData() const override
;
479 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */