From ed5640d8b587fbcfed7dd7967f3de04b37a76f26 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:06:44 +0200 Subject: Adding upstream version 4:7.4.7. Signed-off-by: Daniel Baumann --- starmath/source/unofilter.cxx | 116 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 starmath/source/unofilter.cxx (limited to 'starmath/source/unofilter.cxx') diff --git a/starmath/source/unofilter.cxx b/starmath/source/unofilter.cxx new file mode 100644 index 000000000..fd150005d --- /dev/null +++ b/starmath/source/unofilter.cxx @@ -0,0 +1,116 @@ +/* -*- 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/. + */ + +#include +#include +#include +#include + +#include +#include "mathtype.hxx" +#include +#include + +using namespace ::com::sun::star; + +namespace +{ +/// Invokes the MathType importer via UNO. +class MathTypeFilter + : public cppu::WeakImplHelper +{ + uno::Reference m_xDstDoc; + +public: + MathTypeFilter(); + + // XFilter + sal_Bool SAL_CALL filter(const uno::Sequence& rDescriptor) override; + void SAL_CALL cancel() override; + + // XImporter + void SAL_CALL setTargetDocument(const uno::Reference& xDoc) override; + + // XServiceInfo + OUString SAL_CALL getImplementationName() override; + sal_Bool SAL_CALL supportsService(const OUString& rServiceName) override; + uno::Sequence SAL_CALL getSupportedServiceNames() override; +}; +} + +MathTypeFilter::MathTypeFilter() = default; + +sal_Bool MathTypeFilter::filter(const uno::Sequence& rDescriptor) +{ + bool bSuccess = false; + try + { + utl::MediaDescriptor aMediaDesc(rDescriptor); + aMediaDesc.addInputStream(); + uno::Reference xInputStream; + aMediaDesc[utl::MediaDescriptor::PROP_INPUTSTREAM] >>= xInputStream; + std::unique_ptr pStream(utl::UcbStreamHelper::CreateStream(xInputStream)); + if (pStream) + { + if (SotStorage::IsStorageFile(pStream.get())) + { + tools::SvRef aStorage(new SotStorage(pStream.get(), false)); + // Is this a MathType Storage? + if (aStorage->IsStream("Equation Native")) + { + if (auto pModel = dynamic_cast(m_xDstDoc.get())) + { + auto pDocShell = static_cast(pModel->GetObjectShell()); + OUStringBuffer aText(pDocShell->GetText()); + MathType aEquation(aText); + bSuccess = aEquation.Parse(aStorage.get()); + if (bSuccess) + { + pDocShell->SetText(aText.makeStringAndClear()); + pDocShell->Parse(); + } + } + } + } + } + } + catch (const uno::Exception&) + { + DBG_UNHANDLED_EXCEPTION("starmath"); + } + return bSuccess; +} + +void MathTypeFilter::cancel() {} + +void MathTypeFilter::setTargetDocument(const uno::Reference& xDoc) +{ + m_xDstDoc = xDoc; +} + +OUString MathTypeFilter::getImplementationName() { return "com.sun.star.comp.Math.MathTypeFilter"; } + +sal_Bool MathTypeFilter::supportsService(const OUString& rServiceName) +{ + return cppu::supportsService(this, rServiceName); +} + +uno::Sequence MathTypeFilter::getSupportedServiceNames() +{ + return { "com.sun.star.document.ImportFilter" }; +} + +extern "C" SAL_DLLPUBLIC_EXPORT uno::XInterface* +com_sun_star_comp_Math_MathTypeFilter_get_implementation(uno::XComponentContext* /*pCtx*/, + uno::Sequence const& /*rSeq*/) +{ + return cppu::acquire(new MathTypeFilter); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit v1.2.3