1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #ifndef INCLUDED_VCL_INC_HEADLESS_SVPGDI_HXX
21 #define INCLUDED_VCL_INC_HEADLESS_SVPGDI_HXX
22 
23 #ifdef IOS
24 #error This file is not for iOS
25 #endif
26 
27 #include <osl/endian.h>
28 #include <vcl/sysdata.hxx>
29 #include <config_cairo_canvas.h>
30 
31 #include <salgdi.hxx>
32 #include <sallayout.hxx>
33 #include "svpcairotextrender.hxx"
34 #include <impfontmetricdata.hxx>
35 
36 #include <cairo.h>
37 
38 //Using formats that match cairo's formats. For android we patch cairo,
39 //which is internal in that case, to swap the rgb components so that
40 //cairo then matches the OpenGL GL_RGBA format so we can use it there
41 //where we don't have GL_BGRA support.
42 // SVP_24BIT_FORMAT is used to store 24-bit images in 3-byte pixels to conserve memory.
43 #if defined ANDROID
44 #   define SVP_24BIT_FORMAT (ScanlineFormat::N24BitTcRgb | ScanlineFormat::TopDown)
45 #   define SVP_CAIRO_FORMAT (ScanlineFormat::N32BitTcRgba | ScanlineFormat::TopDown)
46 #   define SVP_CAIRO_BLUE 1
47 #   define SVP_CAIRO_GREEN 2
48 #   define SVP_CAIRO_RED 0
49 #   define SVP_CAIRO_ALPHA 3
50 #elif defined OSL_BIGENDIAN
51 #   define SVP_24BIT_FORMAT (ScanlineFormat::N24BitTcRgb | ScanlineFormat::TopDown)
52 #   define SVP_CAIRO_FORMAT (ScanlineFormat::N32BitTcArgb | ScanlineFormat::TopDown)
53 #   define SVP_CAIRO_BLUE 3
54 #   define SVP_CAIRO_GREEN 2
55 #   define SVP_CAIRO_RED 1
56 #   define SVP_CAIRO_ALPHA 0
57 #else
58 #   define SVP_24BIT_FORMAT (ScanlineFormat::N24BitTcBgr | ScanlineFormat::TopDown)
59 #   define SVP_CAIRO_FORMAT (ScanlineFormat::N32BitTcBgra | ScanlineFormat::TopDown)
60 #   define SVP_CAIRO_BLUE 0
61 #   define SVP_CAIRO_GREEN 1
62 #   define SVP_CAIRO_RED 2
63 #   define SVP_CAIRO_ALPHA 3
64 #endif
65 
66 struct BitmapBuffer;
67 class GlyphCache;
68 class FreetypeFont;
69 typedef struct _cairo cairo_t;
70 typedef struct _cairo_surface cairo_surface_t;
71 typedef struct _cairo_user_data_key cairo_user_data_key_t;
72 
73 VCL_DLLPUBLIC void dl_cairo_surface_set_device_scale(cairo_surface_t *surface, double x_scale, double y_scale);
74 VCL_DLLPUBLIC void dl_cairo_surface_get_device_scale(cairo_surface_t *surface, double *x_scale, double *y_scale);
75 
76 enum class PaintMode { Over, Xor };
77 
78 typedef void (*damageHandler)(void* handle,
79                               sal_Int32 nExtentsX, sal_Int32 nExtentsY,
80                               sal_Int32 nExtentsWidth, sal_Int32 nExtentsHeight);
81 
82 struct VCL_DLLPUBLIC DamageHandler
83 {
84     void *handle;
85     damageHandler damaged;
86 };
87 
88 class VCL_DLLPUBLIC SvpSalGraphics : public SalGraphics
89 {
90     cairo_surface_t*               m_pSurface;
91     basegfx::B2IVector             m_aFrameSize;
92     double                         m_fScale;
93     Color                          m_aLineColor;
94     Color                          m_aFillColor;
95     PaintMode                      m_ePaintMode;
96 
97 public:
98     static GlyphCache& getPlatformGlyphCache();
99     void setSurface(cairo_surface_t* pSurface, const basegfx::B2IVector& rSize);
getSurface() const100     cairo_surface_t* getSurface() const { return m_pSurface; }
101     static cairo_user_data_key_t* getDamageKey();
102 
103     static void clipRegion(cairo_t* cr, const vcl::Region& rClipRegion);
104 
105     // need this static version of ::drawPolyLine for usage from
106     // vcl/unx/generic/gdi/salgdi.cxx. It gets wrapped by
107     // ::drawPolyLine with some added parameters (see there)
108     static bool drawPolyLine(
109         cairo_t* cr,
110         basegfx::B2DRange* pExtents,
111         const Color& rLineColor,
112         bool bAntiAliasB2DDraw,
113         const basegfx::B2DHomMatrix& rObjectToDevice,
114         const basegfx::B2DPolygon& rPolyLine,
115         double fTransparency,
116         const basegfx::B2DVector& rLineWidths,
117         basegfx::B2DLineJoin eLineJoin,
118         css::drawing::LineCap eLineCap,
119         double fMiterMinimumAngle,
120         bool bPixelSnapHairline);
121 
122     void copySource(const SalTwoRect& rTR, cairo_surface_t* source);
123     void copyWithOperator(const SalTwoRect& rTR, cairo_surface_t* source,
124                           cairo_operator_t eOp = CAIRO_OPERATOR_SOURCE);
125 
126 private:
127     void invert(const basegfx::B2DPolygon &rPoly, SalInvert nFlags);
128     void applyColor(cairo_t *cr, Color rColor, double fTransparency = 0.0);
129 
130 protected:
131     vcl::Region                         m_aClipRegion;
132     SvpCairoTextRender                  m_aTextRenderImpl;
133 
134 protected:
135     virtual bool blendBitmap( const SalTwoRect&, const SalBitmap& rBitmap ) override;
136     virtual bool blendAlphaBitmap( const SalTwoRect&,
137                                    const SalBitmap& rSrcBitmap,
138                                    const SalBitmap& rMaskBitmap,
139                                    const SalBitmap& rAlphaBitmap ) override;
140     virtual bool drawAlphaBitmap( const SalTwoRect&, const SalBitmap& rSourceBitmap, const SalBitmap& rAlphaBitmap ) override;
141     virtual bool drawTransformedBitmap(
142         const basegfx::B2DPoint& rNull,
143         const basegfx::B2DPoint& rX,
144         const basegfx::B2DPoint& rY,
145         const SalBitmap& rSourceBitmap,
146         const SalBitmap* pAlphaBitmap) override;
147     virtual bool drawAlphaRect( long nX, long nY, long nWidth, long nHeight, sal_uInt8 nTransparency ) override;
148 
149     cairo_t* createTmpCompatibleCairoContext() const;
150 
151 public:
152     SvpSalGraphics();
153     virtual ~SvpSalGraphics() override;
154 
GetImpl() const155     virtual SalGraphicsImpl* GetImpl() const override { return nullptr; };
156     virtual void            GetResolution( sal_Int32& rDPIX, sal_Int32& rDPIY ) override;
157     virtual sal_uInt16      GetBitCount() const override;
158     virtual long            GetGraphicsWidth() const override;
159 
160     virtual void            ResetClipRegion() override;
161     virtual bool            setClipRegion( const vcl::Region& ) override;
162 
163     virtual void            SetLineColor() override;
164     virtual void            SetLineColor( Color nColor ) override;
165     virtual void            SetFillColor() override;
166     virtual void            SetFillColor( Color nColor ) override;
167 
168     virtual void            SetXORMode( bool bSet, bool ) override;
169 
170     virtual void            SetROPLineColor( SalROPColor nROPColor ) override;
171     virtual void            SetROPFillColor( SalROPColor nROPColor ) override;
172 
173     virtual void            SetTextColor( Color nColor ) override;
174     virtual void            SetFont(LogicalFontInstance*, int nFallbackLevel) override;
175     virtual void            GetFontMetric( ImplFontMetricDataRef&, int nFallbackLevel ) override;
176     virtual FontCharMapRef  GetFontCharMap() const override;
177     virtual bool GetFontCapabilities(vcl::FontCapabilities &rFontCapabilities) const override;
178     virtual void            GetDevFontList( PhysicalFontCollection* ) override;
179     virtual void ClearDevFontCache() override;
180     virtual bool            AddTempDevFont( PhysicalFontCollection*, const OUString& rFileURL, const OUString& rFontName ) override;
181     virtual bool        CreateFontSubset( const OUString& rToFile,
182                                               const PhysicalFontFace*,
183                                               const sal_GlyphId* pGlyphIds,
184                                               const sal_uInt8* pEncoding,
185                                               sal_Int32* pWidths,
186                                               int nGlyphs,
187                                               FontSubsetInfo& rInfo
188                                               ) override;
189     virtual const void*     GetEmbedFontData(const PhysicalFontFace*, long* pDataLen) override;
190     virtual void            FreeEmbedFontData( const void* pData, long nDataLen ) override;
191     virtual void            GetGlyphWidths( const PhysicalFontFace*,
192                                             bool bVertical,
193                                             std::vector< sal_Int32 >& rWidths,
194                                             Ucs2UIntMap& rUnicodeEnc ) override;
195     virtual std::unique_ptr<GenericSalLayout>
196                             GetTextLayout(int nFallbackLevel) override;
197     virtual void            DrawTextLayout( const GenericSalLayout& ) override;
198     virtual bool            supportsOperation( OutDevSupportType ) const override;
199     virtual void            drawPixel( long nX, long nY ) override;
200     virtual void            drawPixel( long nX, long nY, Color nColor ) override;
201     virtual void            drawLine( long nX1, long nY1, long nX2, long nY2 ) override;
202     virtual void            drawRect( long nX, long nY, long nWidth, long nHeight ) override;
203 
204     virtual bool            drawPolyPolygon(
205                                 const basegfx::B2DHomMatrix& rObjectToDevice,
206                                 const basegfx::B2DPolyPolygon&,
207                                 double fTransparency ) override;
208 
209     virtual bool            drawPolyLine(
210                                 const basegfx::B2DHomMatrix& rObjectToDevice,
211                                 const basegfx::B2DPolygon&,
212                                 double fTransparency,
213                                 const basegfx::B2DVector& rLineWidths,
214                                 basegfx::B2DLineJoin,
215                                 css::drawing::LineCap,
216                                 double fMiterMinimumAngle,
217                                 bool bPixelSnapHairline) override;
218     virtual void            drawPolyLine( sal_uInt32 nPoints, const SalPoint* pPtAry ) override;
219     virtual void            drawPolygon( sal_uInt32 nPoints, const SalPoint* pPtAry ) override;
220     virtual void            drawPolyPolygon( sal_uInt32 nPoly,
221                                              const sal_uInt32* pPoints,
222                                              PCONSTSALPOINT* pPtAry ) override;
223     virtual bool        drawPolyLineBezier( sal_uInt32 nPoints,
224                                                 const SalPoint* pPtAry,
225                                                 const PolyFlags* pFlgAry ) override;
226     virtual bool        drawPolygonBezier( sal_uInt32 nPoints,
227                                                const SalPoint* pPtAry,
228                                                const PolyFlags* pFlgAry ) override;
229     virtual bool        drawPolyPolygonBezier( sal_uInt32 nPoly,
230                                                    const sal_uInt32* pPoints,
231                                                    const SalPoint* const* pPtAry,
232                                                    const PolyFlags* const* pFlgAry ) override;
drawGradient(const tools::PolyPolygon &,const Gradient &)233     virtual bool            drawGradient( const tools::PolyPolygon&, const Gradient& ) override { return false; };
234 
235     virtual bool implDrawGradient(basegfx::B2DPolyPolygon const & rPolyPolygon, SalGradient const & rGradient) override;
236 
237     virtual void            copyArea( long nDestX,
238                                       long nDestY,
239                                       long nSrcX,
240                                       long nSrcY,
241                                       long nSrcWidth,
242                                       long nSrcHeight,
243                                       bool bWindowInvalidate) override;
244     virtual void            copyBits( const SalTwoRect& rPosAry,
245                                       SalGraphics* pSrcGraphics ) override;
246     virtual void            drawBitmap( const SalTwoRect& rPosAry,
247                                         const SalBitmap& rSalBitmap ) override;
248     void                    drawBitmap( const SalTwoRect& rPosAry,
249                                         const BitmapBuffer* pBuffer,
250                                         cairo_operator_t eOp );
251     virtual void            drawBitmap( const SalTwoRect& rPosAry,
252                                         const SalBitmap& rSalBitmap,
253                                         const SalBitmap& rTransparentBitmap ) override;
254     virtual void            drawMask( const SalTwoRect& rPosAry,
255                                       const SalBitmap& rSalBitmap,
256                                       Color nMaskColor ) override;
257     virtual std::shared_ptr<SalBitmap> getBitmap( long nX, long nY, long nWidth, long nHeight ) override;
258     virtual Color           getPixel( long nX, long nY ) override;
259     virtual void            invert( long nX, long nY, long nWidth, long nHeight, SalInvert nFlags ) override;
260     virtual void            invert( sal_uInt32 nPoints, const SalPoint* pPtAry, SalInvert nFlags ) override;
261 
262     virtual bool        drawEPS( long nX, long nY, long nWidth, long nHeight, void* pPtr, sal_uInt32 nSize ) override;
263 
264     virtual SystemGraphicsData GetGraphicsData() const override;
265 
266 
267 #if ENABLE_CAIRO_CANVAS
268     virtual bool            SupportsCairo() const override;
269     virtual cairo::SurfaceSharedPtr CreateSurface(const cairo::CairoSurfaceSharedPtr& rSurface) const override;
270     virtual cairo::SurfaceSharedPtr CreateSurface(const OutputDevice& rRefDevice, int x, int y, int width, int height) const override;
271     virtual cairo::SurfaceSharedPtr CreateBitmapSurface(const OutputDevice& rRefDevice, const BitmapSystemData& rData, const Size& rSize) const override;
272     virtual css::uno::Any   GetNativeSurfaceHandle(cairo::SurfaceSharedPtr& rSurface, const basegfx::B2ISize& rSize) const override;
273     virtual SystemFontData  GetSysFontData( int nFallbacklevel ) const override;
274 #endif // ENABLE_CAIRO_CANVAS
275 
276     cairo_t*                getCairoContext(bool bXorModeAllowed) const;
277     void                    releaseCairoContext(cairo_t* cr, bool bXorModeAllowed, const basegfx::B2DRange& rExtents) const;
278     static cairo_surface_t* createCairoSurface(const BitmapBuffer *pBuffer);
279     void                    clipRegion(cairo_t* cr);
280 };
281 
282 #endif // INCLUDED_VCL_INC_HEADLESS_SVPGDI_HXX
283 
284 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
285