Coverage Report

Created: 2025-12-31 10:39

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