summaryrefslogtreecommitdiff
path: root/vcl/skia/x11/cairotextrender.cxx
blob: f3cc30b709feeb7131f6a1bc210fd3f24f818581 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <skia/x11/cairotextrender.hxx>

#include <salbmp.hxx>
#include <vcl/salbtype.hxx>
#include <skia/gdiimpl.hxx>

#include <cairo.h>

SkiaX11CairoTextRender::SkiaX11CairoTextRender(X11SalGraphics& rParent)
    : X11CairoTextRender(rParent)
{
}

cairo_t* SkiaX11CairoTextRender::getCairoContext()
{
    cairo_surface_t* surface = nullptr;
    SkiaSalGraphicsImpl* pImpl = dynamic_cast<SkiaSalGraphicsImpl*>(mrParent.GetImpl());
    if (pImpl)
    {
        tools::Rectangle aClipRect = pImpl->getClipRegion().GetBoundRect();
        if (aClipRect.GetWidth() == 0 || aClipRect.GetHeight() == 0)
        {
            aClipRect.setWidth(GetWidth());
            aClipRect.setHeight(GetHeight());
        }
        surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, aClipRect.GetWidth(),
                                             aClipRect.GetHeight());
    }
    if (!surface)
        return nullptr;
    cairo_t* cr = cairo_create(surface);
    cairo_surface_destroy(surface);
    return cr;
}

void SkiaX11CairoTextRender::getSurfaceOffset(double& nDX, double& nDY)
{
    SkiaSalGraphicsImpl* pImpl = dynamic_cast<SkiaSalGraphicsImpl*>(mrParent.GetImpl());
    if (pImpl)
    {
        tools::Rectangle aClipRect = pImpl->getClipRegion().GetBoundRect();
        nDX = -aClipRect.Left();
        nDY = -aClipRect.Top();
    }
}

void SkiaX11CairoTextRender::releaseCairoContext(cairo_t* cr)
{
    SkiaSalGraphicsImpl* pImpl = dynamic_cast<SkiaSalGraphicsImpl*>(mrParent.GetImpl());
    if (!pImpl)
    {
        cairo_destroy(cr);
        return;
    }

    cairo_surface_t* pSurface = cairo_get_target(cr);
    int nWidth = cairo_image_surface_get_width(pSurface);
    int nHeight = cairo_image_surface_get_height(pSurface);
    cairo_surface_flush(pSurface);
    unsigned char* pSrc = cairo_image_surface_get_data(pSurface);

    tools::Rectangle aClipRect = pImpl->getClipRegion().GetBoundRect();
    SalTwoRect aRect(0, 0, nWidth, nHeight, aClipRect.Left(), aClipRect.Top(), nWidth, nHeight);

    SkBitmap bitmap;
    if (!bitmap.installPixels(SkImageInfo::MakeN32Premul(nWidth, nHeight), pSrc, nWidth * 4))
        abort();

    pImpl->drawBitmap(aRect, bitmap);
    bitmap.reset();
    cairo_destroy(cr);
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */