/* -*- 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/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //#include //#include //#include //#include //#include #include "xemfparser.hxx" using namespace ::com::sun::star; namespace emfio { namespace emfreader { class XEmfParser : public ::cppu::WeakAggImplHelper2< graphic::XEmfParser, lang::XServiceInfo > { private: uno::Reference< uno::XComponentContext > context_; protected: public: explicit XEmfParser( uno::Reference< uno::XComponentContext > const & context); XEmfParser(const XEmfParser&) = delete; XEmfParser& operator=(const XEmfParser&) = delete; // XEmfParser virtual uno::Sequence< uno::Reference< ::graphic::XPrimitive2D > > SAL_CALL getDecomposition( const uno::Reference< ::io::XInputStream >& xEmfStream, const OUString& aAbsolutePath) override; // XServiceInfo virtual OUString SAL_CALL getImplementationName() override; virtual sal_Bool SAL_CALL supportsService(const OUString&) override; virtual uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override; }; } // end of namespace emfreader } // end of namespace emfio // uno functions namespace emfio { namespace emfreader { uno::Sequence< OUString > XEmfParser_getSupportedServiceNames() { return uno::Sequence< OUString > { "com.sun.star.graphic.EmfTools" }; } OUString XEmfParser_getImplementationName() { return OUString( "emfio::emfreader::XEmfParser" ); } uno::Reference< uno::XInterface > SAL_CALL XEmfParser_createInstance(const uno::Reference< uno::XComponentContext >& context) { return static_cast< ::cppu::OWeakObject* >(new XEmfParser(context)); } } // end of namespace emfreader } // end of namespace emfio namespace emfio { namespace emfreader { XEmfParser::XEmfParser( uno::Reference< uno::XComponentContext > const & context): context_(context) { } uno::Sequence< uno::Reference< ::graphic::XPrimitive2D > > XEmfParser::getDecomposition( const uno::Reference< ::io::XInputStream >& xEmfStream, const OUString& aAbsolutePath ) { drawinglayer::primitive2d::Primitive2DContainer aRetval; if (xEmfStream.is()) { static bool bTestCode(false); if (bTestCode) { static bool bUseOldFilterEmbedded(true); if (bUseOldFilterEmbedded) { GDIMetaFile aMtf; std::unique_ptr pStream(::utl::UcbStreamHelper::CreateStream(xEmfStream)); if (pStream && ConvertWMFToGDIMetaFile(*pStream, aMtf, nullptr, nullptr)) { Size aSize(aMtf.GetPrefSize()); if (aMtf.GetPrefMapMode().GetMapUnit() == MapUnit::MapPixel) { aSize = Application::GetDefaultDevice()->PixelToLogic(aSize, MapUnit::Map100thMM); } else { aSize = OutputDevice::LogicToLogic(aSize, aMtf.GetPrefMapMode(), MapMode(MapUnit::Map100thMM)); } const basegfx::B2DHomMatrix aMetafileTransform( basegfx::tools::createScaleB2DHomMatrix( aSize.Width(), aSize.Height())); aRetval.push_back( new drawinglayer::primitive2d::MetafilePrimitive2D( aMetafileTransform, aMtf)); } } if(aRetval.empty()) { // for test, just create some graphic data that will get visualized const basegfx::B2DRange aRange(1000, 1000, 5000, 5000); const basegfx::BColor aColor(1.0, 0.0, 0.0); const basegfx::B2DPolygon aOutline(basegfx::tools::createPolygonFromRect(aRange)); aRetval.push_back(new drawinglayer::primitive2d::PolyPolygonColorPrimitive2D(basegfx::B2DPolyPolygon(aOutline), aColor)); } } else { // new parser here bool bBla = true; // rouch check - import and conv to primitive GDIMetaFile aMtf; std::unique_ptr pStream(::utl::UcbStreamHelper::CreateStream(xEmfStream)); sal_uInt32 nMetaType(0); sal_uInt32 nOrgPos = pStream->Tell(); SvStreamEndian nOrigNumberFormat = pStream->GetEndian(); pStream->SetEndian(SvStreamEndian::LITTLE); pStream->Seek(0x28); pStream->ReadUInt32(nMetaType); pStream->Seek(nOrgPos); if (nMetaType == 0x464d4520) { emfio::EmfReader(*pStream, aMtf).ReadEnhWMF(); } else { emfio::WmfReader(*pStream, aMtf).ReadWMF(); } pStream->SetEndian(nOrigNumberFormat); Size aSize(aMtf.GetPrefSize()); if (aMtf.GetPrefMapMode().GetMapUnit() == MapUnit::MapPixel) { aSize = Application::GetDefaultDevice()->PixelToLogic(aSize, MapUnit::Map100thMM); } else { aSize = OutputDevice::LogicToLogic(aSize, aMtf.GetPrefMapMode(), MapMode(MapUnit::Map100thMM)); } const basegfx::B2DHomMatrix aMetafileTransform( basegfx::tools::createScaleB2DHomMatrix( aSize.Width(), aSize.Height())); // force to use decomposition directly to get rid of the metafile const css::uno::Sequence< css::beans::PropertyValue > aViewParameters; drawinglayer::primitive2d::MetafilePrimitive2D aMetafilePrimitive2D( aMetafileTransform, aMtf); aRetval.append(aMetafilePrimitive2D.getDecomposition(aViewParameters)); } } else { OSL_ENSURE(false, "Invalid stream (!)"); } return comphelper::containerToSequence(aRetval); } OUString SAL_CALL XEmfParser::getImplementationName() { return(XEmfParser_getImplementationName()); } sal_Bool SAL_CALL XEmfParser::supportsService(const OUString& rServiceName) { return cppu::supportsService(this, rServiceName); } uno::Sequence< OUString > SAL_CALL XEmfParser::getSupportedServiceNames() { return XEmfParser_getSupportedServiceNames(); } } // end of namespace emfreader } // end of namespace emfio /* vim:set shiftwidth=4 softtabstop=4 expandtab: */