Coverage Report

Created: 2025-12-31 10:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/vcl/source/gdi/vectorgraphicdata.cxx
Line
Count
Source
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 <comphelper/diagnose_ex.hxx>
21
#include <tools/stream.hxx>
22
#include <sal/log.hxx>
23
#include <utility>
24
#include <vcl/vectorgraphicdata.hxx>
25
#include <comphelper/processfactory.hxx>
26
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
27
#include <com/sun/star/graphic/PdfTools.hpp>
28
#include <com/sun/star/graphic/SvgTools.hpp>
29
#include <com/sun/star/graphic/EmfTools.hpp>
30
#include <com/sun/star/graphic/Primitive2DTools.hpp>
31
#include <com/sun/star/rendering/XIntegerReadOnlyBitmap.hpp>
32
#include <com/sun/star/util/XAccounting.hpp>
33
#include <com/sun/star/util/XBinaryDataContainer.hpp>
34
#include <basegfx/matrix/b2dhommatrixtools.hxx>
35
#include <vcl/canvastools.hxx>
36
#include <comphelper/seqstream.hxx>
37
#include <comphelper/sequence.hxx>
38
#include <comphelper/propertysequence.hxx>
39
#include <comphelper/propertyvalue.hxx>
40
#include <pdf/PdfConfig.hxx>
41
#include <rtl/crc.h>
42
#include <vcl/svapp.hxx>
43
#include <vcl/outdev.hxx>
44
#include <vcl/wmfexternal.hxx>
45
#include <vcl/pdfread.hxx>
46
#include <unotools/streamwrap.hxx>
47
#include <graphic/UnoBinaryDataContainer.hxx>
48
49
using namespace ::com::sun::star;
50
51
Bitmap convertPrimitive2DSequenceToBitmap(
52
    const std::deque< css::uno::Reference< css::graphic::XPrimitive2D > >& rSequence,
53
    const basegfx::B2DRange& rTargetRange,
54
    const sal_uInt32 nMaximumQuadraticPixels,
55
    const o3tl::Length eTargetUnit,
56
    const std::optional<Size>& rTargetDPI)
57
2.13k
{
58
2.13k
    Bitmap aRetval;
59
60
2.13k
    if(!rSequence.empty())
61
2.13k
    {
62
        // create replacement graphic from maSequence
63
        // create XPrimitive2DRenderer
64
2.13k
        try
65
2.13k
        {
66
2.13k
            const uno::Reference< uno::XComponentContext >& xContext(::comphelper::getProcessComponentContext());
67
2.13k
            const uno::Reference< graphic::XPrimitive2DRenderer > xPrimitive2DRenderer = graphic::Primitive2DTools::create(xContext);
68
69
2.13k
            uno::Sequence< beans::PropertyValue > aViewParameters = {
70
2.13k
                comphelper::makePropertyValue(u"RangeUnit"_ustr, static_cast<sal_Int32>(eTargetUnit)),
71
2.13k
            };
72
2.13k
            geometry::RealRectangle2D aRealRect;
73
74
2.13k
            aRealRect.X1 = rTargetRange.getMinX();
75
2.13k
            aRealRect.Y1 = rTargetRange.getMinY();
76
2.13k
            aRealRect.X2 = rTargetRange.getMaxX();
77
2.13k
            aRealRect.Y2 = rTargetRange.getMaxY();
78
79
            // get system DPI
80
2.13k
            Size aDPI(Application::GetDefaultDevice()->LogicToPixel(Size(1, 1), MapMode(MapUnit::MapInch)));
81
2.13k
            if (rTargetDPI.has_value())
82
0
            {
83
0
                aDPI = *rTargetDPI;
84
0
            }
85
86
2.13k
            const uno::Reference< rendering::XBitmap > xBitmap(
87
2.13k
                xPrimitive2DRenderer->rasterize(
88
2.13k
                    comphelper::containerToSequence(rSequence),
89
2.13k
                    aViewParameters,
90
2.13k
                    aDPI.getWidth(),
91
2.13k
                    aDPI.getHeight(),
92
2.13k
                    aRealRect,
93
2.13k
                    nMaximumQuadraticPixels));
94
95
2.13k
            if(xBitmap.is())
96
5
            {
97
5
                const uno::Reference< rendering::XIntegerReadOnlyBitmap> xIntBmp(xBitmap, uno::UNO_QUERY_THROW);
98
5
                aRetval = vcl::unotools::bitmapFromXBitmap(xIntBmp);
99
5
            }
100
2.13k
        }
101
2.13k
        catch (const uno::Exception&)
102
2.13k
        {
103
2.13k
            TOOLS_WARN_EXCEPTION("vcl", "Got no graphic::XPrimitive2DRenderer!");
104
2.13k
        }
105
2.13k
        catch (const std::exception& e)
106
2.13k
        {
107
0
            SAL_WARN("vcl", "Got no graphic::XPrimitive2DRenderer! : " << e.what());
108
0
        }
109
2.13k
    }
110
111
2.13k
    return aRetval;
112
2.13k
}
113
114
static size_t estimateSize(
115
    std::deque<uno::Reference<graphic::XPrimitive2D>> const& rSequence)
116
42.8k
{
117
42.8k
    size_t nRet(0);
118
42.8k
    for (auto& it : rSequence)
119
7.02k
    {
120
7.02k
        uno::Reference<util::XAccounting> const xAcc(it, uno::UNO_QUERY);
121
7.02k
        assert(xAcc.is()); // we expect only BasePrimitive2D from SVG parser
122
7.02k
        nRet += xAcc->estimateUsage();
123
7.02k
    }
124
42.8k
    return nRet;
125
42.8k
}
126
127
bool VectorGraphicData::operator==(const VectorGraphicData& rCandidate) const
128
0
{
129
0
    if (getType() == rCandidate.getType())
130
0
    {
131
0
        if (maDataContainer.getSize() == rCandidate.maDataContainer.getSize())
132
0
        {
133
0
            if (0 == memcmp(
134
0
                maDataContainer.getData(),
135
0
                rCandidate.maDataContainer.getData(),
136
0
                maDataContainer.getSize()))
137
0
            {
138
0
                return true;
139
0
            }
140
0
        }
141
0
    }
142
143
0
    return false;
144
0
}
145
146
void VectorGraphicData::ensurePdfReplacement()
147
3.31k
{
148
3.31k
    assert(getType() == VectorGraphicDataType::Pdf);
149
150
3.31k
    if (!maReplacement.IsEmpty())
151
0
        return; // nothing to do
152
153
    // use PDFium directly
154
3.31k
    std::vector<Bitmap> aBitmaps;
155
3.31k
    sal_Int32 nUsePageIndex = 0;
156
3.31k
    if (mnPageIndex >= 0)
157
0
        nUsePageIndex = mnPageIndex;
158
3.31k
    vcl::RenderPDFBitmaps(maDataContainer.getData(),
159
3.31k
                          maDataContainer.getSize(), aBitmaps, nUsePageIndex, 1,
160
3.31k
                          &maSizeHint);
161
3.31k
    if (!aBitmaps.empty())
162
1.98k
        maReplacement = aBitmaps[0];
163
3.31k
}
164
165
void VectorGraphicData::ensureReplacement()
166
28.6k
{
167
28.6k
    if (!maReplacement.IsEmpty())
168
0
        return; // nothing to do
169
170
    // shortcut for PDF - PDFium can generate the replacement bitmap for us
171
    // directly
172
28.6k
    if (getType() == VectorGraphicDataType::Pdf)
173
3.31k
    {
174
3.31k
        ensurePdfReplacement();
175
3.31k
        return;
176
3.31k
    }
177
178
25.3k
    ensureSequenceAndRange();
179
180
25.3k
    if (!maSequence.empty())
181
2.13k
    {
182
2.13k
        maReplacement = convertPrimitive2DSequenceToBitmap(maSequence, getRange());
183
2.13k
    }
184
25.3k
}
185
186
Bitmap VectorGraphicData::getBitmap(const Size& pixelSize) const
187
0
{
188
0
    if (!maReplacement.IsEmpty() && maReplacement.GetSizePixel() == pixelSize)
189
0
        return maReplacement;
190
191
0
    if (getType() == VectorGraphicDataType::Pdf)
192
0
    {
193
        // use PDFium directly
194
0
        const sal_Int32 nUsePageIndex = mnPageIndex > 0 ? mnPageIndex : 0;
195
0
        const double dpi = vcl::pdf::getDefaultPdfResolutionDpi();
196
0
        basegfx::B2DTuple sizeMM100(
197
0
            o3tl::convert(pixelSize.Width() / dpi / vcl::PDF_INSERT_MAGIC_SCALE_FACTOR, o3tl::Length::in, o3tl::Length::mm100),
198
0
            o3tl::convert(pixelSize.Height() / dpi / vcl::PDF_INSERT_MAGIC_SCALE_FACTOR, o3tl::Length::in, o3tl::Length::mm100));
199
0
        std::vector<Bitmap> aBitmaps;
200
0
        vcl::RenderPDFBitmaps(maDataContainer.getData(), maDataContainer.getSize(), aBitmaps,
201
0
                              nUsePageIndex, 1, &sizeMM100);
202
0
        if (!aBitmaps.empty())
203
0
            return aBitmaps[0];
204
0
    }
205
206
0
    if (getPrimitive2DSequence().empty())
207
0
        return {};
208
209
0
    Size dpi(
210
0
        std::round(pixelSize.Width() / o3tl::convert(maRange.getWidth(), o3tl::Length::mm100, o3tl::Length::in)),
211
0
        std::round(pixelSize.Height() / o3tl::convert(maRange.getHeight(), o3tl::Length::mm100, o3tl::Length::in)));
212
0
    return convertPrimitive2DSequenceToBitmap(maSequence, maRange, 4096 * 4096, o3tl::Length::mm100, dpi);
213
0
}
214
215
void VectorGraphicData::ensureSequenceAndRange()
216
82.8k
{
217
82.8k
    if (mbSequenceCreated || maDataContainer.isEmpty())
218
40.0k
        return;
219
220
    // import SVG to maSequence, also set maRange
221
42.8k
    maRange.reset();
222
223
    // create Vector Graphic Data interpreter
224
42.8k
    const uno::Reference<uno::XComponentContext>& xContext(::comphelper::getProcessComponentContext());
225
226
42.8k
    switch (getType())
227
42.8k
    {
228
14.3k
        case VectorGraphicDataType::Svg:
229
14.3k
        {
230
14.3k
            const uno::Reference<io::XInputStream> xInputStream = maDataContainer.getAsXInputStream();
231
232
14.3k
            const uno::Reference< graphic::XSvgParser > xSvgParser = graphic::SvgTools::create(xContext);
233
234
14.3k
            if (xInputStream.is())
235
14.3k
                maSequence = comphelper::sequenceToContainer<std::deque<css::uno::Reference< css::graphic::XPrimitive2D >>>(xSvgParser->getDecomposition(xInputStream, OUString()));
236
237
14.3k
            break;
238
0
        }
239
26.6k
        case VectorGraphicDataType::Emf:
240
28.4k
        case VectorGraphicDataType::Wmf:
241
28.4k
        {
242
28.4k
            const uno::Reference< graphic::XEmfParser > xEmfParser = graphic::EmfTools::create(xContext);
243
244
28.4k
            const uno::Reference<io::XInputStream> xInputStream = maDataContainer.getAsXInputStream();
245
246
28.4k
            if (xInputStream.is())
247
28.4k
            {
248
28.4k
                uno::Sequence< ::beans::PropertyValue > aPropertySequence;
249
250
                // Pass the size hint of the graphic to the EMF parser.
251
28.4k
                geometry::RealPoint2D aSizeHint;
252
28.4k
                aSizeHint.X = maSizeHint.getX();
253
28.4k
                aSizeHint.Y = maSizeHint.getY();
254
28.4k
                xEmfParser->setSizeHint(aSizeHint);
255
256
28.4k
                if (!mbEnableEMFPlus)
257
0
                {
258
0
                    aPropertySequence = { comphelper::makePropertyValue(u"EMFPlusEnable"_ustr, uno::Any(false)) };
259
0
                }
260
261
28.4k
                maSequence = comphelper::sequenceToContainer<std::deque<css::uno::Reference< css::graphic::XPrimitive2D >>>(xEmfParser->getDecomposition(xInputStream, OUString(), aPropertySequence));
262
28.4k
            }
263
264
28.4k
            break;
265
26.6k
        }
266
0
        case VectorGraphicDataType::Pdf:
267
0
        {
268
0
            const uno::Reference<graphic::XPdfDecomposer> xPdfDecomposer = graphic::PdfTools::create(xContext);
269
0
            uno::Sequence<beans::PropertyValue> aDecompositionParameters = comphelper::InitPropertySequence({
270
0
                {"PageIndex", uno::Any(sal_Int32(mnPageIndex))},
271
0
            });
272
273
0
            rtl::Reference<UnoBinaryDataContainer> xDataContainer = new UnoBinaryDataContainer(getBinaryDataContainer());
274
275
0
            auto xPrimitive2D = xPdfDecomposer->getDecomposition(xDataContainer, aDecompositionParameters);
276
0
            maSequence = comphelper::sequenceToContainer<std::deque<uno::Reference<graphic::XPrimitive2D>>>(xPrimitive2D);
277
278
0
            break;
279
26.6k
        }
280
42.8k
    }
281
282
42.8k
    if(!maSequence.empty())
283
7.02k
    {
284
7.02k
        const sal_Int32 nCount(maSequence.size());
285
7.02k
        geometry::RealRectangle2D aRealRect;
286
7.02k
        uno::Sequence< beans::PropertyValue > aViewParameters;
287
288
14.0k
        for(sal_Int32 a(0); a < nCount; a++)
289
7.02k
        {
290
            // get reference
291
7.02k
            const css::uno::Reference< css::graphic::XPrimitive2D > xReference(maSequence[a]);
292
293
7.02k
            if(xReference.is())
294
7.02k
            {
295
7.02k
                aRealRect = xReference->getRange(aViewParameters);
296
297
7.02k
                maRange.expand(
298
7.02k
                    basegfx::B2DRange(
299
7.02k
                        aRealRect.X1,
300
7.02k
                        aRealRect.Y1,
301
7.02k
                        aRealRect.X2,
302
7.02k
                        aRealRect.Y2));
303
7.02k
            }
304
7.02k
        }
305
7.02k
    }
306
42.8k
    mNestedBitmapSize = estimateSize(maSequence);
307
42.8k
    mbSequenceCreated = true;
308
42.8k
}
309
310
std::pair<VectorGraphicData::State, size_t> VectorGraphicData::getSizeBytes() const
311
62.6k
{
312
62.6k
    if (!maSequence.empty() && !maDataContainer.isEmpty())
313
0
    {
314
0
        return std::make_pair(State::PARSED, maDataContainer.getSize() + mNestedBitmapSize);
315
0
    }
316
62.6k
    else
317
62.6k
    {
318
62.6k
        return std::make_pair(State::UNPARSED, maDataContainer.getSize());
319
62.6k
    }
320
62.6k
}
321
322
VectorGraphicData::VectorGraphicData(BinaryDataContainer aDataContainer, VectorGraphicDataType eVectorDataType, sal_Int32 nPageIndex)
323
46.4k
:   maDataContainer(std::move(aDataContainer)),
324
46.4k
    mbSequenceCreated(false),
325
46.4k
    mNestedBitmapSize(0),
326
46.4k
    meType(eVectorDataType),
327
46.4k
    mnPageIndex(nPageIndex)
328
46.4k
{
329
46.4k
}
330
331
VectorGraphicData::~VectorGraphicData()
332
46.4k
{
333
46.4k
}
334
335
const basegfx::B2DRange& VectorGraphicData::getRange() const
336
29.0k
{
337
29.0k
    const_cast< VectorGraphicData* >(this)->ensureSequenceAndRange();
338
339
29.0k
    return maRange;
340
29.0k
}
341
342
const std::deque< css::uno::Reference< css::graphic::XPrimitive2D > >& VectorGraphicData::getPrimitive2DSequence() const
343
28.5k
{
344
28.5k
    const_cast< VectorGraphicData* >(this)->ensureSequenceAndRange();
345
346
28.5k
    return maSequence;
347
28.5k
}
348
349
const Bitmap& VectorGraphicData::getReplacement() const
350
28.6k
{
351
28.6k
    const_cast< VectorGraphicData* >(this)->ensureReplacement();
352
353
28.6k
    return maReplacement;
354
28.6k
}
355
356
BitmapChecksum VectorGraphicData::GetChecksum() const
357
0
{
358
0
    return rtl_crc32(0, maDataContainer.getData(), maDataContainer.getSize());
359
0
}
360
361
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */