Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "GraphicExportFilter.hxx"
21 :
22 : #include <vcl/graphicfilter.hxx>
23 : #include <svl/outstrm.hxx>
24 : #include <svtools/DocumentToGraphicRenderer.hxx>
25 :
26 0 : GraphicExportFilter::GraphicExportFilter( const Reference<XComponentContext>& rxContext )
27 : : mxContext( rxContext )
28 : , mExportSelection(false)
29 : , mTargetWidth(0)
30 0 : , mTargetHeight(0)
31 0 : {}
32 :
33 0 : GraphicExportFilter::~GraphicExportFilter()
34 0 : {}
35 :
36 0 : void GraphicExportFilter::gatherProperties( const Sequence<PropertyValue>& rProperties )
37 : {
38 0 : OUString aInternalFilterName;
39 :
40 0 : for ( sal_Int32 i = 0; i < rProperties.getLength(); i++ )
41 : {
42 0 : PropertyValue aProperty = rProperties[i];
43 :
44 0 : if ( aProperty.Name == "FilterName" )
45 : {
46 0 : aProperty.Value >>= aInternalFilterName;
47 0 : aInternalFilterName = aInternalFilterName.replaceFirst( OUString( "draw_" ), OUString() );
48 0 : aInternalFilterName = aInternalFilterName.replaceFirst( OUString( "impress_" ), OUString() );
49 0 : aInternalFilterName = aInternalFilterName.replaceFirst( OUString( "calc_" ), OUString() );
50 0 : aInternalFilterName = aInternalFilterName.replaceFirst( OUString( "writer_" ), OUString() );
51 : }
52 0 : else if ( aProperty.Name == "FilterData" )
53 : {
54 0 : aProperty.Value >>= mFilterDataSequence;
55 : }
56 0 : else if ( aProperty.Name == "SelectionOnly" )
57 : {
58 0 : aProperty.Value >>= mExportSelection;
59 : }
60 0 : else if ( aProperty.Name == "URL" )
61 : {
62 0 : if( !( aProperty.Value >>= mTargetUrl ) )
63 : {
64 0 : aProperty.Value >>= mTargetUrl.Complete;
65 : }
66 : }
67 0 : else if ( aProperty.Name == "OutputStream" )
68 : {
69 0 : aProperty.Value >>= mxOutputStream;
70 : }
71 0 : }
72 :
73 0 : for ( sal_Int32 i = 0; i < mFilterDataSequence.getLength(); i++ )
74 : {
75 0 : if ( mFilterDataSequence[i].Name == "PixelWidth" )
76 : {
77 0 : mFilterDataSequence[i].Value >>= mTargetWidth;
78 : }
79 0 : else if ( mFilterDataSequence[i].Name == "PixelHeight" )
80 : {
81 0 : mFilterDataSequence[i].Value >>= mTargetHeight;
82 : }
83 : }
84 :
85 0 : if ( !aInternalFilterName.isEmpty() )
86 : {
87 0 : GraphicFilter aGraphicFilter( true );
88 :
89 0 : sal_uInt16 nFilterCount = aGraphicFilter.GetExportFormatCount();
90 : sal_uInt16 nFormat;
91 :
92 0 : for ( nFormat = 0; nFormat < nFilterCount; nFormat++ )
93 : {
94 0 : if ( aGraphicFilter.GetExportInternalFilterName( nFormat ) == aInternalFilterName )
95 0 : break;
96 : }
97 0 : if ( nFormat < nFilterCount )
98 : {
99 0 : mFilterExtension = aGraphicFilter.GetExportFormatShortName( nFormat );
100 0 : }
101 0 : }
102 0 : }
103 :
104 0 : sal_Bool SAL_CALL GraphicExportFilter::filter( const Sequence<PropertyValue>& rDescriptor )
105 : throw (RuntimeException, std::exception)
106 : {
107 0 : gatherProperties(rDescriptor);
108 :
109 0 : DocumentToGraphicRenderer aRenderer( mxDocument );
110 0 : sal_Int32 aCurrentPage = aRenderer.getCurrentPageWriter();
111 0 : Size aDocumentSizePixel = aRenderer.getDocumentSizeInPixels(aCurrentPage);
112 :
113 0 : Size aTargetSizePixel(mTargetWidth, mTargetHeight);
114 :
115 0 : Graphic aGraphic = aRenderer.renderToGraphic( aCurrentPage, aDocumentSizePixel, aTargetSizePixel );
116 :
117 0 : GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
118 :
119 0 : Sequence< PropertyValue > aFilterData( 3 );
120 0 : aFilterData[ 0 ].Name = "Interlaced";
121 0 : aFilterData[ 0 ].Value <<= (sal_Int32) 0;
122 0 : aFilterData[ 1 ].Name = "Compression";
123 0 : aFilterData[ 1 ].Value <<= (sal_Int32) 9;
124 0 : aFilterData[ 2 ].Name = "Quality";
125 0 : aFilterData[ 2 ].Value <<= (sal_Int32) 99;
126 :
127 0 : sal_uInt16 nFilterFormat = rFilter.GetExportFormatNumberForShortName( mFilterExtension );
128 :
129 0 : SvMemoryStream aMemStream;
130 0 : const GraphicConversionParameters aParameters(aTargetSizePixel, true, true);
131 :
132 0 : const sal_uInt16 nResult = rFilter.ExportGraphic( aGraphic.GetBitmapEx(aParameters), OUString(), aMemStream, nFilterFormat, &aFilterData );
133 :
134 0 : if ( nResult == GRFILTER_OK )
135 : {
136 0 : SvOutputStream aOutputStream( mxOutputStream );
137 0 : aMemStream.Seek(0);
138 0 : aOutputStream.WriteStream( aMemStream );
139 :
140 0 : return true;
141 : }
142 :
143 0 : return false;
144 : }
145 :
146 0 : void SAL_CALL GraphicExportFilter::cancel( ) throw (RuntimeException, std::exception)
147 : {
148 0 : }
149 :
150 0 : void SAL_CALL GraphicExportFilter::setSourceDocument( const Reference<XComponent>& xDocument )
151 : throw (IllegalArgumentException, RuntimeException, std::exception)
152 : {
153 0 : mxDocument = xDocument;
154 0 : }
155 :
156 0 : void SAL_CALL GraphicExportFilter::initialize( const Sequence<Any>& )
157 : throw (Exception, RuntimeException, std::exception)
158 : {
159 0 : }
160 :
161 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|