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 
10 #include <utility>
11 
12 #include <Qt5SvpSurface.hxx>
13 
14 #include <Qt5SvpGraphics.hxx>
15 
16 #include <vcl/sysdata.hxx>
17 #include <vcl/bitmap.hxx>
18 #include <vcl/virdev.hxx>
19 #include <vcl/window.hxx>
20 #include <basegfx/vector/b2isize.hxx>
21 
22 namespace
23 {
get_surface_size(cairo_surface_t * surface)24 Size get_surface_size(cairo_surface_t* surface)
25 {
26     cairo_t* cr = cairo_create(surface);
27     double x1, x2, y1, y2;
28     cairo_clip_extents(cr, &x1, &y1, &x2, &y2);
29     cairo_destroy(cr);
30     return Size(x2 - x1, y2 - y1);
31 }
32 }
33 
34 namespace cairo
35 {
Qt5SvpSurface(const CairoSurfaceSharedPtr & pSurface)36 Qt5SvpSurface::Qt5SvpSurface(const CairoSurfaceSharedPtr& pSurface)
37     : m_pGraphics(nullptr)
38     , m_pCairoContext(nullptr)
39     , m_pSurface(pSurface)
40 {
41 }
42 
Qt5SvpSurface(const Qt5SvpGraphics * pGraphics,int x,int y,int width,int height)43 Qt5SvpSurface::Qt5SvpSurface(const Qt5SvpGraphics* pGraphics, int x, int y, int width, int height)
44     : m_pGraphics(pGraphics)
45     , m_pCairoContext(pGraphics->getCairoContext(false))
46 {
47     cairo_surface_t* surface = cairo_get_target(m_pCairoContext);
48     m_pSurface.reset(cairo_surface_create_for_rectangle(surface, x, y, width, height),
49                      &cairo_surface_destroy);
50 }
51 
~Qt5SvpSurface()52 Qt5SvpSurface::~Qt5SvpSurface()
53 {
54     if (m_pCairoContext)
55         cairo_destroy(m_pCairoContext);
56 }
57 
getCairo() const58 CairoSharedPtr Qt5SvpSurface::getCairo() const
59 {
60     return CairoSharedPtr(cairo_create(m_pSurface.get()), &cairo_destroy);
61 }
62 
getSimilar(int cairo_content_type,int width,int height) const63 SurfaceSharedPtr Qt5SvpSurface::getSimilar(int cairo_content_type, int width, int height) const
64 {
65     return SurfaceSharedPtr(new Qt5SvpSurface(CairoSurfaceSharedPtr(
66         cairo_surface_create_similar(
67             m_pSurface.get(), static_cast<cairo_content_t>(cairo_content_type), width, height),
68         &cairo_surface_destroy)));
69 }
70 
flush() const71 void Qt5SvpSurface::flush() const
72 {
73     cairo_surface_flush(m_pSurface.get());
74     if (m_pGraphics)
75         m_pGraphics->updateQWidget();
76 }
77 
createVirtualDevice() const78 VclPtr<VirtualDevice> Qt5SvpSurface::createVirtualDevice() const
79 {
80     SystemGraphicsData aSystemGraphicsData;
81 
82     aSystemGraphicsData.nSize = sizeof(SystemGraphicsData);
83     aSystemGraphicsData.pSurface = m_pSurface.get();
84 
85     return VclPtr<VirtualDevice>::Create(aSystemGraphicsData, get_surface_size(m_pSurface.get()),
86                                          DeviceFormat::DEFAULT);
87 }
88 
89 } // namespace cairo
90 
91 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
92