Coverage Report

Created: 2026-08-14 10:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/emfio/source/emfuno/xemfparser.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 <sal/config.h>
21
22
#include <com/sun/star/graphic/XEmfParser.hpp>
23
#include <com/sun/star/lang/XServiceInfo.hpp>
24
#include <cppuhelper/implbase.hxx>
25
#include <cppuhelper/supportsservice.hxx>
26
27
#include <utility>
28
#include <tools/mapunit.hxx>
29
#include <vcl/outdev.hxx>
30
#include <vcl/svapp.hxx>
31
#include <vcl/wmfexternal.hxx>
32
#include <basegfx/matrix/b2dhommatrixtools.hxx>
33
#include <unotools/ucbstreamhelper.hxx>
34
#include <drawinglayer/primitive2d/metafileprimitive2d.hxx>
35
#include <sal/log.hxx>
36
#include <comphelper/sequenceashashmap.hxx>
37
38
#include <wmfreader.hxx>
39
#include <emfreader.hxx>
40
41
using namespace ::com::sun::star;
42
43
namespace emfio::emfreader
44
{
45
        namespace {
46
47
        class XEmfParser : public ::cppu::WeakImplHelper< graphic::XEmfParser, lang::XServiceInfo >
48
        {
49
        private:
50
            uno::Reference< uno::XComponentContext > context_;
51
            basegfx::B2DTuple maSizeHint;
52
53
        public:
54
            explicit XEmfParser(
55
                uno::Reference< uno::XComponentContext > context);
56
            XEmfParser(const XEmfParser&) = delete;
57
            XEmfParser& operator=(const XEmfParser&) = delete;
58
59
            // XEmfParser
60
            virtual uno::Sequence< uno::Reference< ::graphic::XPrimitive2D > > SAL_CALL getDecomposition(
61
                const uno::Reference< ::io::XInputStream >& xEmfStream,
62
                const OUString& aAbsolutePath,
63
                const uno::Sequence< ::beans::PropertyValue >& rProperties) override;
64
            void SAL_CALL setSizeHint(const geometry::RealPoint2D& rSize) override;
65
66
            // XServiceInfo
67
            virtual OUString SAL_CALL getImplementationName() override;
68
            virtual sal_Bool SAL_CALL supportsService(const OUString&) override;
69
            virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
70
        };
71
72
        }
73
74
        XEmfParser::XEmfParser(
75
            uno::Reference< uno::XComponentContext > context):
76
14.8k
            context_(std::move(context))
77
14.8k
        {
78
14.8k
        }
79
80
        uno::Sequence< uno::Reference< ::graphic::XPrimitive2D > > XEmfParser::getDecomposition(
81
            const uno::Reference< ::io::XInputStream >& xEmfStream,
82
            const OUString& /*aAbsolutePath*/,
83
            const uno::Sequence< ::beans::PropertyValue >& rProperties)
84
14.8k
        {
85
14.8k
            drawinglayer::primitive2d::Primitive2DContainer aRetval;
86
87
14.8k
            if (xEmfStream.is())
88
14.8k
            {
89
14.8k
                WmfExternal aExternalHeader;
90
14.8k
                const bool bExternalHeaderUsed(aExternalHeader.setSequence(rProperties));
91
14.8k
                bool bEnableEMFPlus = true;
92
14.8k
                comphelper::SequenceAsHashMap aMap(rProperties);
93
14.8k
                auto it = aMap.find(u"EMFPlusEnable"_ustr);
94
14.8k
                if (it != aMap.end())
95
0
                {
96
0
                    bool bValue;
97
0
                    if (it->second >>= bValue)
98
0
                    {
99
0
                        bEnableEMFPlus = bValue;
100
0
                    }
101
0
                }
102
103
                // rough check - import and conv to primitive
104
14.8k
                GDIMetaFile aMtf;
105
14.8k
                std::unique_ptr<SvStream> pStream(::utl::UcbStreamHelper::CreateStream(xEmfStream));
106
14.8k
                sal_uInt64 nOrgPos = pStream->Tell();
107
108
14.8k
                SvStreamEndian nOrigNumberFormat = pStream->GetEndian();
109
14.8k
                pStream->SetEndian(SvStreamEndian::LITTLE);
110
111
14.8k
                sal_uInt32 nMetaType(0);
112
14.8k
                if (checkSeek(*pStream, 0x28))
113
13.5k
                    pStream->ReadUInt32(nMetaType);
114
14.8k
                pStream->Seek(nOrgPos);
115
116
14.8k
                bool bReadError(false);
117
118
14.8k
                try
119
14.8k
                {
120
14.8k
                    if (nMetaType == 0x464d4520)
121
5.52k
                    {
122
                        // read and get possible failure/error, ReadEnhWMF returns success
123
5.52k
                        emfio::EmfReader aReader(*pStream, aMtf);
124
5.52k
                        aReader.SetSizeHint(maSizeHint);
125
5.52k
                        if (!bEnableEMFPlus)
126
0
                        {
127
0
                            aReader.SetEnableEMFPlus(bEnableEMFPlus);
128
0
                        }
129
5.52k
                        bReadError = !aReader.ReadEnhWMF();
130
5.52k
                    }
131
9.33k
                    else
132
9.33k
                    {
133
9.33k
                        emfio::WmfReader aReader(*pStream, aMtf, bExternalHeaderUsed ? &aExternalHeader : nullptr);
134
9.33k
                        if (!bEnableEMFPlus)
135
0
                            aReader.SetEnableEMFPlus(bEnableEMFPlus);
136
9.33k
                        aReader.ReadWMF();
137
138
                        // Need to check for ErrCode at stream to not lose former work.
139
                        // This may contain important information and will behave the
140
                        // same as before. When we have an error, do not create content
141
9.33k
                        ErrCode aErrCode(pStream->GetError());
142
143
9.33k
                        bReadError = aErrCode.IsError();
144
9.33k
                    }
145
14.8k
                }
146
14.8k
                catch (...)
147
14.8k
                {
148
0
                    bReadError = true;
149
0
                }
150
151
14.8k
                pStream->SetEndian(nOrigNumberFormat);
152
153
14.8k
                if (!bReadError)
154
2.91k
                {
155
2.91k
                    Size aSize(aMtf.GetPrefSize());
156
157
2.91k
                    if (aMtf.GetPrefMapMode().GetMapUnit() == MapUnit::MapPixel)
158
0
                    {
159
0
                        aSize = Application::GetDefaultDevice()->PixelToLogic(aSize, MapMode(MapUnit::Map100thMM));
160
0
                    }
161
2.91k
                    else
162
2.91k
                    {
163
2.91k
                        aSize = OutputDevice::LogicToLogic(aSize, aMtf.GetPrefMapMode(), MapMode(MapUnit::Map100thMM));
164
2.91k
                    }
165
166
                    // use size
167
2.91k
                    const basegfx::B2DHomMatrix aMetafileTransform(
168
2.91k
                        basegfx::utils::createScaleB2DHomMatrix(
169
2.91k
                            aSize.Width(),
170
2.91k
                            aSize.Height()));
171
172
                    // ...and create a single MetafilePrimitive2D containing the Metafile.
173
                    // CAUTION: Currently, ReadWindowMetafile uses the local VectorGraphicData
174
                    // and a MetafileAccessor hook at the MetafilePrimitive2D inside of
175
                    // ImpGraphic::ImplGetGDIMetaFile to get the Metafile. Thus, the first
176
                    // and only primitive in this case *has to be* a MetafilePrimitive2D.
177
2.91k
                    aRetval.push_back(
178
2.91k
                        new drawinglayer::primitive2d::MetafilePrimitive2D(
179
2.91k
                            aMetafileTransform,
180
2.91k
                            aMtf));
181
182
                    // // force to use decomposition directly to get rid of the metafile
183
                    // const css::uno::Sequence< css::beans::PropertyValue > aViewParameters;
184
                    // drawinglayer::primitive2d::MetafilePrimitive2D aMetafilePrimitive2D(
185
                    //     aMetafileTransform,
186
                    //     aMtf);
187
                    // aRetval.append(aMetafilePrimitive2D.getDecomposition(aViewParameters));
188
189
                    // if (aRetval.empty())
190
                    // {
191
                    //     // for test, just create some graphic data that will get visualized
192
                    //     const basegfx::B2DRange aRange(1000, 1000, 5000, 5000);
193
                    //     const basegfx::BColor aColor(1.0, 0.0, 0.0);
194
                    //     const basegfx::B2DPolygon aOutline(basegfx::utils::createPolygonFromRect(aRange));
195
                    //
196
                    //     aRetval.push_back(new drawinglayer::primitive2d::PolyPolygonColorPrimitive2D(basegfx::B2DPolyPolygon(aOutline), aColor));
197
                    // }
198
2.91k
                }
199
14.8k
            }
200
0
            else
201
0
            {
202
0
                SAL_WARN("emfio", "Invalid stream (!)");
203
0
            }
204
205
14.8k
            return aRetval.toSequence();
206
14.8k
        }
207
208
        void XEmfParser::setSizeHint(const geometry::RealPoint2D& rSize)
209
14.8k
        {
210
14.8k
            maSizeHint.setX(rSize.X);
211
14.8k
            maSizeHint.setY(rSize.Y);
212
14.8k
        }
213
214
        OUString SAL_CALL XEmfParser::getImplementationName()
215
0
        {
216
0
            return u"emfio::emfreader::XEmfParser"_ustr;
217
0
        }
218
219
        sal_Bool SAL_CALL XEmfParser::supportsService(const OUString& rServiceName)
220
0
        {
221
0
            return cppu::supportsService(this, rServiceName);
222
0
        }
223
224
        uno::Sequence< OUString > SAL_CALL XEmfParser::getSupportedServiceNames()
225
0
        {
226
0
            return { u"com.sun.star.graphic.EmfTools"_ustr };
227
0
        }
228
229
} // end of namespace emfio::emfreader
230
231
232
233
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
234
emfio_emfreader_XEmfParser_get_implementation(
235
    css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
236
14.8k
{
237
14.8k
    return cppu::acquire(new emfio::emfreader::XEmfParser(context));
238
14.8k
}
239
240
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */