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 #include <memory>
21 #ifndef IOS
22 #include <headless/svpgdi.hxx>
23 #endif
24 #include <headless/svpbmp.hxx>
25 #include <headless/svpframe.hxx>
26 #include <headless/svpcairotextrender.hxx>
27 #include <headless/CustomWidgetDraw.hxx>
28 #include <saldatabasic.hxx>
29 
30 #include <sal/log.hxx>
31 #include <tools/helpers.hxx>
32 #include <o3tl/safeint.hxx>
33 #include <vcl/BitmapTools.hxx>
34 #include <vcl/sysdata.hxx>
35 #include <vcl/gradient.hxx>
36 #include <config_cairo_canvas.h>
37 #include <basegfx/numeric/ftools.hxx>
38 #include <basegfx/range/b2drange.hxx>
39 #include <basegfx/range/b2ibox.hxx>
40 #include <basegfx/range/b2irange.hxx>
41 #include <basegfx/polygon/b2dpolypolygon.hxx>
42 #include <basegfx/polygon/b2dpolypolygontools.hxx>
43 #include <basegfx/polygon/b2dpolygon.hxx>
44 #include <basegfx/polygon/b2dpolygontools.hxx>
45 #include <basegfx/matrix/b2dhommatrix.hxx>
46 #include <basegfx/utils/canvastools.hxx>
47 #include <basegfx/utils/systemdependentdata.hxx>
48 #include <basegfx/matrix/b2dhommatrixtools.hxx>
49 #include <comphelper/lok.hxx>
50 #include <unx/gendata.hxx>
51 #include <dlfcn.h>
52 
53 #if ENABLE_CAIRO_CANVAS
54 #   if defined CAIRO_VERSION && CAIRO_VERSION < CAIRO_VERSION_ENCODE(1, 10, 0)
55 #      define CAIRO_OPERATOR_DIFFERENCE (static_cast<cairo_operator_t>(23))
56 #   endif
57 #endif
58 
59 namespace
60 {
getClipBox(cairo_t * cr)61     basegfx::B2DRange getClipBox(cairo_t* cr)
62     {
63         double x1, y1, x2, y2;
64 
65         cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
66 
67         // support B2DRange::isEmpty()
68         if(0.0 != x1 || 0.0 != y1 || 0.0 != x2 || 0.0 != y2)
69         {
70             return basegfx::B2DRange(x1, y1, x2, y2);
71         }
72 
73         return basegfx::B2DRange();
74     }
75 
getFillDamage(cairo_t * cr)76     basegfx::B2DRange getFillDamage(cairo_t* cr)
77     {
78         double x1, y1, x2, y2;
79 
80         // this is faster than cairo_fill_extents, at the cost of some overdraw
81         cairo_path_extents(cr, &x1, &y1, &x2, &y2);
82 
83         // support B2DRange::isEmpty()
84         if(0.0 != x1 || 0.0 != y1 || 0.0 != x2 || 0.0 != y2)
85         {
86             return basegfx::B2DRange(x1, y1, x2, y2);
87         }
88 
89         return basegfx::B2DRange();
90     }
91 
getClippedFillDamage(cairo_t * cr)92     basegfx::B2DRange getClippedFillDamage(cairo_t* cr)
93     {
94         basegfx::B2DRange aDamageRect(getFillDamage(cr));
95         aDamageRect.intersect(getClipBox(cr));
96         return aDamageRect;
97     }
98 
getStrokeDamage(cairo_t * cr)99     basegfx::B2DRange getStrokeDamage(cairo_t* cr)
100     {
101         double x1, y1, x2, y2;
102 
103         cairo_stroke_extents(cr, &x1, &y1, &x2, &y2);
104 
105         // support B2DRange::isEmpty()
106         if(0.0 != x1 || 0.0 != y1 || 0.0 != x2 || 0.0 != y2)
107         {
108             return basegfx::B2DRange(x1, y1, x2, y2);
109         }
110 
111         return basegfx::B2DRange();
112     }
113 
getClippedStrokeDamage(cairo_t * cr)114     basegfx::B2DRange getClippedStrokeDamage(cairo_t* cr)
115     {
116         basegfx::B2DRange aDamageRect(getStrokeDamage(cr));
117         aDamageRect.intersect(getClipBox(cr));
118         return aDamageRect;
119     }
120 }
121 
blendBitmap(const SalTwoRect &,const SalBitmap &)122 bool SvpSalGraphics::blendBitmap( const SalTwoRect&, const SalBitmap& /*rBitmap*/ )
123 {
124     SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::blendBitmap case");
125     return false;
126 }
127 
blendAlphaBitmap(const SalTwoRect &,const SalBitmap &,const SalBitmap &,const SalBitmap &)128 bool SvpSalGraphics::blendAlphaBitmap( const SalTwoRect&, const SalBitmap&, const SalBitmap&, const SalBitmap& )
129 {
130     SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::blendAlphaBitmap case");
131     return false;
132 }
133 
134 namespace
135 {
getCairoFormat(const BitmapBuffer & rBuffer)136     cairo_format_t getCairoFormat(const BitmapBuffer& rBuffer)
137     {
138         cairo_format_t nFormat;
139 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
140         assert(rBuffer.mnBitCount == 32 || rBuffer.mnBitCount == 24 || rBuffer.mnBitCount == 1);
141 #else
142         assert(rBuffer.mnBitCount == 32 || rBuffer.mnBitCount == 1);
143 #endif
144 
145         if (rBuffer.mnBitCount == 32)
146             nFormat = CAIRO_FORMAT_ARGB32;
147 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
148         else if (rBuffer.mnBitCount == 24)
149             nFormat = CAIRO_FORMAT_RGB24_888;
150 #endif
151         else
152             nFormat = CAIRO_FORMAT_A1;
153         return nFormat;
154     }
155 
Toggle1BitTransparency(const BitmapBuffer & rBuf)156     void Toggle1BitTransparency(const BitmapBuffer& rBuf)
157     {
158         assert(rBuf.maPalette.GetBestIndex(BitmapColor(COL_BLACK)) == 0);
159         // TODO: make upper layers use standard alpha
160         if (getCairoFormat(rBuf) == CAIRO_FORMAT_A1)
161         {
162             const int nImageSize = rBuf.mnHeight * rBuf.mnScanlineSize;
163             unsigned char* pDst = rBuf.mpBits;
164             for (int i = nImageSize; --i >= 0; ++pDst)
165                 *pDst = ~*pDst;
166         }
167     }
168 
FastConvert24BitRgbTo32BitCairo(const BitmapBuffer * pSrc)169     std::unique_ptr<BitmapBuffer> FastConvert24BitRgbTo32BitCairo(const BitmapBuffer* pSrc)
170     {
171         if (pSrc == nullptr)
172             return nullptr;
173 
174         assert(pSrc->mnFormat == SVP_24BIT_FORMAT);
175         const long nWidth = pSrc->mnWidth;
176         const long nHeight = pSrc->mnHeight;
177         std::unique_ptr<BitmapBuffer> pDst(new BitmapBuffer);
178         pDst->mnFormat = (ScanlineFormat::N32BitTcArgb | ScanlineFormat::TopDown);
179         pDst->mnWidth = nWidth;
180         pDst->mnHeight = nHeight;
181         pDst->mnBitCount = 32;
182         pDst->maColorMask = pSrc->maColorMask;
183         pDst->maPalette = pSrc->maPalette;
184 
185         long nScanlineBase;
186         const bool bFail = o3tl::checked_multiply<long>(pDst->mnBitCount, nWidth, nScanlineBase);
187         if (bFail)
188         {
189             SAL_WARN("vcl.gdi", "checked multiply failed");
190             pDst->mpBits = nullptr;
191             return nullptr;
192         }
193 
194         pDst->mnScanlineSize = AlignedWidth4Bytes(nScanlineBase);
195         if (pDst->mnScanlineSize < nScanlineBase/8)
196         {
197             SAL_WARN("vcl.gdi", "scanline calculation wraparound");
198             pDst->mpBits = nullptr;
199             return nullptr;
200         }
201 
202         try
203         {
204             pDst->mpBits = new sal_uInt8[ pDst->mnScanlineSize * nHeight ];
205         }
206         catch (const std::bad_alloc&)
207         {
208             // memory exception, clean up
209             pDst->mpBits = nullptr;
210             return nullptr;
211         }
212 
213         for (long y = 0; y < nHeight; ++y)
214         {
215             sal_uInt8* pS = pSrc->mpBits + y * pSrc->mnScanlineSize;
216             sal_uInt8* pD = pDst->mpBits + y * pDst->mnScanlineSize;
217             for (long x = 0; x < nWidth; ++x)
218             {
219 #if defined ANDROID
220                 static_assert((SVP_CAIRO_FORMAT & ~ScanlineFormat::TopDown) == ScanlineFormat::N32BitTcRgba, "Expected SVP_CAIRO_FORMAT set to N32BitTcBgra");
221                 static_assert((SVP_24BIT_FORMAT & ~ScanlineFormat::TopDown) == ScanlineFormat::N24BitTcRgb, "Expected SVP_24BIT_FORMAT set to N24BitTcRgb");
222                 pD[0] = pS[0];
223                 pD[1] = pS[1];
224                 pD[2] = pS[2];
225                 pD[3] = 0xff; // Alpha
226 #elif defined OSL_BIGENDIAN
227                 static_assert((SVP_CAIRO_FORMAT & ~ScanlineFormat::TopDown) == ScanlineFormat::N32BitTcArgb, "Expected SVP_CAIRO_FORMAT set to N32BitTcBgra");
228                 static_assert((SVP_24BIT_FORMAT & ~ScanlineFormat::TopDown) == ScanlineFormat::N24BitTcRgb, "Expected SVP_24BIT_FORMAT set to N24BitTcRgb");
229                 pD[0] = 0xff; // Alpha
230                 pD[1] = pS[0];
231                 pD[2] = pS[1];
232                 pD[3] = pS[2];
233 #else
234                 static_assert((SVP_CAIRO_FORMAT & ~ScanlineFormat::TopDown) == ScanlineFormat::N32BitTcBgra, "Expected SVP_CAIRO_FORMAT set to N32BitTcBgra");
235                 static_assert((SVP_24BIT_FORMAT & ~ScanlineFormat::TopDown) == ScanlineFormat::N24BitTcBgr, "Expected SVP_24BIT_FORMAT set to N24BitTcBgr");
236                 pD[0] = pS[0];
237                 pD[1] = pS[1];
238                 pD[2] = pS[2];
239                 pD[3] = 0xff; // Alpha
240 #endif
241 
242                 pS += 3;
243                 pD += 4;
244             }
245         }
246 
247         return pDst;
248     }
249 
250     class SourceHelper
251     {
252     public:
SourceHelper(const SalBitmap & rSourceBitmap,const bool bForceARGB32=false)253         explicit SourceHelper(const SalBitmap& rSourceBitmap, const bool bForceARGB32 = false)
254 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
255             : m_bForceARGB32(bForceARGB32)
256 #endif
257         {
258             const SvpSalBitmap& rSrcBmp = static_cast<const SvpSalBitmap&>(rSourceBitmap);
259 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
260             if ((rSrcBmp.GetBitCount() != 32 && rSrcBmp.GetBitCount() != 24) || bForceARGB32)
261 #else
262             (void)bForceARGB32;
263             if (rSrcBmp.GetBitCount() != 32)
264 #endif
265             {
266                 //big stupid copy here
267                 const BitmapBuffer* pSrc = rSrcBmp.GetBuffer();
268                 const SalTwoRect aTwoRect = { 0, 0, pSrc->mnWidth, pSrc->mnHeight,
269                                               0, 0, pSrc->mnWidth, pSrc->mnHeight };
270                 std::unique_ptr<BitmapBuffer> pTmp = (pSrc->mnFormat == SVP_24BIT_FORMAT
271                                    ? FastConvert24BitRgbTo32BitCairo(pSrc)
272                                    : StretchAndConvert(*pSrc, aTwoRect, SVP_CAIRO_FORMAT));
273                 aTmpBmp.Create(std::move(pTmp));
274 
275                 assert(aTmpBmp.GetBitCount() == 32);
276                 source = SvpSalGraphics::createCairoSurface(aTmpBmp.GetBuffer());
277             }
278             else
279                 source = SvpSalGraphics::createCairoSurface(rSrcBmp.GetBuffer());
280         }
~SourceHelper()281         ~SourceHelper()
282         {
283             cairo_surface_destroy(source);
284         }
getSurface()285         cairo_surface_t* getSurface()
286         {
287             return source;
288         }
mark_dirty()289         void mark_dirty()
290         {
291             cairo_surface_mark_dirty(source);
292         }
getBits(sal_Int32 & rStride)293         unsigned char* getBits(sal_Int32 &rStride)
294         {
295             cairo_surface_flush(source);
296 
297             unsigned char *mask_data = cairo_image_surface_get_data(source);
298 
299             const cairo_format_t nFormat = cairo_image_surface_get_format(source);
300 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
301             if (!m_bForceARGB32)
302                 assert(nFormat == CAIRO_FORMAT_RGB24_888 && "Expected RGB24_888 image");
303             else
304 #endif
305             assert(nFormat == CAIRO_FORMAT_ARGB32 && "need to implement CAIRO_FORMAT_A1 after all here");
306 
307             rStride = cairo_format_stride_for_width(nFormat, cairo_image_surface_get_width(source));
308 
309             return mask_data;
310         }
311     private:
312 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
313         const bool m_bForceARGB32;
314 #endif
315         SvpSalBitmap aTmpBmp;
316         cairo_surface_t* source;
317 
318         SourceHelper(const SourceHelper&) = delete;
319         SourceHelper& operator=(const SourceHelper&) = delete;
320     };
321 
322     class MaskHelper
323     {
324     public:
MaskHelper(const SalBitmap & rAlphaBitmap)325         explicit MaskHelper(const SalBitmap& rAlphaBitmap)
326         {
327             const SvpSalBitmap& rMask = static_cast<const SvpSalBitmap&>(rAlphaBitmap);
328             const BitmapBuffer* pMaskBuf = rMask.GetBuffer();
329 
330             if (rAlphaBitmap.GetBitCount() == 8)
331             {
332                 // the alpha values need to be inverted for Cairo
333                 // so big stupid copy and invert here
334                 const int nImageSize = pMaskBuf->mnHeight * pMaskBuf->mnScanlineSize;
335                 pAlphaBits.reset( new unsigned char[nImageSize] );
336                 memcpy(pAlphaBits.get(), pMaskBuf->mpBits, nImageSize);
337 
338                 // TODO: make upper layers use standard alpha
339                 sal_uInt32* pLDst = reinterpret_cast<sal_uInt32*>(pAlphaBits.get());
340                 for( int i = nImageSize/sizeof(sal_uInt32); --i >= 0; ++pLDst )
341                     *pLDst = ~*pLDst;
342                 assert(reinterpret_cast<unsigned char*>(pLDst) == pAlphaBits.get()+nImageSize);
343 
344                 mask = cairo_image_surface_create_for_data(pAlphaBits.get(),
345                                                 CAIRO_FORMAT_A8,
346                                                 pMaskBuf->mnWidth, pMaskBuf->mnHeight,
347                                                 pMaskBuf->mnScanlineSize);
348             }
349             else
350             {
351                 // the alpha values need to be inverted for Cairo
352                 // so big stupid copy and invert here
353                 const int nImageSize = pMaskBuf->mnHeight * pMaskBuf->mnScanlineSize;
354                 pAlphaBits.reset( new unsigned char[nImageSize] );
355                 memcpy(pAlphaBits.get(), pMaskBuf->mpBits, nImageSize);
356 
357                 const sal_Int32 nBlackIndex = pMaskBuf->maPalette.GetBestIndex(BitmapColor(COL_BLACK));
358                 if (nBlackIndex == 0)
359                 {
360                     // TODO: make upper layers use standard alpha
361                     unsigned char* pDst = pAlphaBits.get();
362                     for (int i = nImageSize; --i >= 0; ++pDst)
363                         *pDst = ~*pDst;
364                 }
365 
366                 mask = cairo_image_surface_create_for_data(pAlphaBits.get(),
367                                                 CAIRO_FORMAT_A1,
368                                                 pMaskBuf->mnWidth, pMaskBuf->mnHeight,
369                                                 pMaskBuf->mnScanlineSize);
370             }
371         }
~MaskHelper()372         ~MaskHelper()
373         {
374             cairo_surface_destroy(mask);
375         }
getMask()376         cairo_surface_t* getMask()
377         {
378             return mask;
379         }
380     private:
381         cairo_surface_t *mask;
382         std::unique_ptr<unsigned char[]> pAlphaBits;
383 
384         MaskHelper(const MaskHelper&) = delete;
385         MaskHelper& operator=(const MaskHelper&) = delete;
386     };
387 }
388 
drawAlphaBitmap(const SalTwoRect & rTR,const SalBitmap & rSourceBitmap,const SalBitmap & rAlphaBitmap)389 bool SvpSalGraphics::drawAlphaBitmap( const SalTwoRect& rTR, const SalBitmap& rSourceBitmap, const SalBitmap& rAlphaBitmap )
390 {
391     if (rAlphaBitmap.GetBitCount() != 8 && rAlphaBitmap.GetBitCount() != 1)
392     {
393         SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawAlphaBitmap alpha depth case: " << rAlphaBitmap.GetBitCount());
394         return false;
395     }
396 
397     SourceHelper aSurface(rSourceBitmap);
398     cairo_surface_t* source = aSurface.getSurface();
399     if (!source)
400     {
401         SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawAlphaBitmap case");
402         return false;
403     }
404 
405     MaskHelper aMask(rAlphaBitmap);
406     cairo_surface_t *mask = aMask.getMask();
407     if (!mask)
408     {
409         SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawAlphaBitmap case");
410         return false;
411     }
412 
413     cairo_t* cr = getCairoContext(false);
414     clipRegion(cr);
415 
416     cairo_rectangle(cr, rTR.mnDestX, rTR.mnDestY, rTR.mnDestWidth, rTR.mnDestHeight);
417 
418     basegfx::B2DRange extents = getClippedFillDamage(cr);
419 
420     cairo_clip(cr);
421 
422     cairo_pattern_t* maskpattern = cairo_pattern_create_for_surface(mask);
423     cairo_translate(cr, rTR.mnDestX, rTR.mnDestY);
424     double fXScale = static_cast<double>(rTR.mnDestWidth)/rTR.mnSrcWidth;
425     double fYScale = static_cast<double>(rTR.mnDestHeight)/rTR.mnSrcHeight;
426     cairo_scale(cr, fXScale, fYScale);
427     cairo_set_source_surface(cr, source, -rTR.mnSrcX, -rTR.mnSrcY);
428 
429     //tdf#114117 when stretching a single pixel width/height source to fit an area
430     //set extend and filter to stretch it with simplest expected interpolation
431     if ((fXScale != 1.0 && rTR.mnSrcWidth == 1) || (fYScale != 1.0 && rTR.mnSrcHeight == 1))
432     {
433         cairo_pattern_t* sourcepattern = cairo_get_source(cr);
434         cairo_pattern_set_extend(sourcepattern, CAIRO_EXTEND_REPEAT);
435         cairo_pattern_set_filter(sourcepattern, CAIRO_FILTER_NEAREST);
436         cairo_pattern_set_extend(maskpattern, CAIRO_EXTEND_REPEAT);
437         cairo_pattern_set_filter(maskpattern, CAIRO_FILTER_NEAREST);
438     }
439 
440     //this block is just "cairo_mask_surface", but we have to make it explicit
441     //because of the cairo_pattern_set_filter etc we may want applied
442     cairo_matrix_t matrix;
443     cairo_matrix_init_translate(&matrix, rTR.mnSrcX, rTR.mnSrcY);
444     cairo_pattern_set_matrix(maskpattern, &matrix);
445     cairo_mask(cr, maskpattern);
446 
447     cairo_pattern_destroy(maskpattern);
448 
449     releaseCairoContext(cr, false, extents);
450 
451     return true;
452 }
453 
drawTransformedBitmap(const basegfx::B2DPoint & rNull,const basegfx::B2DPoint & rX,const basegfx::B2DPoint & rY,const SalBitmap & rSourceBitmap,const SalBitmap * pAlphaBitmap)454 bool SvpSalGraphics::drawTransformedBitmap(
455     const basegfx::B2DPoint& rNull,
456     const basegfx::B2DPoint& rX,
457     const basegfx::B2DPoint& rY,
458     const SalBitmap& rSourceBitmap,
459     const SalBitmap* pAlphaBitmap)
460 {
461     if (pAlphaBitmap && pAlphaBitmap->GetBitCount() != 8 && pAlphaBitmap->GetBitCount() != 1)
462     {
463         SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawTransformedBitmap alpha depth case: " << pAlphaBitmap->GetBitCount());
464         return false;
465     }
466 
467     SourceHelper aSurface(rSourceBitmap);
468     cairo_surface_t* source = aSurface.getSurface();
469     if (!source)
470     {
471         SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawTransformedBitmap case");
472         return false;
473     }
474 
475     std::unique_ptr<MaskHelper> xMask;
476     cairo_surface_t *mask = nullptr;
477     if (pAlphaBitmap)
478     {
479         xMask.reset(new MaskHelper(*pAlphaBitmap));
480         mask = xMask->getMask();
481         if (!mask)
482         {
483             SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawTransformedBitmap case");
484             return false;
485         }
486     }
487 
488     const Size aSize = rSourceBitmap.GetSize();
489 
490     cairo_t* cr = getCairoContext(false);
491     clipRegion(cr);
492 
493     // setup the image transformation
494     // using the rNull,rX,rY points as destinations for the (0,0),(0,Width),(Height,0) source points
495     const basegfx::B2DVector aXRel = rX - rNull;
496     const basegfx::B2DVector aYRel = rY - rNull;
497     cairo_matrix_t matrix;
498     cairo_matrix_init(&matrix,
499                       aXRel.getX()/aSize.Width(), aXRel.getY()/aSize.Width(),
500                       aYRel.getX()/aSize.Height(), aYRel.getY()/aSize.Height(),
501                       rNull.getX(), rNull.getY());
502 
503     cairo_transform(cr, &matrix);
504 
505     cairo_rectangle(cr, 0, 0, aSize.Width(), aSize.Height());
506     basegfx::B2DRange extents = getClippedFillDamage(cr);
507     cairo_clip(cr);
508 
509     cairo_set_source_surface(cr, source, 0, 0);
510     if (mask)
511         cairo_mask_surface(cr, mask, 0, 0);
512     else
513         cairo_paint(cr);
514 
515     releaseCairoContext(cr, false, extents);
516 
517     return true;
518 }
519 
clipRegion(cairo_t * cr,const vcl::Region & rClipRegion)520 void SvpSalGraphics::clipRegion(cairo_t* cr, const vcl::Region& rClipRegion)
521 {
522     RectangleVector aRectangles;
523     if (!rClipRegion.IsEmpty())
524     {
525         rClipRegion.GetRegionRectangles(aRectangles);
526     }
527     if (!aRectangles.empty())
528     {
529         for (auto const& rectangle : aRectangles)
530         {
531             cairo_rectangle(cr, rectangle.Left(), rectangle.Top(), rectangle.GetWidth(), rectangle.GetHeight());
532         }
533         cairo_clip(cr);
534     }
535 }
536 
clipRegion(cairo_t * cr)537 void SvpSalGraphics::clipRegion(cairo_t* cr)
538 {
539     SvpSalGraphics::clipRegion(cr, m_aClipRegion);
540 }
541 
drawAlphaRect(long nX,long nY,long nWidth,long nHeight,sal_uInt8 nTransparency)542 bool SvpSalGraphics::drawAlphaRect(long nX, long nY, long nWidth, long nHeight, sal_uInt8 nTransparency)
543 {
544     const bool bHasFill(m_aFillColor != SALCOLOR_NONE);
545     const bool bHasLine(m_aLineColor != SALCOLOR_NONE);
546 
547     if(!(bHasFill || bHasLine))
548     {
549         return true;
550     }
551 
552     cairo_t* cr = getCairoContext(false);
553     clipRegion(cr);
554 
555     const double fTransparency = nTransparency * (1.0/100);
556 
557     // To make releaseCairoContext work, use empty extents
558     basegfx::B2DRange extents;
559 
560     if (bHasFill)
561     {
562         cairo_rectangle(cr, nX, nY, nWidth, nHeight);
563 
564         applyColor(cr, m_aFillColor, fTransparency);
565 
566         // set FillDamage
567         extents = getClippedFillDamage(cr);
568 
569         cairo_fill(cr);
570     }
571 
572     if (bHasLine)
573     {
574         // PixelOffset used: Set PixelOffset as linear transformation
575         // Note: Was missing here - probably not by purpose (?)
576         cairo_matrix_t aMatrix;
577         cairo_matrix_init_translate(&aMatrix, 0.5, 0.5);
578         cairo_set_matrix(cr, &aMatrix);
579 
580         cairo_rectangle(cr, nX, nY, nWidth, nHeight);
581 
582         applyColor(cr, m_aLineColor, fTransparency);
583 
584         // expand with possible StrokeDamage
585         basegfx::B2DRange stroke_extents = getClippedStrokeDamage(cr);
586         stroke_extents.transform(basegfx::utils::createTranslateB2DHomMatrix(0.5, 0.5));
587         extents.expand(stroke_extents);
588 
589         cairo_stroke(cr);
590     }
591 
592     releaseCairoContext(cr, false, extents);
593 
594     return true;
595 }
596 
SvpSalGraphics()597 SvpSalGraphics::SvpSalGraphics()
598     : m_pSurface(nullptr)
599     , m_fScale(1.0)
600     , m_aLineColor(Color(0x00, 0x00, 0x00))
601     , m_aFillColor(Color(0xFF, 0xFF, 0XFF))
602     , m_ePaintMode(PaintMode::Over)
603     , m_aTextRenderImpl(*this)
604 {
605     bool bLOKActive = comphelper::LibreOfficeKit::isActive();
606     if (!initWidgetDrawBackends(bLOKActive))
607     {
608         if (bLOKActive)
609             m_pWidgetDraw.reset(new vcl::CustomWidgetDraw(*this));
610     }
611 }
612 
~SvpSalGraphics()613 SvpSalGraphics::~SvpSalGraphics()
614 {
615     ReleaseFonts();
616 }
617 
setSurface(cairo_surface_t * pSurface,const basegfx::B2IVector & rSize)618 void SvpSalGraphics::setSurface(cairo_surface_t* pSurface, const basegfx::B2IVector& rSize)
619 {
620     m_pSurface = pSurface;
621     m_aFrameSize = rSize;
622     dl_cairo_surface_get_device_scale(pSurface, &m_fScale, nullptr);
623     ResetClipRegion();
624 }
625 
GetResolution(sal_Int32 & rDPIX,sal_Int32 & rDPIY)626 void SvpSalGraphics::GetResolution( sal_Int32& rDPIX, sal_Int32& rDPIY )
627 {
628     rDPIX = rDPIY = 96;
629 }
630 
GetBitCount() const631 sal_uInt16 SvpSalGraphics::GetBitCount() const
632 {
633     if (cairo_surface_get_content(m_pSurface) != CAIRO_CONTENT_COLOR_ALPHA)
634         return 1;
635     return 32;
636 }
637 
GetGraphicsWidth() const638 long SvpSalGraphics::GetGraphicsWidth() const
639 {
640     return m_pSurface ? m_aFrameSize.getX() : 0;
641 }
642 
ResetClipRegion()643 void SvpSalGraphics::ResetClipRegion()
644 {
645     m_aClipRegion.SetNull();
646 }
647 
setClipRegion(const vcl::Region & i_rClip)648 bool SvpSalGraphics::setClipRegion( const vcl::Region& i_rClip )
649 {
650     m_aClipRegion = i_rClip;
651     return true;
652 }
653 
SetLineColor()654 void SvpSalGraphics::SetLineColor()
655 {
656     m_aLineColor = SALCOLOR_NONE;
657 }
658 
SetLineColor(Color nColor)659 void SvpSalGraphics::SetLineColor( Color nColor )
660 {
661     m_aLineColor = nColor;
662 }
663 
SetFillColor()664 void SvpSalGraphics::SetFillColor()
665 {
666     m_aFillColor = SALCOLOR_NONE;
667 }
668 
SetFillColor(Color nColor)669 void SvpSalGraphics::SetFillColor( Color nColor )
670 {
671     m_aFillColor = nColor;
672 }
673 
SetXORMode(bool bSet,bool)674 void SvpSalGraphics::SetXORMode(bool bSet, bool )
675 {
676     m_ePaintMode = bSet ? PaintMode::Xor : PaintMode::Over;
677 }
678 
SetROPLineColor(SalROPColor nROPColor)679 void SvpSalGraphics::SetROPLineColor( SalROPColor nROPColor )
680 {
681     switch( nROPColor )
682     {
683         case SalROPColor::N0:
684             m_aLineColor = Color(0, 0, 0);
685             break;
686         case SalROPColor::N1:
687             m_aLineColor = Color(0xff, 0xff, 0xff);
688             break;
689         case SalROPColor::Invert:
690             m_aLineColor = Color(0xff, 0xff, 0xff);
691             break;
692     }
693 }
694 
SetROPFillColor(SalROPColor nROPColor)695 void SvpSalGraphics::SetROPFillColor( SalROPColor nROPColor )
696 {
697     switch( nROPColor )
698     {
699         case SalROPColor::N0:
700             m_aFillColor = Color(0, 0, 0);
701             break;
702         case SalROPColor::N1:
703             m_aFillColor = Color(0xff, 0xff, 0xff);
704             break;
705         case SalROPColor::Invert:
706             m_aFillColor = Color(0xff, 0xff, 0xff);
707             break;
708     }
709 }
710 
drawPixel(long nX,long nY)711 void SvpSalGraphics::drawPixel( long nX, long nY )
712 {
713     if (m_aLineColor != SALCOLOR_NONE)
714     {
715         drawPixel(nX, nY, m_aLineColor);
716     }
717 }
718 
drawPixel(long nX,long nY,Color aColor)719 void SvpSalGraphics::drawPixel( long nX, long nY, Color aColor )
720 {
721     cairo_t* cr = getCairoContext(true);
722     clipRegion(cr);
723 
724     cairo_rectangle(cr, nX, nY, 1, 1);
725     applyColor(cr, aColor, 0.0);
726     cairo_fill(cr);
727 
728     basegfx::B2DRange extents = getClippedFillDamage(cr);
729     releaseCairoContext(cr, true, extents);
730 }
731 
drawRect(long nX,long nY,long nWidth,long nHeight)732 void SvpSalGraphics::drawRect( long nX, long nY, long nWidth, long nHeight )
733 {
734     // because of the -1 hack we have to do fill and draw separately
735     Color aOrigFillColor = m_aFillColor;
736     Color aOrigLineColor = m_aLineColor;
737     m_aFillColor = SALCOLOR_NONE;
738     m_aLineColor = SALCOLOR_NONE;
739 
740     if (aOrigFillColor != SALCOLOR_NONE)
741     {
742         basegfx::B2DPolygon aRect = basegfx::utils::createPolygonFromRect(basegfx::B2DRectangle(nX, nY, nX+nWidth, nY+nHeight));
743         m_aFillColor = aOrigFillColor;
744 
745         drawPolyPolygon(
746             basegfx::B2DHomMatrix(),
747             basegfx::B2DPolyPolygon(aRect),
748             0.0);
749 
750         m_aFillColor = SALCOLOR_NONE;
751     }
752 
753     if (aOrigLineColor != SALCOLOR_NONE)
754     {
755         // need same -1 hack as X11SalGraphicsImpl::drawRect
756         basegfx::B2DPolygon aRect = basegfx::utils::createPolygonFromRect(basegfx::B2DRectangle( nX, nY, nX+nWidth-1, nY+nHeight-1));
757         m_aLineColor = aOrigLineColor;
758 
759         drawPolyPolygon(
760             basegfx::B2DHomMatrix(),
761             basegfx::B2DPolyPolygon(aRect),
762             0.0);
763 
764         m_aLineColor = SALCOLOR_NONE;
765     }
766 
767     m_aFillColor = aOrigFillColor;
768     m_aLineColor = aOrigLineColor;
769 }
770 
drawPolyLine(sal_uInt32 nPoints,const SalPoint * pPtAry)771 void SvpSalGraphics::drawPolyLine(sal_uInt32 nPoints, const SalPoint* pPtAry)
772 {
773     basegfx::B2DPolygon aPoly;
774     aPoly.append(basegfx::B2DPoint(pPtAry->mnX, pPtAry->mnY), nPoints);
775     for (sal_uInt32 i = 1; i < nPoints; ++i)
776         aPoly.setB2DPoint(i, basegfx::B2DPoint(pPtAry[i].mnX, pPtAry[i].mnY));
777     aPoly.setClosed(false);
778 
779     drawPolyLine(
780         basegfx::B2DHomMatrix(),
781         aPoly,
782         0.0,
783         basegfx::B2DVector(1.0, 1.0),
784         basegfx::B2DLineJoin::Miter,
785         css::drawing::LineCap_BUTT,
786         basegfx::deg2rad(15.0) /*default*/,
787         false);
788 }
789 
drawPolygon(sal_uInt32 nPoints,const SalPoint * pPtAry)790 void SvpSalGraphics::drawPolygon(sal_uInt32 nPoints, const SalPoint* pPtAry)
791 {
792     basegfx::B2DPolygon aPoly;
793     aPoly.append(basegfx::B2DPoint(pPtAry->mnX, pPtAry->mnY), nPoints);
794     for (sal_uInt32 i = 1; i < nPoints; ++i)
795         aPoly.setB2DPoint(i, basegfx::B2DPoint(pPtAry[i].mnX, pPtAry[i].mnY));
796 
797     drawPolyPolygon(
798         basegfx::B2DHomMatrix(),
799         basegfx::B2DPolyPolygon(aPoly),
800         0.0);
801 }
802 
drawPolyPolygon(sal_uInt32 nPoly,const sal_uInt32 * pPointCounts,PCONSTSALPOINT * pPtAry)803 void SvpSalGraphics::drawPolyPolygon(sal_uInt32 nPoly,
804                                      const sal_uInt32* pPointCounts,
805                                      PCONSTSALPOINT*   pPtAry)
806 {
807     basegfx::B2DPolyPolygon aPolyPoly;
808     for(sal_uInt32 nPolygon = 0; nPolygon < nPoly; ++nPolygon)
809     {
810         sal_uInt32 nPoints = pPointCounts[nPolygon];
811         if (nPoints)
812         {
813             PCONSTSALPOINT pPoints = pPtAry[nPolygon];
814             basegfx::B2DPolygon aPoly;
815             aPoly.append( basegfx::B2DPoint(pPoints->mnX, pPoints->mnY), nPoints);
816             for (sal_uInt32 i = 1; i < nPoints; ++i)
817                 aPoly.setB2DPoint(i, basegfx::B2DPoint( pPoints[i].mnX, pPoints[i].mnY));
818 
819             aPolyPoly.append(aPoly);
820         }
821     }
822 
823     drawPolyPolygon(
824         basegfx::B2DHomMatrix(),
825         aPolyPoly,
826         0.0);
827 }
828 
impPixelSnap(const basegfx::B2DPolygon & rPolygon,const basegfx::B2DHomMatrix & rObjectToDevice,basegfx::B2DHomMatrix & rObjectToDeviceInv,sal_uInt32 nIndex)829 static basegfx::B2DPoint impPixelSnap(
830     const basegfx::B2DPolygon& rPolygon,
831     const basegfx::B2DHomMatrix& rObjectToDevice,
832     basegfx::B2DHomMatrix& rObjectToDeviceInv,
833     sal_uInt32 nIndex)
834 {
835     const sal_uInt32 nCount(rPolygon.count());
836 
837     // get the data
838     const basegfx::B2ITuple aPrevTuple(basegfx::fround(rObjectToDevice * rPolygon.getB2DPoint((nIndex + nCount - 1) % nCount)));
839     const basegfx::B2DPoint aCurrPoint(rObjectToDevice * rPolygon.getB2DPoint(nIndex));
840     const basegfx::B2ITuple aCurrTuple(basegfx::fround(aCurrPoint));
841     const basegfx::B2ITuple aNextTuple(basegfx::fround(rObjectToDevice * rPolygon.getB2DPoint((nIndex + 1) % nCount)));
842 
843     // get the states
844     const bool bPrevVertical(aPrevTuple.getX() == aCurrTuple.getX());
845     const bool bNextVertical(aNextTuple.getX() == aCurrTuple.getX());
846     const bool bPrevHorizontal(aPrevTuple.getY() == aCurrTuple.getY());
847     const bool bNextHorizontal(aNextTuple.getY() == aCurrTuple.getY());
848     const bool bSnapX(bPrevVertical || bNextVertical);
849     const bool bSnapY(bPrevHorizontal || bNextHorizontal);
850 
851     if(bSnapX || bSnapY)
852     {
853         basegfx::B2DPoint aSnappedPoint(
854             bSnapX ? aCurrTuple.getX() : aCurrPoint.getX(),
855             bSnapY ? aCurrTuple.getY() : aCurrPoint.getY());
856 
857         if(rObjectToDeviceInv.isIdentity())
858         {
859             rObjectToDeviceInv = rObjectToDevice;
860             rObjectToDeviceInv.invert();
861         }
862 
863         aSnappedPoint *= rObjectToDeviceInv;
864 
865         return aSnappedPoint;
866     }
867 
868     return rPolygon.getB2DPoint(nIndex);
869 }
870 
871 // Remove bClosePath: Checked that the already used mechanism for Win using
872 // Gdiplus already relies on rPolygon.isClosed(), so should be safe to replace
873 // this.
874 // For PixelSnap we need the ObjectToDevice transformation here now. Tis is a
875 // special case relative to the also executed LineDraw-Offset of (0.5, 0.5) in
876 // DeviceCoordinates: The LineDraw-Offset is applied *after* the snap, so we
877 // need the ObjectToDevice transformation *without* that offset here to do the
878 // same. The LineDraw-Offset will be applied by the callers using a linear
879 // transformation for Cairo now
880 // For support of PixelSnapHairline we also need the ObjectToDevice transformation
881 // and a method (same as in gdiimpl.cxx for Win and Gdiplus). This is needed e.g.
882 // for Chart-content visualization. CAUTION: It's not the same as PixelSnap (!)
AddPolygonToPath(cairo_t * cr,const basegfx::B2DPolygon & rPolygon,const basegfx::B2DHomMatrix & rObjectToDevice,bool bPixelSnap,bool bPixelSnapHairline)883 static void AddPolygonToPath(
884     cairo_t* cr,
885     const basegfx::B2DPolygon& rPolygon,
886     const basegfx::B2DHomMatrix& rObjectToDevice,
887     bool bPixelSnap,
888     bool bPixelSnapHairline)
889 {
890     // short circuit if there is nothing to do
891     const sal_uInt32 nPointCount(rPolygon.count());
892 
893     if(0 == nPointCount)
894     {
895         return;
896     }
897 
898     const bool bHasCurves(rPolygon.areControlPointsUsed());
899     const bool bClosePath(rPolygon.isClosed());
900     const bool bObjectToDeviceUsed(!rObjectToDevice.isIdentity());
901     basegfx::B2DHomMatrix aObjectToDeviceInv;
902     basegfx::B2DPoint aLast;
903 
904     for( sal_uInt32 nPointIdx = 0, nPrevIdx = 0;; nPrevIdx = nPointIdx++ )
905     {
906         int nClosedIdx = nPointIdx;
907         if( nPointIdx >= nPointCount )
908         {
909             // prepare to close last curve segment if needed
910             if( bClosePath && (nPointIdx == nPointCount) )
911             {
912                 nClosedIdx = 0;
913             }
914             else
915             {
916                 break;
917             }
918         }
919 
920         basegfx::B2DPoint aPoint(rPolygon.getB2DPoint(nClosedIdx));
921 
922         if(bPixelSnap)
923         {
924             // snap device coordinates to full pixels
925             if(bObjectToDeviceUsed)
926             {
927                 // go to DeviceCoordinates
928                 aPoint *= rObjectToDevice;
929             }
930 
931             // snap by rounding
932             aPoint.setX( basegfx::fround( aPoint.getX() ) );
933             aPoint.setY( basegfx::fround( aPoint.getY() ) );
934 
935             if(bObjectToDeviceUsed)
936             {
937                 if(aObjectToDeviceInv.isIdentity())
938                 {
939                     aObjectToDeviceInv = rObjectToDevice;
940                     aObjectToDeviceInv.invert();
941                 }
942 
943                 // go back to ObjectCoordinates
944                 aPoint *= aObjectToDeviceInv;
945             }
946         }
947 
948         if(bPixelSnapHairline)
949         {
950             // snap horizontal and vertical lines (mainly used in Chart for
951             // 'nicer' AAing)
952             aPoint = impPixelSnap(rPolygon, rObjectToDevice, aObjectToDeviceInv, nClosedIdx);
953         }
954 
955         if( !nPointIdx )
956         {
957             // first point => just move there
958             cairo_move_to(cr, aPoint.getX(), aPoint.getY());
959             aLast = aPoint;
960             continue;
961         }
962 
963         bool bPendingCurve(false);
964 
965         if( bHasCurves )
966         {
967             bPendingCurve = rPolygon.isNextControlPointUsed( nPrevIdx );
968             bPendingCurve |= rPolygon.isPrevControlPointUsed( nClosedIdx );
969         }
970 
971         if( !bPendingCurve )    // line segment
972         {
973             cairo_line_to(cr, aPoint.getX(), aPoint.getY());
974         }
975         else                        // cubic bezier segment
976         {
977             basegfx::B2DPoint aCP1 = rPolygon.getNextControlPoint( nPrevIdx );
978             basegfx::B2DPoint aCP2 = rPolygon.getPrevControlPoint( nClosedIdx );
979 
980             // tdf#99165 if the control points are 'empty', create the mathematical
981             // correct replacement ones to avoid problems with the graphical sub-system
982             // tdf#101026 The 1st attempt to create a mathematically correct replacement control
983             // vector was wrong. Best alternative is one as close as possible which means short.
984             if (aCP1.equal(aLast))
985             {
986                 aCP1 = aLast + ((aCP2 - aLast) * 0.0005);
987             }
988 
989             if(aCP2.equal(aPoint))
990             {
991                 aCP2 = aPoint + ((aCP1 - aPoint) * 0.0005);
992             }
993 
994             cairo_curve_to(cr, aCP1.getX(), aCP1.getY(), aCP2.getX(), aCP2.getY(),
995                                aPoint.getX(), aPoint.getY());
996         }
997 
998         aLast = aPoint;
999     }
1000 
1001     if( bClosePath )
1002     {
1003         cairo_close_path(cr);
1004     }
1005 }
1006 
drawLine(long nX1,long nY1,long nX2,long nY2)1007 void SvpSalGraphics::drawLine( long nX1, long nY1, long nX2, long nY2 )
1008 {
1009     basegfx::B2DPolygon aPoly;
1010 
1011     // PixelOffset used: To not mix with possible PixelSnap, cannot do
1012     // directly on coordinates as tried before - despite being already 'snapped'
1013     // due to being integer. If it would be directly added here, it would be
1014     // 'snapped' again when !getAntiAliasB2DDraw(), losing the (0.5, 0.5) offset
1015     aPoly.append(basegfx::B2DPoint(nX1, nY1));
1016     aPoly.append(basegfx::B2DPoint(nX2, nY2));
1017 
1018     cairo_t* cr = getCairoContext(false);
1019     clipRegion(cr);
1020 
1021     // PixelOffset used: Set PixelOffset as linear transformation
1022     cairo_matrix_t aMatrix;
1023     cairo_matrix_init_translate(&aMatrix, 0.5, 0.5);
1024     cairo_set_matrix(cr, &aMatrix);
1025 
1026     AddPolygonToPath(
1027         cr,
1028         aPoly,
1029         basegfx::B2DHomMatrix(),
1030         !getAntiAliasB2DDraw(),
1031         false);
1032 
1033     applyColor(cr, m_aLineColor);
1034 
1035     basegfx::B2DRange extents = getClippedStrokeDamage(cr);
1036     extents.transform(basegfx::utils::createTranslateB2DHomMatrix(0.5, 0.5));
1037 
1038     cairo_stroke(cr);
1039 
1040     releaseCairoContext(cr, false, extents);
1041 }
1042 
1043 class SystemDependentData_CairoPath : public basegfx::SystemDependentData
1044 {
1045 private:
1046     // the path data itself
1047     cairo_path_t*       mpCairoPath;
1048 
1049     // all other values the path data  is based on and
1050     // need to be compared with to check for data validity
1051     bool                mbNoJoin;
1052     bool                mbAntiAliasB2DDraw;
1053 
1054 public:
1055     SystemDependentData_CairoPath(
1056         basegfx::SystemDependentDataManager& rSystemDependentDataManager,
1057         cairo_path_t* pCairoPath,
1058         bool bNoJoin,
1059         bool bAntiAliasB2DDraw);
1060     virtual ~SystemDependentData_CairoPath() override;
1061 
getCairoPath()1062     cairo_path_t* getCairoPath() { return mpCairoPath; }
getNoJoin() const1063     bool getNoJoin() const { return mbNoJoin; }
getAntiAliasB2DDraw() const1064     bool getAntiAliasB2DDraw() const { return mbAntiAliasB2DDraw; }
1065 
1066     virtual sal_Int64 estimateUsageInBytes() const override;
1067 };
1068 
SystemDependentData_CairoPath(basegfx::SystemDependentDataManager & rSystemDependentDataManager,cairo_path_t * pCairoPath,bool bNoJoin,bool bAntiAliasB2DDraw)1069 SystemDependentData_CairoPath::SystemDependentData_CairoPath(
1070     basegfx::SystemDependentDataManager& rSystemDependentDataManager,
1071     cairo_path_t* pCairoPath,
1072     bool bNoJoin,
1073     bool bAntiAliasB2DDraw)
1074 :   basegfx::SystemDependentData(rSystemDependentDataManager),
1075     mpCairoPath(pCairoPath),
1076     mbNoJoin(bNoJoin),
1077     mbAntiAliasB2DDraw(bAntiAliasB2DDraw)
1078 {
1079 }
1080 
~SystemDependentData_CairoPath()1081 SystemDependentData_CairoPath::~SystemDependentData_CairoPath()
1082 {
1083     if(nullptr != mpCairoPath)
1084     {
1085         cairo_path_destroy(mpCairoPath);
1086         mpCairoPath = nullptr;
1087     }
1088 }
1089 
estimateUsageInBytes() const1090 sal_Int64 SystemDependentData_CairoPath::estimateUsageInBytes() const
1091 {
1092     sal_Int64 nRetval(0);
1093 
1094     if(nullptr != mpCairoPath)
1095     {
1096         // per node
1097         // - num_data incarnations of
1098         // - sizeof(cairo_path_data_t) which is a union of defines and point data
1099         //   thus may 2 x sizeof(double)
1100         nRetval = mpCairoPath->num_data * sizeof(cairo_path_data_t);
1101     }
1102 
1103     return nRetval;
1104 }
1105 
drawPolyLine(const basegfx::B2DHomMatrix & rObjectToDevice,const basegfx::B2DPolygon & rPolyLine,double fTransparency,const basegfx::B2DVector & rLineWidths,basegfx::B2DLineJoin eLineJoin,css::drawing::LineCap eLineCap,double fMiterMinimumAngle,bool bPixelSnapHairline)1106 bool SvpSalGraphics::drawPolyLine(
1107     const basegfx::B2DHomMatrix& rObjectToDevice,
1108     const basegfx::B2DPolygon& rPolyLine,
1109     double fTransparency,
1110     const basegfx::B2DVector& rLineWidths,
1111     basegfx::B2DLineJoin eLineJoin,
1112     css::drawing::LineCap eLineCap,
1113     double fMiterMinimumAngle,
1114     bool bPixelSnapHairline)
1115 {
1116     // short circuit if there is nothing to do
1117     if(0 == rPolyLine.count() || fTransparency < 0.0 || fTransparency >= 1.0)
1118     {
1119         return true;
1120     }
1121 
1122     // Wrap call to static version of ::drawPolyLine by
1123     // preparing/getting some local data and parameters
1124     // due to usage in vcl/unx/generic/gdi/salgdi.cxx.
1125     // This is mainly about extended handling of extents
1126     // and the way destruction of CairoContext is handled
1127     // due to current XOR stuff
1128     cairo_t* cr = getCairoContext(false);
1129     basegfx::B2DRange aExtents;
1130     clipRegion(cr);
1131 
1132     bool bRetval(
1133         drawPolyLine(
1134             cr,
1135             &aExtents,
1136             m_aLineColor,
1137             getAntiAliasB2DDraw(),
1138             rObjectToDevice,
1139             rPolyLine,
1140             fTransparency,
1141             rLineWidths,
1142             eLineJoin,
1143             eLineCap,
1144             fMiterMinimumAngle,
1145             bPixelSnapHairline));
1146 
1147     releaseCairoContext(cr, false, aExtents);
1148 
1149     return bRetval;
1150 }
1151 
drawPolyLine(cairo_t * cr,basegfx::B2DRange * pExtents,const Color & rLineColor,bool bAntiAliasB2DDraw,const basegfx::B2DHomMatrix & rObjectToDevice,const basegfx::B2DPolygon & rPolyLine,double fTransparency,const basegfx::B2DVector & rLineWidths,basegfx::B2DLineJoin eLineJoin,css::drawing::LineCap eLineCap,double fMiterMinimumAngle,bool bPixelSnapHairline)1152 bool SvpSalGraphics::drawPolyLine(
1153     cairo_t* cr,
1154     basegfx::B2DRange* pExtents,
1155     const Color& rLineColor,
1156     bool bAntiAliasB2DDraw,
1157     const basegfx::B2DHomMatrix& rObjectToDevice,
1158     const basegfx::B2DPolygon& rPolyLine,
1159     double fTransparency,
1160     const basegfx::B2DVector& rLineWidths,
1161     basegfx::B2DLineJoin eLineJoin,
1162     css::drawing::LineCap eLineCap,
1163     double fMiterMinimumAngle,
1164     bool bPixelSnapHairline)
1165 {
1166     // short circuit if there is nothing to do
1167     if(0 == rPolyLine.count() || fTransparency < 0.0 || fTransparency >= 1.0)
1168     {
1169         return true;
1170     }
1171 
1172     // need to check/handle LineWidth when ObjectToDevice transformation is used
1173     basegfx::B2DVector aLineWidths(rLineWidths);
1174     const bool bObjectToDeviceIsIdentity(rObjectToDevice.isIdentity());
1175     const basegfx::B2DVector aDeviceLineWidths(bObjectToDeviceIsIdentity ? rLineWidths : rObjectToDevice * rLineWidths);
1176     const bool bCorrectLineWidth(!bObjectToDeviceIsIdentity && aDeviceLineWidths.getX() < 1.0 && aLineWidths.getX() >= 1.0);
1177 
1178     // on-demand inverse of ObjectToDevice transformation
1179     basegfx::B2DHomMatrix aObjectToDeviceInv;
1180 
1181     if(bCorrectLineWidth)
1182     {
1183         if(aObjectToDeviceInv.isIdentity())
1184         {
1185             aObjectToDeviceInv = rObjectToDevice;
1186             aObjectToDeviceInv.invert();
1187         }
1188 
1189         // calculate-back logical LineWidth for a hairline
1190         aLineWidths = aObjectToDeviceInv * basegfx::B2DVector(1.0, 1.0);
1191     }
1192 
1193     // PixelOffset used: Need to reflect in linear transformation
1194     cairo_matrix_t aMatrix;
1195 
1196     basegfx::B2DHomMatrix aDamageMatrix(basegfx::utils::createTranslateB2DHomMatrix(0.5, 0.5));
1197 
1198     if (bObjectToDeviceIsIdentity)
1199     {
1200         // Set PixelOffset as requested
1201         cairo_matrix_init_translate(&aMatrix, 0.5, 0.5);
1202     }
1203     else
1204     {
1205         // Prepare ObjectToDevice transformation. Take PixelOffset for Lines into
1206         // account: Multiply from left to act in DeviceCoordinates
1207         aDamageMatrix = aDamageMatrix * rObjectToDevice;
1208         cairo_matrix_init(
1209             &aMatrix,
1210             aDamageMatrix.get( 0, 0 ),
1211             aDamageMatrix.get( 1, 0 ),
1212             aDamageMatrix.get( 0, 1 ),
1213             aDamageMatrix.get( 1, 1 ),
1214             aDamageMatrix.get( 0, 2 ),
1215             aDamageMatrix.get( 1, 2 ));
1216     }
1217 
1218     // set linear transformation
1219     cairo_set_matrix(cr, &aMatrix);
1220 
1221     const bool bNoJoin((basegfx::B2DLineJoin::NONE == eLineJoin && basegfx::fTools::more(aLineWidths.getX(), 0.0)));
1222 
1223     // setup line attributes
1224     cairo_line_join_t eCairoLineJoin = CAIRO_LINE_JOIN_MITER;
1225     switch (eLineJoin)
1226     {
1227         case basegfx::B2DLineJoin::Bevel:
1228             eCairoLineJoin = CAIRO_LINE_JOIN_BEVEL;
1229             break;
1230         case basegfx::B2DLineJoin::Round:
1231             eCairoLineJoin = CAIRO_LINE_JOIN_ROUND;
1232             break;
1233         case basegfx::B2DLineJoin::NONE:
1234         case basegfx::B2DLineJoin::Miter:
1235             eCairoLineJoin = CAIRO_LINE_JOIN_MITER;
1236             break;
1237     }
1238 
1239     // convert miter minimum angle to miter limit
1240     double fMiterLimit = 1.0 / sin( fMiterMinimumAngle / 2.0);
1241 
1242     // setup cap attribute
1243     cairo_line_cap_t eCairoLineCap(CAIRO_LINE_CAP_BUTT);
1244 
1245     switch (eLineCap)
1246     {
1247         default: // css::drawing::LineCap_BUTT:
1248         {
1249             eCairoLineCap = CAIRO_LINE_CAP_BUTT;
1250             break;
1251         }
1252         case css::drawing::LineCap_ROUND:
1253         {
1254             eCairoLineCap = CAIRO_LINE_CAP_ROUND;
1255             break;
1256         }
1257         case css::drawing::LineCap_SQUARE:
1258         {
1259             eCairoLineCap = CAIRO_LINE_CAP_SQUARE;
1260             break;
1261         }
1262     }
1263 
1264     cairo_set_source_rgba(
1265         cr,
1266         rLineColor.GetRed()/255.0,
1267         rLineColor.GetGreen()/255.0,
1268         rLineColor.GetBlue()/255.0,
1269         1.0-fTransparency);
1270 
1271     cairo_set_line_join(cr, eCairoLineJoin);
1272     cairo_set_line_cap(cr, eCairoLineCap);
1273     cairo_set_line_width(cr, aLineWidths.getX());
1274     cairo_set_miter_limit(cr, fMiterLimit);
1275 
1276     // try to access buffered data
1277     std::shared_ptr<SystemDependentData_CairoPath> pSystemDependentData_CairoPath(
1278         rPolyLine.getSystemDependentData<SystemDependentData_CairoPath>());
1279 
1280     if(pSystemDependentData_CairoPath)
1281     {
1282         // check data validity
1283         if(nullptr == pSystemDependentData_CairoPath->getCairoPath()
1284             || pSystemDependentData_CairoPath->getNoJoin() != bNoJoin
1285             || pSystemDependentData_CairoPath->getAntiAliasB2DDraw() != bAntiAliasB2DDraw
1286             || bPixelSnapHairline /*tdf#124700*/ )
1287         {
1288             // data invalid, forget
1289             pSystemDependentData_CairoPath.reset();
1290         }
1291     }
1292 
1293     if(pSystemDependentData_CairoPath)
1294     {
1295         // re-use data
1296         cairo_append_path(cr, pSystemDependentData_CairoPath->getCairoPath());
1297     }
1298     else
1299     {
1300         // create data
1301         if (!bNoJoin)
1302         {
1303             // PixelOffset now reflected in linear transformation used
1304             AddPolygonToPath(
1305                 cr,
1306                 rPolyLine,
1307                 rObjectToDevice, // ObjectToDevice *without* LineDraw-Offset
1308                 !bAntiAliasB2DDraw,
1309                 bPixelSnapHairline);
1310         }
1311         else
1312         {
1313             const sal_uInt32 nPointCount(rPolyLine.count());
1314             const sal_uInt32 nEdgeCount(rPolyLine.isClosed() ? nPointCount : nPointCount - 1);
1315             basegfx::B2DPolygon aEdge;
1316 
1317             aEdge.append(rPolyLine.getB2DPoint(0));
1318             aEdge.append(basegfx::B2DPoint(0.0, 0.0));
1319 
1320             for (sal_uInt32 i(0); i < nEdgeCount; i++)
1321             {
1322                 const sal_uInt32 nNextIndex((i + 1) % nPointCount);
1323                 aEdge.setB2DPoint(1, rPolyLine.getB2DPoint(nNextIndex));
1324                 aEdge.setNextControlPoint(0, rPolyLine.getNextControlPoint(i));
1325                 aEdge.setPrevControlPoint(1, rPolyLine.getPrevControlPoint(nNextIndex));
1326 
1327                 // PixelOffset now reflected in linear transformation used
1328                 AddPolygonToPath(
1329                     cr,
1330                     aEdge,
1331                     rObjectToDevice, // ObjectToDevice *without* LineDraw-Offset
1332                     !bAntiAliasB2DDraw,
1333                     bPixelSnapHairline);
1334 
1335                 // prepare next step
1336                 aEdge.setB2DPoint(0, aEdge.getB2DPoint(1));
1337             }
1338         }
1339 
1340         // copy and add to buffering mechanism
1341         if (!bPixelSnapHairline /*tdf#124700*/)
1342         {
1343             pSystemDependentData_CairoPath = rPolyLine.addOrReplaceSystemDependentData<SystemDependentData_CairoPath>(
1344                 ImplGetSystemDependentDataManager(),
1345                 cairo_copy_path(cr),
1346                 bNoJoin,
1347                 bAntiAliasB2DDraw);
1348         }
1349     }
1350 
1351     // extract extents
1352     if (pExtents)
1353     {
1354         *pExtents = getClippedStrokeDamage(cr);
1355         // transform also extents (ranges) of damage so they can be correctly redrawn
1356         pExtents->transform(aDamageMatrix);
1357     }
1358 
1359     // draw and consume
1360     cairo_stroke(cr);
1361 
1362     return true;
1363 }
1364 
drawPolyLineBezier(sal_uInt32,const SalPoint *,const PolyFlags *)1365 bool SvpSalGraphics::drawPolyLineBezier( sal_uInt32,
1366                                          const SalPoint*,
1367                                          const PolyFlags* )
1368 {
1369     SAL_INFO("vcl.gdi", "unsupported SvpSalGraphics::drawPolyLineBezier case");
1370     return false;
1371 }
1372 
drawPolygonBezier(sal_uInt32,const SalPoint *,const PolyFlags *)1373 bool SvpSalGraphics::drawPolygonBezier( sal_uInt32,
1374                                         const SalPoint*,
1375                                         const PolyFlags* )
1376 {
1377     SAL_INFO("vcl.gdi", "unsupported SvpSalGraphics::drawPolygonBezier case");
1378     return false;
1379 }
1380 
drawPolyPolygonBezier(sal_uInt32,const sal_uInt32 *,const SalPoint * const *,const PolyFlags * const *)1381 bool SvpSalGraphics::drawPolyPolygonBezier( sal_uInt32,
1382                                             const sal_uInt32*,
1383                                             const SalPoint* const*,
1384                                             const PolyFlags* const* )
1385 {
1386     SAL_INFO("vcl.gdi", "unsupported SvpSalGraphics::drawPolyPolygonBezier case");
1387     return false;
1388 }
1389 
1390 namespace
1391 {
add_polygon_path(cairo_t * cr,const basegfx::B2DPolyPolygon & rPolyPolygon,const basegfx::B2DHomMatrix & rObjectToDevice,bool bPixelSnap)1392     void add_polygon_path(cairo_t* cr, const basegfx::B2DPolyPolygon& rPolyPolygon, const basegfx::B2DHomMatrix& rObjectToDevice, bool bPixelSnap)
1393     {
1394         // try to access buffered data
1395         std::shared_ptr<SystemDependentData_CairoPath> pSystemDependentData_CairoPath(
1396             rPolyPolygon.getSystemDependentData<SystemDependentData_CairoPath>());
1397 
1398         if(pSystemDependentData_CairoPath)
1399         {
1400             // re-use data
1401             cairo_append_path(cr, pSystemDependentData_CairoPath->getCairoPath());
1402         }
1403         else
1404         {
1405             // create data
1406             for (const auto & rPoly : rPolyPolygon)
1407             {
1408                 // PixelOffset used: Was dependent of 'm_aLineColor != SALCOLOR_NONE'
1409                 // Adapt setupPolyPolygon-users to set a linear transformation to achieve PixelOffset
1410                 AddPolygonToPath(
1411                     cr,
1412                     rPoly,
1413                     rObjectToDevice,
1414                     bPixelSnap,
1415                     false);
1416             }
1417 
1418             // copy and add to buffering mechanism
1419             // for decisions how/what to buffer, see Note in WinSalGraphicsImpl::drawPolyPolygon
1420             pSystemDependentData_CairoPath = rPolyPolygon.addOrReplaceSystemDependentData<SystemDependentData_CairoPath>(
1421                 ImplGetSystemDependentDataManager(),
1422                 cairo_copy_path(cr),
1423                 false,
1424                 false);
1425         }
1426     }
1427 }
1428 
drawPolyPolygon(const basegfx::B2DHomMatrix & rObjectToDevice,const basegfx::B2DPolyPolygon & rPolyPolygon,double fTransparency)1429 bool SvpSalGraphics::drawPolyPolygon(
1430     const basegfx::B2DHomMatrix& rObjectToDevice,
1431     const basegfx::B2DPolyPolygon& rPolyPolygon,
1432     double fTransparency)
1433 {
1434     const bool bHasFill(m_aFillColor != SALCOLOR_NONE);
1435     const bool bHasLine(m_aLineColor != SALCOLOR_NONE);
1436 
1437     if(0 == rPolyPolygon.count() || !(bHasFill || bHasLine) || fTransparency < 0.0 || fTransparency >= 1.0)
1438     {
1439         return true;
1440     }
1441 
1442     cairo_t* cr = getCairoContext(true);
1443     clipRegion(cr);
1444 
1445     // Set full (Object-to-Device) transformation - if used
1446     if(!rObjectToDevice.isIdentity())
1447     {
1448         cairo_matrix_t aMatrix;
1449 
1450         cairo_matrix_init(
1451             &aMatrix,
1452             rObjectToDevice.get( 0, 0 ),
1453             rObjectToDevice.get( 1, 0 ),
1454             rObjectToDevice.get( 0, 1 ),
1455             rObjectToDevice.get( 1, 1 ),
1456             rObjectToDevice.get( 0, 2 ),
1457             rObjectToDevice.get( 1, 2 ));
1458         cairo_set_matrix(cr, &aMatrix);
1459     }
1460 
1461     // To make releaseCairoContext work, use empty extents
1462     basegfx::B2DRange extents;
1463 
1464     if (bHasFill)
1465     {
1466         add_polygon_path(cr, rPolyPolygon, rObjectToDevice, !getAntiAliasB2DDraw());
1467 
1468         applyColor(cr, m_aFillColor, fTransparency);
1469         // Get FillDamage (will be extended for LineDamage below)
1470         extents = getClippedFillDamage(cr);
1471 
1472         cairo_fill(cr);
1473     }
1474 
1475     if (bHasLine)
1476     {
1477         // PixelOffset used: Set PixelOffset as linear transformation
1478         cairo_matrix_t aMatrix;
1479         cairo_matrix_init_translate(&aMatrix, 0.5, 0.5);
1480         cairo_set_matrix(cr, &aMatrix);
1481 
1482         add_polygon_path(cr, rPolyPolygon, rObjectToDevice, !getAntiAliasB2DDraw());
1483 
1484         applyColor(cr, m_aLineColor, fTransparency);
1485 
1486         // expand with possible StrokeDamage
1487         basegfx::B2DRange stroke_extents = getClippedStrokeDamage(cr);
1488         stroke_extents.transform(basegfx::utils::createTranslateB2DHomMatrix(0.5, 0.5));
1489         extents.expand(stroke_extents);
1490 
1491         cairo_stroke(cr);
1492     }
1493 
1494     // if transformation has been applied, transform also extents (ranges)
1495     // of damage so they can be correctly redrawn
1496     extents.transform(rObjectToDevice);
1497     releaseCairoContext(cr, true, extents);
1498 
1499     return true;
1500 }
1501 
implDrawGradient(basegfx::B2DPolyPolygon const & rPolyPolygon,SalGradient const & rGradient)1502 bool SvpSalGraphics::implDrawGradient(basegfx::B2DPolyPolygon const & rPolyPolygon, SalGradient const & rGradient)
1503 {
1504     cairo_t* cr = getCairoContext(true);
1505     clipRegion(cr);
1506 
1507     basegfx::B2DHomMatrix rObjectToDevice;
1508 
1509     for (auto const & rPolygon : rPolyPolygon)
1510         AddPolygonToPath(cr, rPolygon, rObjectToDevice, !getAntiAliasB2DDraw(), false);
1511 
1512     cairo_pattern_t* pattern;
1513     pattern = cairo_pattern_create_linear(rGradient.maPoint1.getX(), rGradient.maPoint1.getY(), rGradient.maPoint2.getX(), rGradient.maPoint2.getY());
1514 
1515     for (SalGradientStop const & rStop : rGradient.maStops)
1516     {
1517         double r = rStop.maColor.GetRed() / 255.0;
1518         double g = rStop.maColor.GetGreen() / 255.0;
1519         double b = rStop.maColor.GetBlue() / 255.0;
1520         double a = (0xFF - rStop.maColor.GetTransparency()) / 255.0;
1521         double offset = rStop.mfOffset;
1522 
1523         cairo_pattern_add_color_stop_rgba(pattern, offset, r, g, b, a);
1524     }
1525     cairo_set_source(cr, pattern);
1526 
1527     basegfx::B2DRange extents = getClippedFillDamage(cr);
1528     cairo_fill_preserve(cr);
1529 
1530     releaseCairoContext(cr, true, extents);
1531 
1532     return true;
1533 }
1534 
applyColor(cairo_t * cr,Color aColor,double fTransparency)1535 void SvpSalGraphics::applyColor(cairo_t *cr, Color aColor, double fTransparency)
1536 {
1537     if (cairo_surface_get_content(m_pSurface) == CAIRO_CONTENT_COLOR_ALPHA)
1538     {
1539         cairo_set_source_rgba(cr, aColor.GetRed()/255.0,
1540                                   aColor.GetGreen()/255.0,
1541                                   aColor.GetBlue()/255.0,
1542                                   1.0 - fTransparency);
1543     }
1544     else
1545     {
1546         double fSet = aColor == COL_BLACK ? 1.0 : 0.0;
1547         cairo_set_source_rgba(cr, 1, 1, 1, fSet);
1548         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
1549     }
1550 }
1551 
copyArea(long nDestX,long nDestY,long nSrcX,long nSrcY,long nSrcWidth,long nSrcHeight,bool)1552 void SvpSalGraphics::copyArea( long nDestX,
1553                                long nDestY,
1554                                long nSrcX,
1555                                long nSrcY,
1556                                long nSrcWidth,
1557                                long nSrcHeight,
1558                                bool /*bWindowInvalidate*/ )
1559 {
1560     SalTwoRect aTR(nSrcX, nSrcY, nSrcWidth, nSrcHeight, nDestX, nDestY, nSrcWidth, nSrcHeight);
1561     copyBits(aTR, this);
1562 }
1563 
renderWithOperator(cairo_t * cr,const SalTwoRect & rTR,cairo_surface_t * source,cairo_operator_t eOperator=CAIRO_OPERATOR_SOURCE)1564 static basegfx::B2DRange renderWithOperator(cairo_t* cr, const SalTwoRect& rTR,
1565                                           cairo_surface_t* source, cairo_operator_t eOperator = CAIRO_OPERATOR_SOURCE)
1566 {
1567     cairo_rectangle(cr, rTR.mnDestX, rTR.mnDestY, rTR.mnDestWidth, rTR.mnDestHeight);
1568 
1569     basegfx::B2DRange extents = getClippedFillDamage(cr);
1570 
1571     cairo_clip(cr);
1572 
1573     cairo_translate(cr, rTR.mnDestX, rTR.mnDestY);
1574     double fXScale = 1.0f;
1575     double fYScale = 1.0f;
1576     if (rTR.mnSrcWidth != 0 && rTR.mnSrcHeight != 0) {
1577         fXScale = static_cast<double>(rTR.mnDestWidth)/rTR.mnSrcWidth;
1578         fYScale = static_cast<double>(rTR.mnDestHeight)/rTR.mnSrcHeight;
1579         cairo_scale(cr, fXScale, fYScale);
1580     }
1581 
1582     cairo_save(cr);
1583     cairo_set_source_surface(cr, source, -rTR.mnSrcX, -rTR.mnSrcY);
1584     if ((fXScale != 1.0 && rTR.mnSrcWidth == 1) || (fYScale != 1.0 && rTR.mnSrcHeight == 1))
1585     {
1586         cairo_pattern_t* sourcepattern = cairo_get_source(cr);
1587         cairo_pattern_set_extend(sourcepattern, CAIRO_EXTEND_REPEAT);
1588         cairo_pattern_set_filter(sourcepattern, CAIRO_FILTER_NEAREST);
1589     }
1590     cairo_set_operator(cr, eOperator);
1591     cairo_paint(cr);
1592     cairo_restore(cr);
1593 
1594     return extents;
1595 }
1596 
renderSource(cairo_t * cr,const SalTwoRect & rTR,cairo_surface_t * source)1597 static basegfx::B2DRange renderSource(cairo_t* cr, const SalTwoRect& rTR,
1598                                           cairo_surface_t* source)
1599 {
1600     return renderWithOperator(cr, rTR, source, CAIRO_OPERATOR_SOURCE);
1601 }
1602 
copyWithOperator(const SalTwoRect & rTR,cairo_surface_t * source,cairo_operator_t eOp)1603 void SvpSalGraphics::copyWithOperator( const SalTwoRect& rTR, cairo_surface_t* source,
1604                                  cairo_operator_t eOp )
1605 {
1606     cairo_t* cr = getCairoContext(false);
1607     clipRegion(cr);
1608 
1609     basegfx::B2DRange extents = renderWithOperator(cr, rTR, source, eOp);
1610 
1611     releaseCairoContext(cr, false, extents);
1612 }
1613 
copySource(const SalTwoRect & rTR,cairo_surface_t * source)1614 void SvpSalGraphics::copySource( const SalTwoRect& rTR, cairo_surface_t* source )
1615 {
1616    copyWithOperator(rTR, source, CAIRO_OPERATOR_SOURCE);
1617 }
1618 
copyBits(const SalTwoRect & rTR,SalGraphics * pSrcGraphics)1619 void SvpSalGraphics::copyBits( const SalTwoRect& rTR,
1620                                SalGraphics*      pSrcGraphics )
1621 {
1622     SalTwoRect aTR(rTR);
1623 
1624     SvpSalGraphics* pSrc = pSrcGraphics ?
1625         static_cast<SvpSalGraphics*>(pSrcGraphics) : this;
1626 
1627     cairo_surface_t* source = pSrc->m_pSurface;
1628 
1629     cairo_surface_t *pCopy = nullptr;
1630     if (pSrc == this)
1631     {
1632         //self copy is a problem, so dup source in that case
1633         pCopy = cairo_surface_create_similar(source,
1634                                             cairo_surface_get_content(m_pSurface),
1635                                             aTR.mnSrcWidth * m_fScale,
1636                                             aTR.mnSrcHeight * m_fScale);
1637         dl_cairo_surface_set_device_scale(pCopy, m_fScale, m_fScale);
1638         cairo_t* cr = cairo_create(pCopy);
1639         cairo_set_source_surface(cr, source, -aTR.mnSrcX, -aTR.mnSrcY);
1640         cairo_rectangle(cr, 0, 0, aTR.mnSrcWidth, aTR.mnSrcHeight);
1641         cairo_fill(cr);
1642         cairo_destroy(cr);
1643 
1644         source = pCopy;
1645 
1646         aTR.mnSrcX = 0;
1647         aTR.mnSrcY = 0;
1648     }
1649 
1650     copySource(aTR, source);
1651 
1652     if (pCopy)
1653         cairo_surface_destroy(pCopy);
1654 }
1655 
drawBitmap(const SalTwoRect & rTR,const SalBitmap & rSourceBitmap)1656 void SvpSalGraphics::drawBitmap(const SalTwoRect& rTR, const SalBitmap& rSourceBitmap)
1657 {
1658     SourceHelper aSurface(rSourceBitmap);
1659     cairo_surface_t* source = aSurface.getSurface();
1660     copyWithOperator(rTR, source, CAIRO_OPERATOR_OVER);
1661 }
1662 
drawBitmap(const SalTwoRect & rTR,const BitmapBuffer * pBuffer,cairo_operator_t eOp)1663 void SvpSalGraphics::drawBitmap(const SalTwoRect& rTR, const BitmapBuffer* pBuffer, cairo_operator_t eOp)
1664 {
1665     cairo_surface_t* source = createCairoSurface( pBuffer );
1666     copyWithOperator(rTR, source, eOp);
1667     cairo_surface_destroy(source);
1668 }
1669 
drawBitmap(const SalTwoRect & rTR,const SalBitmap & rSourceBitmap,const SalBitmap & rTransparentBitmap)1670 void SvpSalGraphics::drawBitmap( const SalTwoRect& rTR,
1671                                  const SalBitmap& rSourceBitmap,
1672                                  const SalBitmap& rTransparentBitmap )
1673 {
1674     drawAlphaBitmap(rTR, rSourceBitmap, rTransparentBitmap);
1675 }
1676 
drawMask(const SalTwoRect & rTR,const SalBitmap & rSalBitmap,Color nMaskColor)1677 void SvpSalGraphics::drawMask( const SalTwoRect& rTR,
1678                                const SalBitmap& rSalBitmap,
1679                                Color nMaskColor )
1680 {
1681     /** creates an image from the given rectangle, replacing all black pixels
1682      *  with nMaskColor and make all other full transparent */
1683     SourceHelper aSurface(rSalBitmap, true); // The mask is argb32
1684     if (!aSurface.getSurface())
1685     {
1686         SAL_WARN("vcl.gdi", "unsupported SvpSalGraphics::drawMask case");
1687         return;
1688     }
1689     sal_Int32 nStride;
1690     unsigned char *mask_data = aSurface.getBits(nStride);
1691     vcl::bitmap::lookup_table unpremultiply_table = vcl::bitmap::get_unpremultiply_table();
1692     for (long y = rTR.mnSrcY ; y < rTR.mnSrcY + rTR.mnSrcHeight; ++y)
1693     {
1694         unsigned char *row = mask_data + (nStride*y);
1695         unsigned char *data = row + (rTR.mnSrcX * 4);
1696         for (long x = rTR.mnSrcX; x < rTR.mnSrcX + rTR.mnSrcWidth; ++x)
1697         {
1698             sal_uInt8 a = data[SVP_CAIRO_ALPHA];
1699             sal_uInt8 b = unpremultiply_table[a][data[SVP_CAIRO_BLUE]];
1700             sal_uInt8 g = unpremultiply_table[a][data[SVP_CAIRO_GREEN]];
1701             sal_uInt8 r = unpremultiply_table[a][data[SVP_CAIRO_RED]];
1702             if (r == 0 && g == 0 && b == 0)
1703             {
1704                 data[0] = nMaskColor.GetBlue();
1705                 data[1] = nMaskColor.GetGreen();
1706                 data[2] = nMaskColor.GetRed();
1707                 data[3] = 0xff;
1708             }
1709             else
1710             {
1711                 data[0] = 0;
1712                 data[1] = 0;
1713                 data[2] = 0;
1714                 data[3] = 0;
1715             }
1716             data+=4;
1717         }
1718     }
1719     aSurface.mark_dirty();
1720 
1721     cairo_t* cr = getCairoContext(false);
1722     clipRegion(cr);
1723 
1724     cairo_rectangle(cr, rTR.mnDestX, rTR.mnDestY, rTR.mnDestWidth, rTR.mnDestHeight);
1725 
1726     basegfx::B2DRange extents = getClippedFillDamage(cr);
1727 
1728     cairo_clip(cr);
1729 
1730     cairo_translate(cr, rTR.mnDestX, rTR.mnDestY);
1731     double fXScale = static_cast<double>(rTR.mnDestWidth)/rTR.mnSrcWidth;
1732     double fYScale = static_cast<double>(rTR.mnDestHeight)/rTR.mnSrcHeight;
1733     cairo_scale(cr, fXScale, fYScale);
1734     cairo_set_source_surface(cr, aSurface.getSurface(), -rTR.mnSrcX, -rTR.mnSrcY);
1735     if ((fXScale != 1.0 && rTR.mnSrcWidth == 1) || (fYScale != 1.0 && rTR.mnSrcHeight == 1))
1736     {
1737         cairo_pattern_t* sourcepattern = cairo_get_source(cr);
1738         cairo_pattern_set_extend(sourcepattern, CAIRO_EXTEND_REPEAT);
1739         cairo_pattern_set_filter(sourcepattern, CAIRO_FILTER_NEAREST);
1740     }
1741     cairo_paint(cr);
1742 
1743     releaseCairoContext(cr, false, extents);
1744 }
1745 
getBitmap(long nX,long nY,long nWidth,long nHeight)1746 std::shared_ptr<SalBitmap> SvpSalGraphics::getBitmap( long nX, long nY, long nWidth, long nHeight )
1747 {
1748     std::shared_ptr<SvpSalBitmap> pBitmap = std::make_shared<SvpSalBitmap>();
1749     BitmapPalette aPal;
1750     if (GetBitCount() == 1)
1751     {
1752         aPal.SetEntryCount(2);
1753         aPal[0] = COL_BLACK;
1754         aPal[1] = COL_WHITE;
1755     }
1756 
1757     if (!pBitmap->Create(Size(nWidth, nHeight), GetBitCount(), aPal))
1758     {
1759         SAL_WARN("vcl.gdi", "SvpSalGraphics::getBitmap, cannot create bitmap");
1760         return nullptr;
1761     }
1762 
1763     cairo_surface_t* target = SvpSalGraphics::createCairoSurface(pBitmap->GetBuffer());
1764     if (!target)
1765     {
1766         SAL_WARN("vcl.gdi", "SvpSalGraphics::getBitmap, cannot create cairo surface");
1767         return nullptr;
1768     }
1769     cairo_t* cr = cairo_create(target);
1770 
1771     SalTwoRect aTR(nX, nY, nWidth, nHeight, 0, 0, nWidth, nHeight);
1772     renderSource(cr, aTR, m_pSurface);
1773 
1774     cairo_destroy(cr);
1775     cairo_surface_destroy(target);
1776 
1777     Toggle1BitTransparency(*pBitmap->GetBuffer());
1778 
1779     return pBitmap;
1780 }
1781 
getPixel(long nX,long nY)1782 Color SvpSalGraphics::getPixel( long nX, long nY )
1783 {
1784 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 12, 0)
1785     cairo_surface_t *target = cairo_surface_create_similar_image(m_pSurface, CAIRO_FORMAT_ARGB32, 1, 1);
1786 #else
1787     cairo_surface_t *target = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1);
1788 #endif
1789 
1790     cairo_t* cr = cairo_create(target);
1791 
1792     cairo_rectangle(cr, 0, 0, 1, 1);
1793     cairo_set_source_surface(cr, m_pSurface, -nX, -nY);
1794     cairo_paint(cr);
1795     cairo_destroy(cr);
1796 
1797     cairo_surface_flush(target);
1798     vcl::bitmap::lookup_table unpremultiply_table = vcl::bitmap::get_unpremultiply_table();
1799     unsigned char *data = cairo_image_surface_get_data(target);
1800     sal_uInt8 a = data[SVP_CAIRO_ALPHA];
1801     sal_uInt8 b = unpremultiply_table[a][data[SVP_CAIRO_BLUE]];
1802     sal_uInt8 g = unpremultiply_table[a][data[SVP_CAIRO_GREEN]];
1803     sal_uInt8 r = unpremultiply_table[a][data[SVP_CAIRO_RED]];
1804     Color aColor(0xFF - a, r, g, b);
1805     cairo_surface_destroy(target);
1806 
1807     return aColor;
1808 }
1809 
1810 namespace
1811 {
create_stipple()1812     cairo_pattern_t * create_stipple()
1813     {
1814         static unsigned char data[16] = { 0xFF, 0xFF, 0x00, 0x00,
1815                                           0xFF, 0xFF, 0x00, 0x00,
1816                                           0x00, 0x00, 0xFF, 0xFF,
1817                                           0x00, 0x00, 0xFF, 0xFF };
1818         cairo_surface_t* surface = cairo_image_surface_create_for_data(data, CAIRO_FORMAT_A8, 4, 4, 4);
1819         cairo_pattern_t* pattern = cairo_pattern_create_for_surface(surface);
1820         cairo_surface_destroy(surface);
1821         cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
1822         cairo_pattern_set_filter(pattern, CAIRO_FILTER_NEAREST);
1823         return pattern;
1824     }
1825 }
1826 
invert(const basegfx::B2DPolygon & rPoly,SalInvert nFlags)1827 void SvpSalGraphics::invert(const basegfx::B2DPolygon &rPoly, SalInvert nFlags)
1828 {
1829     cairo_t* cr = getCairoContext(false);
1830     clipRegion(cr);
1831 
1832     // To make releaseCairoContext work, use empty extents
1833     basegfx::B2DRange extents;
1834 
1835     AddPolygonToPath(
1836         cr,
1837         rPoly,
1838         basegfx::B2DHomMatrix(),
1839         !getAntiAliasB2DDraw(),
1840         false);
1841 
1842     cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
1843 
1844     if (cairo_version() >= CAIRO_VERSION_ENCODE(1, 10, 0))
1845     {
1846         cairo_set_operator(cr, CAIRO_OPERATOR_DIFFERENCE);
1847     }
1848     else
1849     {
1850         SAL_WARN("vcl.gdi", "SvpSalGraphics::invert, archaic cairo");
1851     }
1852 
1853     if (nFlags & SalInvert::TrackFrame)
1854     {
1855         cairo_set_line_width(cr, 2.0);
1856         const double dashLengths[2] = { 4.0, 4.0 };
1857         cairo_set_dash(cr, dashLengths, 2, 0);
1858 
1859         extents = getClippedStrokeDamage(cr);
1860         //see tdf#106577 under wayland, some pixel droppings seen, maybe we're
1861         //out by one somewhere, or cairo_stroke_extents is confused by
1862         //dashes/line width
1863         if(!extents.isEmpty())
1864         {
1865             extents.grow(1);
1866         }
1867 
1868         cairo_stroke(cr);
1869     }
1870     else
1871     {
1872         extents = getClippedFillDamage(cr);
1873 
1874         cairo_clip(cr);
1875 
1876         if (nFlags & SalInvert::N50)
1877         {
1878             cairo_pattern_t *pattern = create_stipple();
1879             cairo_surface_t* surface = cairo_surface_create_similar(m_pSurface,
1880                                                                     cairo_surface_get_content(m_pSurface),
1881                                                                     extents.getWidth() * m_fScale,
1882                                                                     extents.getHeight() * m_fScale);
1883 
1884             dl_cairo_surface_set_device_scale(surface, m_fScale, m_fScale);
1885             cairo_t* stipple_cr = cairo_create(surface);
1886             cairo_set_source_rgb(stipple_cr, 1.0, 1.0, 1.0);
1887             cairo_mask(stipple_cr, pattern);
1888             cairo_pattern_destroy(pattern);
1889             cairo_destroy(stipple_cr);
1890             cairo_mask_surface(cr, surface, extents.getMinX(), extents.getMinY());
1891             cairo_surface_destroy(surface);
1892         }
1893         else
1894         {
1895             cairo_paint(cr);
1896         }
1897     }
1898 
1899     releaseCairoContext(cr, false, extents);
1900 }
1901 
invert(long nX,long nY,long nWidth,long nHeight,SalInvert nFlags)1902 void SvpSalGraphics::invert( long nX, long nY, long nWidth, long nHeight, SalInvert nFlags )
1903 {
1904     basegfx::B2DPolygon aRect = basegfx::utils::createPolygonFromRect(basegfx::B2DRectangle(nX, nY, nX+nWidth, nY+nHeight));
1905 
1906     invert(aRect, nFlags);
1907 }
1908 
invert(sal_uInt32 nPoints,const SalPoint * pPtAry,SalInvert nFlags)1909 void SvpSalGraphics::invert(sal_uInt32 nPoints, const SalPoint* pPtAry, SalInvert nFlags)
1910 {
1911     basegfx::B2DPolygon aPoly;
1912     aPoly.append(basegfx::B2DPoint(pPtAry->mnX, pPtAry->mnY), nPoints);
1913     for (sal_uInt32 i = 1; i < nPoints; ++i)
1914         aPoly.setB2DPoint(i, basegfx::B2DPoint(pPtAry[i].mnX, pPtAry[i].mnY));
1915     aPoly.setClosed(true);
1916 
1917     invert(aPoly, nFlags);
1918 }
1919 
drawEPS(long,long,long,long,void *,sal_uInt32)1920 bool SvpSalGraphics::drawEPS( long, long, long, long, void*, sal_uInt32 )
1921 {
1922     return false;
1923 }
1924 
1925 namespace
1926 {
isCairoCompatible(const BitmapBuffer * pBuffer)1927     bool isCairoCompatible(const BitmapBuffer* pBuffer)
1928     {
1929         if (!pBuffer)
1930             return false;
1931 
1932         // We use Cairo that supports 24-bit RGB.
1933 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
1934         if (pBuffer->mnBitCount != 32 && pBuffer->mnBitCount != 24 && pBuffer->mnBitCount != 1)
1935 #else
1936         if (pBuffer->mnBitCount != 32 && pBuffer->mnBitCount != 1)
1937 #endif
1938             return false;
1939 
1940         cairo_format_t nFormat = getCairoFormat(*pBuffer);
1941         return (cairo_format_stride_for_width(nFormat, pBuffer->mnWidth) == pBuffer->mnScanlineSize);
1942     }
1943 }
1944 
createCairoSurface(const BitmapBuffer * pBuffer)1945 cairo_surface_t* SvpSalGraphics::createCairoSurface(const BitmapBuffer *pBuffer)
1946 {
1947     if (!isCairoCompatible(pBuffer))
1948         return nullptr;
1949 
1950     cairo_format_t nFormat = getCairoFormat(*pBuffer);
1951     cairo_surface_t *target =
1952         cairo_image_surface_create_for_data(pBuffer->mpBits,
1953                                         nFormat,
1954                                         pBuffer->mnWidth, pBuffer->mnHeight,
1955                                         pBuffer->mnScanlineSize);
1956     if (cairo_surface_status(target) != CAIRO_STATUS_SUCCESS)
1957     {
1958         cairo_surface_destroy(target);
1959         return nullptr;
1960     }
1961     return target;
1962 }
1963 
createTmpCompatibleCairoContext() const1964 cairo_t* SvpSalGraphics::createTmpCompatibleCairoContext() const
1965 {
1966 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 12, 0)
1967     cairo_surface_t *target = cairo_surface_create_similar_image(m_pSurface,
1968 #else
1969     cairo_surface_t *target = cairo_image_surface_create(
1970 #endif
1971             CAIRO_FORMAT_ARGB32,
1972             m_aFrameSize.getX() * m_fScale,
1973             m_aFrameSize.getY() * m_fScale);
1974 
1975     dl_cairo_surface_set_device_scale(target, m_fScale, m_fScale);
1976 
1977     return cairo_create(target);
1978 }
1979 
getCairoContext(bool bXorModeAllowed) const1980 cairo_t* SvpSalGraphics::getCairoContext(bool bXorModeAllowed) const
1981 {
1982     cairo_t* cr;
1983     if (m_ePaintMode == PaintMode::Xor && bXorModeAllowed)
1984         cr = createTmpCompatibleCairoContext();
1985     else
1986         cr = cairo_create(m_pSurface);
1987     cairo_set_line_width(cr, 1);
1988     cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
1989     cairo_set_antialias(cr, getAntiAliasB2DDraw() ? CAIRO_ANTIALIAS_DEFAULT : CAIRO_ANTIALIAS_NONE);
1990     cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
1991 
1992     // ensure no linear transformation and no PathInfo in local cairo_path_t
1993     cairo_identity_matrix(cr);
1994     cairo_new_path(cr);
1995 
1996     return cr;
1997 }
1998 
getDamageKey()1999 cairo_user_data_key_t* SvpSalGraphics::getDamageKey()
2000 {
2001     static cairo_user_data_key_t aDamageKey;
2002     return &aDamageKey;
2003 }
2004 
releaseCairoContext(cairo_t * cr,bool bXorModeAllowed,const basegfx::B2DRange & rExtents) const2005 void SvpSalGraphics::releaseCairoContext(cairo_t* cr, bool bXorModeAllowed, const basegfx::B2DRange& rExtents) const
2006 {
2007     const bool bXoring = (m_ePaintMode == PaintMode::Xor && bXorModeAllowed);
2008 
2009     if (rExtents.isEmpty())
2010     {
2011         //nothing changed, return early
2012         if (bXoring)
2013         {
2014             cairo_surface_t* surface = cairo_get_target(cr);
2015             cairo_surface_destroy(surface);
2016         }
2017         cairo_destroy(cr);
2018         return;
2019     }
2020 
2021     basegfx::B2IRange aIntExtents(basegfx::unotools::b2ISurroundingRangeFromB2DRange(rExtents));
2022     sal_Int32 nExtentsLeft(aIntExtents.getMinX()), nExtentsTop(aIntExtents.getMinY());
2023     sal_Int32 nExtentsRight(aIntExtents.getMaxX()), nExtentsBottom(aIntExtents.getMaxY());
2024     sal_Int32 nWidth = m_aFrameSize.getX();
2025     sal_Int32 nHeight = m_aFrameSize.getY();
2026     nExtentsLeft = std::max<sal_Int32>(nExtentsLeft, 0);
2027     nExtentsTop = std::max<sal_Int32>(nExtentsTop, 0);
2028     nExtentsRight = std::min<sal_Int32>(nExtentsRight, nWidth);
2029     nExtentsBottom = std::min<sal_Int32>(nExtentsBottom, nHeight);
2030 
2031     cairo_surface_t* surface = cairo_get_target(cr);
2032     cairo_surface_flush(surface);
2033 
2034     //For the most part we avoid the use of XOR these days, but there
2035     //are some edge cases where legacy stuff still supports it, so
2036     //emulate it (slowly) here.
2037     if (bXoring)
2038     {
2039         cairo_surface_t* target_surface = m_pSurface;
2040         if (cairo_surface_get_type(target_surface) != CAIRO_SURFACE_TYPE_IMAGE)
2041         {
2042             //in the unlikely case we can't use m_pSurface directly, copy contents
2043             //to another temp image surface
2044             cairo_t* copycr = createTmpCompatibleCairoContext();
2045             cairo_rectangle(copycr, nExtentsLeft, nExtentsTop,
2046                                     nExtentsRight - nExtentsLeft,
2047                                     nExtentsBottom - nExtentsTop);
2048             cairo_set_source_surface(copycr, m_pSurface, 0, 0);
2049             cairo_paint(copycr);
2050             target_surface = cairo_get_target(copycr);
2051             cairo_destroy(copycr);
2052         }
2053 
2054         cairo_surface_flush(target_surface);
2055         unsigned char *target_surface_data = cairo_image_surface_get_data(target_surface);
2056         unsigned char *xor_surface_data = cairo_image_surface_get_data(surface);
2057 
2058         cairo_format_t nFormat = cairo_image_surface_get_format(target_surface);
2059         assert(nFormat == CAIRO_FORMAT_ARGB32 && "need to implement CAIRO_FORMAT_A1 after all here");
2060         sal_Int32 nStride = cairo_format_stride_for_width(nFormat, nWidth * m_fScale);
2061         sal_Int32 nUnscaledExtentsLeft = nExtentsLeft * m_fScale;
2062         sal_Int32 nUnscaledExtentsRight = nExtentsRight * m_fScale;
2063         sal_Int32 nUnscaledExtentsTop = nExtentsTop * m_fScale;
2064         sal_Int32 nUnscaledExtentsBottom = nExtentsBottom * m_fScale;
2065         vcl::bitmap::lookup_table unpremultiply_table = vcl::bitmap::get_unpremultiply_table();
2066         vcl::bitmap::lookup_table premultiply_table = vcl::bitmap::get_premultiply_table();
2067         for (sal_Int32 y = nUnscaledExtentsTop; y < nUnscaledExtentsBottom; ++y)
2068         {
2069             unsigned char *true_row = target_surface_data + (nStride*y);
2070             unsigned char *xor_row = xor_surface_data + (nStride*y);
2071             unsigned char *true_data = true_row + (nUnscaledExtentsLeft * 4);
2072             unsigned char *xor_data = xor_row + (nUnscaledExtentsLeft * 4);
2073             for (sal_Int32 x = nUnscaledExtentsLeft; x < nUnscaledExtentsRight; ++x)
2074             {
2075                 sal_uInt8 a = true_data[SVP_CAIRO_ALPHA];
2076                 sal_uInt8 xor_a = xor_data[SVP_CAIRO_ALPHA];
2077                 sal_uInt8 b = unpremultiply_table[a][true_data[SVP_CAIRO_BLUE]] ^
2078                               unpremultiply_table[xor_a][xor_data[SVP_CAIRO_BLUE]];
2079                 sal_uInt8 g = unpremultiply_table[a][true_data[SVP_CAIRO_GREEN]] ^
2080                               unpremultiply_table[xor_a][xor_data[SVP_CAIRO_GREEN]];
2081                 sal_uInt8 r = unpremultiply_table[a][true_data[SVP_CAIRO_RED]] ^
2082                               unpremultiply_table[xor_a][xor_data[SVP_CAIRO_RED]];
2083                 true_data[SVP_CAIRO_BLUE] = premultiply_table[a][b];
2084                 true_data[SVP_CAIRO_GREEN] = premultiply_table[a][g];
2085                 true_data[SVP_CAIRO_RED] = premultiply_table[a][r];
2086                 true_data+=4;
2087                 xor_data+=4;
2088             }
2089         }
2090         cairo_surface_mark_dirty(target_surface);
2091 
2092         if (target_surface != m_pSurface)
2093         {
2094             cairo_t* copycr = cairo_create(m_pSurface);
2095             //unlikely case we couldn't use m_pSurface directly, copy contents
2096             //back from image surface
2097             cairo_rectangle(copycr, nExtentsLeft, nExtentsTop,
2098                                     nExtentsRight - nExtentsLeft,
2099                                     nExtentsBottom - nExtentsTop);
2100             cairo_set_source_surface(copycr, target_surface, 0, 0);
2101             cairo_paint(copycr);
2102             cairo_destroy(copycr);
2103             cairo_surface_destroy(target_surface);
2104         }
2105 
2106         cairo_surface_destroy(surface);
2107     }
2108 
2109     cairo_destroy(cr); // unref
2110 
2111     DamageHandler* pDamage = static_cast<DamageHandler*>(cairo_surface_get_user_data(m_pSurface, getDamageKey()));
2112 
2113     if (pDamage)
2114     {
2115         pDamage->damaged(pDamage->handle, nExtentsLeft, nExtentsTop,
2116                                           nExtentsRight - nExtentsLeft,
2117                                           nExtentsBottom - nExtentsTop);
2118     }
2119 }
2120 
2121 #if ENABLE_CAIRO_CANVAS
SupportsCairo() const2122 bool SvpSalGraphics::SupportsCairo() const
2123 {
2124     return false;
2125 }
2126 
CreateSurface(const cairo::CairoSurfaceSharedPtr &) const2127 cairo::SurfaceSharedPtr SvpSalGraphics::CreateSurface(const cairo::CairoSurfaceSharedPtr& /*rSurface*/) const
2128 {
2129     return cairo::SurfaceSharedPtr();
2130 }
2131 
CreateSurface(const OutputDevice &,int,int,int,int) const2132 cairo::SurfaceSharedPtr SvpSalGraphics::CreateSurface(const OutputDevice& /*rRefDevice*/, int /*x*/, int /*y*/, int /*width*/, int /*height*/) const
2133 {
2134     return cairo::SurfaceSharedPtr();
2135 }
2136 
CreateBitmapSurface(const OutputDevice &,const BitmapSystemData &,const Size &) const2137 cairo::SurfaceSharedPtr SvpSalGraphics::CreateBitmapSurface(const OutputDevice& /*rRefDevice*/, const BitmapSystemData& /*rData*/, const Size& /*rSize*/) const
2138 {
2139     return cairo::SurfaceSharedPtr();
2140 }
2141 
GetNativeSurfaceHandle(cairo::SurfaceSharedPtr &,const basegfx::B2ISize &) const2142 css::uno::Any SvpSalGraphics::GetNativeSurfaceHandle(cairo::SurfaceSharedPtr& /*rSurface*/, const basegfx::B2ISize& /*rSize*/) const
2143 {
2144     return css::uno::Any();
2145 }
2146 
2147 #endif // ENABLE_CAIRO_CANVAS
2148 
GetGraphicsData() const2149 SystemGraphicsData SvpSalGraphics::GetGraphicsData() const
2150 {
2151     return SystemGraphicsData();
2152 }
2153 
supportsOperation(OutDevSupportType eType) const2154 bool SvpSalGraphics::supportsOperation(OutDevSupportType eType) const
2155 {
2156     switch (eType)
2157     {
2158         case OutDevSupportType::TransparentRect:
2159         case OutDevSupportType::B2DDraw:
2160             return true;
2161     }
2162     return false;
2163 }
2164 
getPlatformGlyphCache()2165 GlyphCache& SvpSalGraphics::getPlatformGlyphCache()
2166 {
2167     GenericUnixSalData* const pSalData(GetGenericUnixSalData());
2168     assert(pSalData);
2169     return *pSalData->GetGlyphCache();
2170 }
2171 
dl_cairo_surface_set_device_scale(cairo_surface_t * surface,double x_scale,double y_scale)2172 void dl_cairo_surface_set_device_scale(cairo_surface_t *surface, double x_scale, double y_scale)
2173 {
2174     static auto func = reinterpret_cast<void(*)(cairo_surface_t*, double, double)>(
2175         dlsym(nullptr, "cairo_surface_set_device_scale"));
2176     if (func)
2177         func(surface, x_scale, y_scale);
2178 }
2179 
dl_cairo_surface_get_device_scale(cairo_surface_t * surface,double * x_scale,double * y_scale)2180 void dl_cairo_surface_get_device_scale(cairo_surface_t *surface, double* x_scale, double* y_scale)
2181 {
2182     static auto func = reinterpret_cast<void(*)(cairo_surface_t*, double*, double*)>(
2183         dlsym(nullptr, "cairo_surface_get_device_scale"));
2184     if (func)
2185         func(surface, x_scale, y_scale);
2186     else
2187     {
2188         if (x_scale)
2189             *x_scale = 1.0;
2190         if (y_scale)
2191             *y_scale = 1.0;
2192     }
2193 }
2194 
2195 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
2196