tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / vcl / headless / svpvd.cxx
blobd2b96f6253cfbf2f567fcdf7906ca9cb75af4ddb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef IOS
22 #include <headless/svpvd.hxx>
23 #include <headless/svpgdi.hxx>
25 #include <basegfx/vector/b2ivector.hxx>
26 #include <comphelper/lok.hxx>
28 #include <cairo.h>
30 using namespace basegfx;
32 SvpSalVirtualDevice::SvpSalVirtualDevice(cairo_surface_t* pRefSurface, cairo_surface_t* pPreExistingTarget)
33 : m_pRefSurface(pRefSurface)
34 , m_pSurface(pPreExistingTarget)
35 , m_bOwnsSurface(!pPreExistingTarget)
37 cairo_surface_reference(m_pRefSurface);
40 SvpSalVirtualDevice::~SvpSalVirtualDevice()
42 if (m_bOwnsSurface)
43 cairo_surface_destroy(m_pSurface);
44 cairo_surface_destroy(m_pRefSurface);
47 SvpSalGraphics* SvpSalVirtualDevice::AddGraphics(SvpSalGraphics* pGraphics)
49 pGraphics->setSurface(m_pSurface, m_aFrameSize);
50 m_aGraphics.push_back(pGraphics);
51 return pGraphics;
54 SalGraphics* SvpSalVirtualDevice::AcquireGraphics()
56 return AddGraphics(new SvpSalGraphics());
59 void SvpSalVirtualDevice::ReleaseGraphics( SalGraphics* pGraphics )
61 std::erase(m_aGraphics, dynamic_cast<SvpSalGraphics*>(pGraphics));
62 delete pGraphics;
65 bool SvpSalVirtualDevice::SetSize( tools::Long nNewDX, tools::Long nNewDY )
67 return SetSizeUsingBuffer(nNewDX, nNewDY, nullptr);
70 bool SvpSalVirtualDevice::CreateSurface(tools::Long nNewDX, tools::Long nNewDY, sal_uInt8 *const pBuffer)
72 if (m_pSurface)
74 cairo_surface_destroy(m_pSurface);
77 if (pBuffer)
79 // The buffer should only be set by VirtualDevice::SetOutputSizePixelScaleOffsetAndLOKBuffer()
80 // when used to draw a tile for LOK. It cannot be used for something else, because otherwise
81 // this would need a way to detect whether this is a tiled paint that needs LOK handling
82 // or whether it's that something else that just might happen to be called with LOK active.
83 assert(comphelper::LibreOfficeKit::isActive());
84 // Force scaling of the painting
85 double fScale = comphelper::LibreOfficeKit::getDPIScale();
87 m_pSurface = cairo_image_surface_create_for_data(pBuffer, CAIRO_FORMAT_ARGB32,
88 nNewDX, nNewDY, cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, nNewDX));
89 dl_cairo_surface_set_device_scale(m_pSurface, fScale, fScale);
91 else if(nNewDX <= 32 && nNewDY <= 32)
93 double fXScale, fYScale;
94 dl_cairo_surface_get_device_scale(m_pRefSurface, &fXScale, &fYScale);
95 nNewDX *= fXScale;
96 nNewDY *= fYScale;
98 // Force image-based surface if small. Small VirtualDevice instances are often used for small
99 // temporary bitmaps that will eventually have GetBitmap() called on them, which would cause
100 // X Server roundtrip with Xlib-based surface, which may be way more costly than doing the drawing
101 // in software (which should be fairly cheap for small surfaces anyway).
102 m_pSurface = cairo_surface_create_similar_image(m_pRefSurface, CAIRO_FORMAT_ARGB32, nNewDX, nNewDY);
103 dl_cairo_surface_set_device_scale(m_pSurface, fXScale, fYScale);
105 else
107 m_pSurface = cairo_surface_create_similar(m_pRefSurface, CAIRO_CONTENT_COLOR_ALPHA, nNewDX, nNewDY);
108 // Device scale is inherited in this case.
111 SAL_WARN_IF(cairo_surface_status(m_pSurface) != CAIRO_STATUS_SUCCESS, "vcl", "surface of size " << nNewDX << " by " << nNewDY << " creation failed with status of: " << cairo_status_to_string(cairo_surface_status(m_pSurface)));
112 return cairo_surface_status(m_pSurface) == CAIRO_STATUS_SUCCESS;
115 bool SvpSalVirtualDevice::SetSizeUsingBuffer( tools::Long nNewDX, tools::Long nNewDY,
116 sal_uInt8 *const pBuffer)
118 bool bSuccess = true;
120 if (nNewDX == 0)
121 nNewDX = 1;
122 if (nNewDY == 0)
123 nNewDY = 1;
125 if (!m_pSurface || m_aFrameSize.getX() != nNewDX ||
126 m_aFrameSize.getY() != nNewDY)
128 m_aFrameSize = basegfx::B2IVector(nNewDX, nNewDY);
130 if (m_bOwnsSurface)
131 bSuccess = CreateSurface(nNewDX, nNewDY, pBuffer);
133 assert(m_pSurface);
135 // update device in existing graphics
136 for (auto const& graphic : m_aGraphics)
137 graphic->setSurface(m_pSurface, m_aFrameSize);
140 return bSuccess;
143 tools::Long SvpSalVirtualDevice::GetWidth() const
145 return m_pSurface ? m_aFrameSize.getX() : 0;
148 tools::Long SvpSalVirtualDevice::GetHeight() const
150 return m_pSurface ? m_aFrameSize.getY() : 0;
153 #endif
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */