android: Update app-specific/MIME type icons
[LibreOffice.git] / filter / source / pdf / pdfdecomposer.cxx
blobc1a02c776262792cc4c3681780c73f0c29130ccf
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
8 */
10 #include <vector>
12 #include <basegfx/matrix/b2dhommatrixtools.hxx>
13 #include <cppuhelper/implbase2.hxx>
14 #include <cppuhelper/supportsservice.hxx>
15 #include <drawinglayer/primitive2d/bitmapprimitive2d.hxx>
16 #include <drawinglayer/primitive2d/Primitive2DContainer.hxx>
17 #include <vcl/bitmapex.hxx>
18 #include <vcl/pdfread.hxx>
19 #include <vcl/svapp.hxx>
20 #include <vcl/outdev.hxx>
21 #include <vcl/BinaryDataContainer.hxx>
22 #include <vcl/BinaryDataContainerTools.hxx>
23 #include <toolkit/helper/vclunohelper.hxx>
25 #include <com/sun/star/graphic/XPdfDecomposer.hpp>
26 #include <com/sun/star/lang/XServiceInfo.hpp>
27 #include <com/sun/star/uno/XComponentContext.hpp>
28 #include <com/sun/star/util/XBinaryDataContainer.hpp>
30 using namespace css;
32 namespace
34 /// Class to convert the PDF data into a XPrimitive2D (containing only a bitmap).
35 class XPdfDecomposer
36 : public ::cppu::WeakAggImplHelper2<graphic::XPdfDecomposer, lang::XServiceInfo>
38 public:
39 explicit XPdfDecomposer(uno::Reference<uno::XComponentContext> const& context);
40 XPdfDecomposer(const XPdfDecomposer&) = delete;
41 XPdfDecomposer& operator=(const XPdfDecomposer&) = delete;
43 // XPdfDecomposer
44 uno::Sequence<uno::Reference<graphic::XPrimitive2D>> SAL_CALL
45 getDecomposition(const uno::Reference<util::XBinaryDataContainer>& xDataContainer,
46 const uno::Sequence<beans::PropertyValue>& xDecompositionParameters) override;
48 // XServiceInfo
49 OUString SAL_CALL getImplementationName() override;
50 sal_Bool SAL_CALL supportsService(const OUString&) override;
51 uno::Sequence<OUString> SAL_CALL getSupportedServiceNames() override;
54 XPdfDecomposer::XPdfDecomposer(uno::Reference<uno::XComponentContext> const&) {}
56 uno::Sequence<uno::Reference<graphic::XPrimitive2D>> SAL_CALL
57 XPdfDecomposer::getDecomposition(const uno::Reference<util::XBinaryDataContainer>& xDataContainer,
58 const uno::Sequence<beans::PropertyValue>& xParameters)
60 sal_Int32 nPageIndex = -1;
62 for (const beans::PropertyValue& rProperty : xParameters)
64 if (rProperty.Name == "PageIndex")
66 rProperty.Value >>= nPageIndex;
67 break;
71 if (nPageIndex < 0)
72 nPageIndex = 0;
74 BinaryDataContainer aDataContainer = vcl::convertUnoBinaryDataContainer(xDataContainer);
76 std::vector<BitmapEx> aBitmaps;
77 int rv = vcl::RenderPDFBitmaps(aDataContainer.getData(), aDataContainer.getSize(), aBitmaps,
78 nPageIndex, 1);
79 if (rv == 0)
80 return {}; // happens if we do not have PDFium
82 BitmapEx aReplacement(aBitmaps[0]);
84 // short form for scale and translate transformation
85 const Size aBitmapSize(aReplacement.GetSizePixel());
86 // ImpGraphic::getPrefMapMode() requires mm100 for bitmaps rendered from vector graphic data.
87 const Size aMM100(
88 Application::GetDefaultDevice()->PixelToLogic(aBitmapSize, MapMode(MapUnit::Map100thMM)));
89 const basegfx::B2DHomMatrix aBitmapTransform(basegfx::utils::createScaleTranslateB2DHomMatrix(
90 aMM100.getWidth(), aMM100.getHeight(), 0, 0));
92 // create primitive
93 return drawinglayer::primitive2d::Primitive2DContainer{
94 new drawinglayer::primitive2d::BitmapPrimitive2D(aReplacement, aBitmapTransform)
96 .toSequence();
99 OUString SAL_CALL XPdfDecomposer::getImplementationName()
101 return "com.sun.star.comp.PDF.PDFDecomposer";
104 sal_Bool SAL_CALL XPdfDecomposer::supportsService(const OUString& rServiceName)
106 return cppu::supportsService(this, rServiceName);
109 uno::Sequence<OUString> SAL_CALL XPdfDecomposer::getSupportedServiceNames()
111 return { "com.sun.star.graphic.PdfTools" };
115 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
116 filter_PdfDecomposer_get_implementation(css::uno::XComponentContext* context,
117 css::uno::Sequence<css::uno::Any> const&)
119 return cppu::acquire(new XPdfDecomposer(context));
122 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */