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