Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / emfio / source / emfuno / xemfparser.cxx
blobe9175de10d60c2d26da296002a0bbf72a43108cc
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 #include <sal/config.h>
22 #include <com/sun/star/graphic/XEmfParser.hpp>
23 #include <com/sun/star/lang/XServiceInfo.hpp>
24 #include <com/sun/star/lang/XInitialization.hpp>
25 #include <cppuhelper/implbase2.hxx>
26 #include <cppuhelper/supportsservice.hxx>
27 #include <comphelper/sequence.hxx>
28 #include <drawinglayer/primitive2d/baseprimitive2d.hxx>
29 #include <basegfx/color/bcolor.hxx>
30 #include <basegfx/polygon/b2dpolygon.hxx>
31 #include <basegfx/polygon/b2dpolygontools.hxx>
32 #include <drawinglayer/primitive2d/polypolygonprimitive2d.hxx>
34 #include <vcl/svapp.hxx>
35 #include <vcl/wmfexternal.hxx>
36 #include <basegfx/matrix/b2dhommatrixtools.hxx>
37 #include <unotools/ucbstreamhelper.hxx>
38 #include <drawinglayer/primitive2d/metafileprimitive2d.hxx>
40 #include <wmfreader.hxx>
41 #include <emfreader.hxx>
43 //#include <com/sun/star/xml/sax/XParser.hpp>
44 //#include <com/sun/star/xml/sax/Parser.hpp>
45 //#include <com/sun/star/xml/sax/InputSource.hpp>
46 //#include <drawinglayer/geometry/viewinformation2d.hxx>
47 //#include <svgdocumenthandler.hxx>
49 #include "xemfparser.hxx"
51 using namespace ::com::sun::star;
53 namespace emfio
55 namespace emfreader
57 class XEmfParser : public ::cppu::WeakAggImplHelper2< graphic::XEmfParser, lang::XServiceInfo >
59 private:
60 uno::Reference< uno::XComponentContext > context_;
62 protected:
63 public:
64 explicit XEmfParser(
65 uno::Reference< uno::XComponentContext > const & context);
66 XEmfParser(const XEmfParser&) = delete;
67 XEmfParser& operator=(const XEmfParser&) = delete;
69 // XEmfParser
70 virtual uno::Sequence< uno::Reference< ::graphic::XPrimitive2D > > SAL_CALL getDecomposition(
71 const uno::Reference< ::io::XInputStream >& xEmfStream,
72 const OUString& aAbsolutePath,
73 const uno::Sequence< ::beans::PropertyValue >& rProperties) override;
75 // XServiceInfo
76 virtual OUString SAL_CALL getImplementationName() override;
77 virtual sal_Bool SAL_CALL supportsService(const OUString&) override;
78 virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
80 } // end of namespace emfreader
81 } // end of namespace emfio
83 // uno functions
84 namespace emfio
86 namespace emfreader
88 uno::Sequence< OUString > XEmfParser_getSupportedServiceNames()
90 return uno::Sequence< OUString > { "com.sun.star.graphic.EmfTools" };
93 OUString XEmfParser_getImplementationName()
95 return OUString( "emfio::emfreader::XEmfParser" );
98 uno::Reference< uno::XInterface > XEmfParser_createInstance(const uno::Reference< uno::XComponentContext >& context)
100 return static_cast< ::cppu::OWeakObject* >(new XEmfParser(context));
102 } // end of namespace emfreader
103 } // end of namespace emfio
105 namespace emfio
107 namespace emfreader
109 XEmfParser::XEmfParser(
110 uno::Reference< uno::XComponentContext > const & context):
111 context_(context)
115 uno::Sequence< uno::Reference< ::graphic::XPrimitive2D > > XEmfParser::getDecomposition(
116 const uno::Reference< ::io::XInputStream >& xEmfStream,
117 const OUString& /*aAbsolutePath*/,
118 const uno::Sequence< ::beans::PropertyValue >& rProperties)
120 drawinglayer::primitive2d::Primitive2DContainer aRetval;
122 if (xEmfStream.is())
124 WmfExternal aExternalHeader;
125 const bool bExternalHeaderUsed(aExternalHeader.setSequence(rProperties));
127 // rough check - import and conv to primitive
128 GDIMetaFile aMtf;
129 std::unique_ptr<SvStream> pStream(::utl::UcbStreamHelper::CreateStream(xEmfStream));
130 sal_uInt32 nOrgPos = pStream->Tell();
132 SvStreamEndian nOrigNumberFormat = pStream->GetEndian();
133 pStream->SetEndian(SvStreamEndian::LITTLE);
135 sal_uInt32 nMetaType(0);
136 if (checkSeek(*pStream, 0x28))
137 pStream->ReadUInt32(nMetaType);
138 pStream->Seek(nOrgPos);
140 bool bReadError(false);
142 if (nMetaType == 0x464d4520)
144 // read and get possible failure/error, ReadEnhWMF returns success
145 bReadError = !emfio::EmfReader(*pStream, aMtf).ReadEnhWMF();
147 else
149 emfio::WmfReader(*pStream, aMtf, bExternalHeaderUsed ? &aExternalHeader : nullptr).ReadWMF();
151 // Need to check for ErrCode at stream to not lose former work.
152 // This may contain important information and will behave the
153 // same as before. When we have an error, do not create content
154 ErrCode aErrCode(pStream->GetError());
156 bReadError = aErrCode.IsError();
159 pStream->SetEndian(nOrigNumberFormat);
161 if (!bReadError)
163 Size aSize(aMtf.GetPrefSize());
165 if (aMtf.GetPrefMapMode().GetMapUnit() == MapUnit::MapPixel)
167 aSize = Application::GetDefaultDevice()->PixelToLogic(aSize, MapMode(MapUnit::Map100thMM));
169 else
171 aSize = OutputDevice::LogicToLogic(aSize, aMtf.GetPrefMapMode(), MapMode(MapUnit::Map100thMM));
174 // use size
175 const basegfx::B2DHomMatrix aMetafileTransform(
176 basegfx::utils::createScaleB2DHomMatrix(
177 aSize.Width(),
178 aSize.Height()));
180 // ...and create a single MetafilePrimitive2D containing the Metafile.
181 // CAUTION: Currently, ReadWindowMetafile uses the local VectorGraphicData
182 // and a MetafileAccessor hook at the MetafilePrimitive2D inside of
183 // ImpGraphic::ImplGetGDIMetaFile to get the Metafile. Thus, the first
184 // and only primitive in this case *has to be* a MetafilePrimitive2D.
185 aRetval.push_back(
186 new drawinglayer::primitive2d::MetafilePrimitive2D(
187 aMetafileTransform,
188 aMtf));
190 // // force to use decomposition directly to get rid of the metafile
191 // const css::uno::Sequence< css::beans::PropertyValue > aViewParameters;
192 // drawinglayer::primitive2d::MetafilePrimitive2D aMetafilePrimitive2D(
193 // aMetafileTransform,
194 // aMtf);
195 // aRetval.append(aMetafilePrimitive2D.getDecomposition(aViewParameters));
197 // if (aRetval.empty())
198 // {
199 // // for test, just create some graphic data that will get visualized
200 // const basegfx::B2DRange aRange(1000, 1000, 5000, 5000);
201 // const basegfx::BColor aColor(1.0, 0.0, 0.0);
202 // const basegfx::B2DPolygon aOutline(basegfx::utils::createPolygonFromRect(aRange));
204 // aRetval.push_back(new drawinglayer::primitive2d::PolyPolygonColorPrimitive2D(basegfx::B2DPolyPolygon(aOutline), aColor));
205 // }
208 else
210 OSL_ENSURE(false, "Invalid stream (!)");
213 return comphelper::containerToSequence(aRetval);
216 OUString SAL_CALL XEmfParser::getImplementationName()
218 return XEmfParser_getImplementationName();
221 sal_Bool SAL_CALL XEmfParser::supportsService(const OUString& rServiceName)
223 return cppu::supportsService(this, rServiceName);
226 uno::Sequence< OUString > SAL_CALL XEmfParser::getSupportedServiceNames()
228 return XEmfParser_getSupportedServiceNames();
231 } // end of namespace emfreader
232 } // end of namespace emfio
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */