Get the style color and number just once
[LibreOffice.git] / svtools / source / misc / imageresourceaccess.cxx
blobeef81ec526884bf4bbb7a6ccc906e5580a1e2e46
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 .
21 #include <svtools/imageresourceaccess.hxx>
23 #include <com/sun/star/io/NotConnectedException.hpp>
24 #include <com/sun/star/io/XSeekable.hpp>
25 #include <com/sun/star/graphic/GraphicProvider.hpp>
26 #include <com/sun/star/graphic/XGraphicProvider.hpp>
27 #include <com/sun/star/io/XStream.hpp>
29 #include <comphelper/propertyvalue.hxx>
30 #include <o3tl/string_view.hxx>
31 #include <osl/diagnose.h>
32 #include <tools/stream.hxx>
33 #include <comphelper/diagnose_ex.hxx>
34 #include <unotools/streamwrap.hxx>
35 #include <cppuhelper/implbase.hxx>
36 #include <utility>
38 namespace svt::GraphicAccess
41 using namespace ::utl;
42 using namespace css;
44 typedef ::cppu::WeakImplHelper<io::XStream, io::XSeekable> StreamSupplier_Base;
46 namespace {
48 class StreamSupplier : public StreamSupplier_Base
50 private:
51 uno::Reference<io::XInputStream> m_xInput;
52 uno::Reference<io::XOutputStream> m_xOutput;
53 uno::Reference<io::XSeekable> m_xSeekable;
55 public:
56 StreamSupplier(uno::Reference<io::XInputStream> xInput, uno::Reference<io::XOutputStream> xOutput);
58 protected:
59 // XStream
60 virtual uno::Reference<io::XInputStream> SAL_CALL getInputStream() override;
61 virtual uno::Reference<io::XOutputStream> SAL_CALL getOutputStream() override;
63 // XSeekable
64 virtual void SAL_CALL seek(sal_Int64 location) override;
65 virtual sal_Int64 SAL_CALL getPosition() override;
66 virtual sal_Int64 SAL_CALL getLength() override;
71 StreamSupplier::StreamSupplier(uno::Reference<io::XInputStream> xInput, uno::Reference<io::XOutputStream> xOutput)
72 : m_xInput(std::move(xInput))
73 , m_xOutput(std::move(xOutput))
75 m_xSeekable.set(m_xInput, uno::UNO_QUERY);
76 if (!m_xSeekable.is())
77 m_xSeekable.set(m_xOutput, uno::UNO_QUERY);
78 OSL_ENSURE(m_xSeekable.is(), "StreamSupplier::StreamSupplier: at least one of both must be seekable!");
81 uno::Reference<io::XInputStream> SAL_CALL StreamSupplier::getInputStream()
83 return m_xInput;
86 uno::Reference<io::XOutputStream> SAL_CALL StreamSupplier::getOutputStream()
88 return m_xOutput;
91 void SAL_CALL StreamSupplier::seek(sal_Int64 nLocation)
93 if (!m_xSeekable.is())
94 throw io::NotConnectedException();
95 m_xSeekable->seek(nLocation);
98 sal_Int64 SAL_CALL StreamSupplier::getPosition()
100 if (!m_xSeekable.is())
101 throw io::NotConnectedException();
102 return m_xSeekable->getPosition();
105 sal_Int64 SAL_CALL StreamSupplier::getLength()
107 if (!m_xSeekable.is())
108 throw io::NotConnectedException();
110 return m_xSeekable->getLength();
113 bool isSupportedURL(std::u16string_view rURL)
115 return o3tl::starts_with(rURL, u"private:resource/")
116 || o3tl::starts_with(rURL, u"private:graphicrepository/")
117 || o3tl::starts_with(rURL, u"private:standardimage/")
118 || o3tl::starts_with(rURL, u"vnd.sun.star.extension://");
121 std::unique_ptr<SvStream> getImageStream(uno::Reference<uno::XComponentContext> const & rxContext, OUString const & rImageResourceURL)
123 std::unique_ptr<SvMemoryStream> pMemBuffer;
127 // get a GraphicProvider
128 uno::Reference<graphic::XGraphicProvider> xProvider = css::graphic::GraphicProvider::create(rxContext);
130 // let it create a graphic from the given URL
131 uno::Sequence<beans::PropertyValue> aMediaProperties{ comphelper::makePropertyValue(
132 u"URL"_ustr, rImageResourceURL) };
133 uno::Reference<graphic::XGraphic> xGraphic(xProvider->queryGraphic(aMediaProperties));
135 OSL_ENSURE(xGraphic.is(), "GraphicAccess::getImageStream: the provider did not give us a graphic object!");
136 if (!xGraphic.is())
137 return pMemBuffer;
139 // copy the graphic to an in-memory buffer
140 pMemBuffer.reset(new SvMemoryStream);
141 uno::Reference<io::XStream> xBufferAccess = new StreamSupplier(
142 new OSeekableInputStreamWrapper(*pMemBuffer),
143 new OSeekableOutputStreamWrapper(*pMemBuffer));
145 aMediaProperties = { comphelper::makePropertyValue(u"OutputStream"_ustr, xBufferAccess),
146 comphelper::makePropertyValue(u"MimeType"_ustr, u"image/png"_ustr) };
147 xProvider->storeGraphic(xGraphic, aMediaProperties);
149 pMemBuffer->Seek(0);
151 catch (const uno::Exception&)
153 TOOLS_WARN_EXCEPTION("svtools", "GraphicAccess::getImageStream");
154 pMemBuffer.reset();
157 return pMemBuffer;
160 uno::Reference<io::XInputStream> getImageXStream(uno::Reference<uno::XComponentContext> const & rxContext, OUString const & rImageResourceURL)
162 return new OSeekableInputStreamWrapper(getImageStream(rxContext, rImageResourceURL).release(), true); // take ownership
165 } // namespace svt::GraphicAccess
167 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */