nss: upgrade to release 3.73
[LibreOffice.git] / svtools / source / filter / DocumentToGraphicRenderer.cxx
blob29ec30b9480894722b0ac3ed60c06f40fc634562
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 .
20 #include <svtools/DocumentToGraphicRenderer.hxx>
22 #include <vcl/gdimtf.hxx>
23 #include <vcl/graphicfilter.hxx>
24 #include <vcl/svapp.hxx>
25 #include <vcl/outdev.hxx>
26 #include <vcl/pdfextoutdevdata.hxx>
28 #include <tools/fract.hxx>
30 #include <com/sun/star/awt/XDevice.hpp>
31 #include <com/sun/star/awt/XToolkit.hpp>
32 #include <com/sun/star/text/XPageCursor.hpp>
33 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
34 #include <com/sun/star/view/XRenderable.hpp>
35 #include <com/sun/star/view/XSelectionSupplier.hpp>
36 #include <com/sun/star/beans/PropertyValues.hpp>
37 #include <com/sun/star/lang/XServiceInfo.hpp>
38 #include <com/sun/star/drawing/XShapes.hpp>
39 #include <com/sun/star/drawing/XShape.hpp>
40 #include <com/sun/star/frame/XModel.hpp>
42 #include <toolkit/helper/vclunohelper.hxx>
44 using namespace css;
45 using namespace css::uno;
46 using namespace css::lang;
47 using namespace css::beans;
49 DocumentToGraphicRenderer::DocumentToGraphicRenderer( const Reference<XComponent>& rxDocument, bool bSelectionOnly ) :
50 mxDocument(rxDocument),
51 mxModel( mxDocument, uno::UNO_QUERY ),
52 mxController( mxModel->getCurrentController() ),
53 mxRenderable (mxDocument, uno::UNO_QUERY ),
54 mxToolkit( VCLUnoHelper::CreateToolkit() ),
55 meDocType( UNKNOWN )
57 try
59 uno::Reference< lang::XServiceInfo > xServiceInfo( mxDocument, uno::UNO_QUERY);
60 if (xServiceInfo.is())
62 if (xServiceInfo->supportsService("com.sun.star.text.TextDocument"))
63 meDocType = WRITER;
64 else if (xServiceInfo->supportsService("com.sun.star.sheet.SpreadsheetDocument"))
65 meDocType = CALC;
66 else if (xServiceInfo->supportsService("com.sun.star.presentation.PresentationDocument"))
67 meDocType = IMPRESS;
68 else
69 meDocType = UNKNOWN;
72 catch (const uno::Exception&)
76 if (!(bSelectionOnly && mxController.is()))
77 return;
79 try
81 uno::Reference< view::XSelectionSupplier > xSelSup( mxController, uno::UNO_QUERY);
82 if (xSelSup.is())
84 uno::Any aViewSelection( xSelSup->getSelection());
85 if (aViewSelection.hasValue())
87 /* FIXME: Writer always has a selection even if nothing is
88 * selected, but passing a selection to
89 * XRenderable::render() it always renders an empty page.
90 * So disable the selection already here. The current page
91 * the cursor is on is rendered. */
92 if (!isWriter())
93 maSelection = aViewSelection;
97 catch (const uno::Exception&)
102 DocumentToGraphicRenderer::~DocumentToGraphicRenderer()
106 Size DocumentToGraphicRenderer::getDocumentSizeInPixels(sal_Int32 nCurrentPage)
108 Size aSize100mm = getDocumentSizeIn100mm(nCurrentPage);
109 return Application::GetDefaultDevice()->LogicToPixel(aSize100mm, MapMode(MapUnit::Map100thMM));
112 bool DocumentToGraphicRenderer::hasSelection() const
114 return maSelection.hasValue();
117 uno::Any DocumentToGraphicRenderer::getSelection() const
119 uno::Any aSelection;
120 if (hasSelection())
121 aSelection = maSelection;
122 else
123 aSelection <<= mxDocument; // default: render whole document
124 return aSelection;
127 Size DocumentToGraphicRenderer::getDocumentSizeIn100mm(sal_Int32 nCurrentPage,
128 Point* pDocumentPosition, Point* pCalcPagePosition, Size* pCalcPageSize)
130 Reference< awt::XDevice > xDevice(mxToolkit->createScreenCompatibleDevice( 32, 32 ) );
132 uno::Any selection( getSelection());
134 PropertyValues renderProperties;
136 renderProperties.realloc( 4 );
137 renderProperties[0].Name = "IsPrinter";
138 renderProperties[0].Value <<= true;
139 renderProperties[1].Name = "RenderDevice";
140 renderProperties[1].Value <<= xDevice;
141 renderProperties[2].Name = "View";
142 renderProperties[2].Value <<= mxController;
143 renderProperties[3].Name = "RenderToGraphic";
144 renderProperties[3].Value <<= true;
146 awt::Size aSize;
147 awt::Size aCalcPageSize;
148 awt::Point aPos;
149 awt::Point aCalcPos;
151 sal_Int32 nPages = mxRenderable->getRendererCount( selection, renderProperties );
152 if (nPages >= nCurrentPage)
154 const Sequence< beans::PropertyValue > aResult = mxRenderable->getRenderer(nCurrentPage - 1, selection, renderProperties );
155 for( const auto& rProperty : aResult )
157 if ( rProperty.Name == "PageSize" )
159 rProperty.Value >>= aSize;
161 else if (rProperty.Name == "PagePos")
163 rProperty.Value >>= aPos;
165 else if (rProperty.Name == "CalcPagePos")
167 rProperty.Value >>= aCalcPos;
169 else if (rProperty.Name == "CalcPageContentSize")
171 rProperty.Value >>= aCalcPageSize;
176 if (pDocumentPosition)
178 *pDocumentPosition = Point(aPos.X, aPos.Y);
180 if (pCalcPagePosition)
182 *pCalcPagePosition = Point(aCalcPos.X, aCalcPos.Y);
184 if (pCalcPageSize)
186 *pCalcPageSize = Size(aCalcPageSize.Width, aCalcPageSize.Height);
189 return Size( aSize.Width, aSize.Height );
192 Graphic DocumentToGraphicRenderer::renderToGraphic(
193 sal_Int32 nCurrentPage,
194 Size aDocumentSizePixel,
195 Size aTargetSizePixel,
196 Color aPageColor,
197 bool bExtOutDevData)
200 if (!mxModel.is() || !mxController.is() || !mxRenderable.is())
201 return Graphic();
203 Reference< awt::XDevice > xDevice(mxToolkit->createScreenCompatibleDevice( aTargetSizePixel.Width(), aTargetSizePixel.Height() ) );
204 if (!xDevice.is())
205 return Graphic();
207 assert( !aDocumentSizePixel.IsEmpty() && !aTargetSizePixel.IsEmpty());
209 double fScaleX = aTargetSizePixel.Width() / static_cast<double>(aDocumentSizePixel.Width());
210 double fScaleY = aTargetSizePixel.Height() / static_cast<double>(aDocumentSizePixel.Height());
212 PropertyValues renderProps;
213 renderProps.realloc( 6 );
214 renderProps[0].Name = "IsPrinter";
215 renderProps[0].Value <<= true;
216 renderProps[1].Name = "RenderDevice";
217 renderProps[1].Value <<= xDevice;
218 renderProps[2].Name = "View";
219 renderProps[2].Value <<= mxController;
220 renderProps[3].Name = "RenderToGraphic";
221 renderProps[3].Value <<= true;
222 renderProps[4].Name = "HasPDFExtOutDevData";
223 renderProps[4].Value <<= bExtOutDevData;
224 renderProps[5].Name = "PageRange";
225 renderProps[5].Value <<= OUString::number(nCurrentPage);
227 GDIMetaFile aMtf;
229 OutputDevice* pOutputDev = VCLUnoHelper::GetOutputDevice( xDevice );
231 vcl::PDFExtOutDevData aPDFExtOutDevData(*pOutputDev);
232 if (bExtOutDevData)
234 aPDFExtOutDevData.SetIsExportBookmarks(true);
235 pOutputDev->SetExtOutDevData(&aPDFExtOutDevData);
238 pOutputDev->SetAntialiasing(pOutputDev->GetAntialiasing() | AntialiasingFlags::Enable);
239 MapMode mm = pOutputDev->GetMapMode();
240 mm.SetScaleX( Fraction(fScaleX) );
241 mm.SetScaleY( Fraction(fScaleY) );
242 pOutputDev->SetMapMode( mm );
244 aMtf.Record( pOutputDev );
246 if (aPageColor != COL_TRANSPARENT)
248 pOutputDev->SetBackground(Wallpaper(aPageColor));
249 pOutputDev->Erase();
252 uno::Any aSelection( getSelection());
253 mxRenderable->render(nCurrentPage - 1, aSelection, renderProps );
255 aMtf.Stop();
256 aMtf.WindStart();
257 aMtf.SetPrefSize( aTargetSizePixel );
259 if (bExtOutDevData)
260 maChapterNames = aPDFExtOutDevData.GetChapterNames();
262 return Graphic(aMtf);
265 const std::vector<OUString>& DocumentToGraphicRenderer::getChapterNames() const
267 return maChapterNames;
270 sal_Int32 DocumentToGraphicRenderer::getCurrentPage()
272 if (hasSelection())
273 return 1;
275 if (isWriter())
276 return getCurrentPageWriter();
278 /* TODO: other application specific page detection? */
279 return 1;
282 sal_Int32 DocumentToGraphicRenderer::getPageCount()
284 Reference< awt::XDevice > xDevice(mxToolkit->createScreenCompatibleDevice( 32, 32 ) );
286 uno::Any selection( getSelection() );
288 PropertyValues renderProperties;
290 renderProperties.realloc( 4 );
291 renderProperties[0].Name = "IsPrinter";
292 renderProperties[0].Value <<= true;
293 renderProperties[1].Name = "RenderDevice";
294 renderProperties[1].Value <<= xDevice;
295 renderProperties[2].Name = "View";
296 renderProperties[2].Value <<= mxController;
297 renderProperties[3].Name = "RenderToGraphic";
298 renderProperties[3].Value <<= true;
300 sal_Int32 nPages = mxRenderable->getRendererCount( selection, renderProperties );
302 return nPages;
305 sal_Int32 DocumentToGraphicRenderer::getCurrentPageWriter()
307 Reference<text::XTextViewCursorSupplier> xTextViewCursorSupplier(mxModel->getCurrentController(), UNO_QUERY);
308 if (!xTextViewCursorSupplier.is())
309 return 1;
310 Reference<text::XPageCursor> xCursor(xTextViewCursorSupplier->getViewCursor(), UNO_QUERY);
311 return xCursor.is() ? xCursor->getPage() : 1;
314 // static
315 bool DocumentToGraphicRenderer::isShapeSelected(
316 css::uno::Reference< css::drawing::XShapes > & rxShapes,
317 css::uno::Reference< css::drawing::XShape > & rxShape,
318 const css::uno::Reference< css::frame::XController > & rxController )
320 bool bShape = false;
321 if (rxController.is())
323 uno::Reference< view::XSelectionSupplier > xSelectionSupplier( rxController, uno::UNO_QUERY);
324 if (xSelectionSupplier.is())
326 uno::Any aAny( xSelectionSupplier->getSelection());
327 if (aAny >>= rxShapes)
328 bShape = true;
329 else if (aAny >>= rxShape)
330 bShape = true;
333 return bShape;
336 bool DocumentToGraphicRenderer::isWriter() const
338 if (meDocType == WRITER)
339 return true;
340 else
341 return false;
344 bool DocumentToGraphicRenderer::isCalc() const
346 if (meDocType == CALC)
347 return true;
348 else
349 return false;
352 bool DocumentToGraphicRenderer::isImpress() const
354 if (meDocType == IMPRESS)
355 return true;
356 else
357 return false;
360 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */