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 <svtools/DocumentToGraphicRenderer.hxx>
21
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>
27
28 #include <tools/fract.hxx>
29
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>
41
42 #include <toolkit/helper/vclunohelper.hxx>
43
44 using namespace css;
45 using namespace css::uno;
46 using namespace css::lang;
47 using namespace css::beans;
48
DocumentToGraphicRenderer(const Reference<XComponent> & rxDocument,bool bSelectionOnly)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 )
56 {
57 try
58 {
59 uno::Reference< lang::XServiceInfo > xServiceInfo( mxDocument, uno::UNO_QUERY);
60 if (xServiceInfo.is())
61 {
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;
70 }
71 }
72 catch (const uno::Exception&)
73 {
74 }
75
76 if (!(bSelectionOnly && mxController.is()))
77 return;
78
79 try
80 {
81 uno::Reference< view::XSelectionSupplier > xSelSup( mxController, uno::UNO_QUERY);
82 if (xSelSup.is())
83 {
84 uno::Any aViewSelection( xSelSup->getSelection());
85 if (aViewSelection.hasValue())
86 {
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;
94 }
95 }
96 }
97 catch (const uno::Exception&)
98 {
99 }
100 }
101
~DocumentToGraphicRenderer()102 DocumentToGraphicRenderer::~DocumentToGraphicRenderer()
103 {
104 }
105
getDocumentSizeInPixels(sal_Int32 nCurrentPage)106 Size DocumentToGraphicRenderer::getDocumentSizeInPixels(sal_Int32 nCurrentPage)
107 {
108 Size aSize100mm = getDocumentSizeIn100mm(nCurrentPage);
109 return Application::GetDefaultDevice()->LogicToPixel(aSize100mm, MapMode(MapUnit::Map100thMM));
110 }
111
hasSelection() const112 bool DocumentToGraphicRenderer::hasSelection() const
113 {
114 return maSelection.hasValue();
115 }
116
getSelection() const117 uno::Any DocumentToGraphicRenderer::getSelection() const
118 {
119 uno::Any aSelection;
120 if (hasSelection())
121 aSelection = maSelection;
122 else
123 aSelection <<= mxDocument; // default: render whole document
124 return aSelection;
125 }
126
getDocumentSizeIn100mm(sal_Int32 nCurrentPage,Point * pDocumentPosition,Point * pCalcPagePosition,Size * pCalcPageSize)127 Size DocumentToGraphicRenderer::getDocumentSizeIn100mm(sal_Int32 nCurrentPage,
128 Point* pDocumentPosition, Point* pCalcPagePosition, Size* pCalcPageSize)
129 {
130 Reference< awt::XDevice > xDevice(mxToolkit->createScreenCompatibleDevice( 32, 32 ) );
131
132 uno::Any selection( getSelection());
133
134 PropertyValues renderProperties;
135
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;
145
146 awt::Size aSize;
147 awt::Size aCalcPageSize;
148 awt::Point aPos;
149 awt::Point aCalcPos;
150
151 sal_Int32 nPages = mxRenderable->getRendererCount( selection, renderProperties );
152 if (nPages >= nCurrentPage)
153 {
154 const Sequence< beans::PropertyValue > aResult = mxRenderable->getRenderer(nCurrentPage - 1, selection, renderProperties );
155 for( const auto& rProperty : aResult )
156 {
157 if ( rProperty.Name == "PageSize" )
158 {
159 rProperty.Value >>= aSize;
160 }
161 else if (rProperty.Name == "PagePos")
162 {
163 rProperty.Value >>= aPos;
164 }
165 else if (rProperty.Name == "CalcPagePos")
166 {
167 rProperty.Value >>= aCalcPos;
168 }
169 else if (rProperty.Name == "CalcPageContentSize")
170 {
171 rProperty.Value >>= aCalcPageSize;
172 }
173 }
174 }
175
176 if (pDocumentPosition)
177 {
178 *pDocumentPosition = Point(aPos.X, aPos.Y);
179 }
180 if (pCalcPagePosition)
181 {
182 *pCalcPagePosition = Point(aCalcPos.X, aCalcPos.Y);
183 }
184 if (pCalcPageSize)
185 {
186 *pCalcPageSize = Size(aCalcPageSize.Width, aCalcPageSize.Height);
187 }
188
189 return Size( aSize.Width, aSize.Height );
190 }
191
renderToGraphic(sal_Int32 nCurrentPage,Size aDocumentSizePixel,Size aTargetSizePixel,Color aPageColor,bool bExtOutDevData)192 Graphic DocumentToGraphicRenderer::renderToGraphic(
193 sal_Int32 nCurrentPage,
194 Size aDocumentSizePixel,
195 Size aTargetSizePixel,
196 Color aPageColor,
197 bool bExtOutDevData)
198
199 {
200 if (!mxModel.is() || !mxController.is() || !mxRenderable.is())
201 return Graphic();
202
203 Reference< awt::XDevice > xDevice(mxToolkit->createScreenCompatibleDevice( aTargetSizePixel.Width(), aTargetSizePixel.Height() ) );
204 if (!xDevice.is())
205 return Graphic();
206
207 assert( !aDocumentSizePixel.IsEmpty() && !aTargetSizePixel.IsEmpty());
208
209 double fScaleX = aTargetSizePixel.Width() / static_cast<double>(aDocumentSizePixel.Width());
210 double fScaleY = aTargetSizePixel.Height() / static_cast<double>(aDocumentSizePixel.Height());
211
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);
226
227 GDIMetaFile aMtf;
228
229 OutputDevice* pOutputDev = VCLUnoHelper::GetOutputDevice( xDevice );
230
231 vcl::PDFExtOutDevData aPDFExtOutDevData(*pOutputDev);
232 if (bExtOutDevData)
233 {
234 aPDFExtOutDevData.SetIsExportBookmarks(true);
235 pOutputDev->SetExtOutDevData(&aPDFExtOutDevData);
236 }
237
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 );
243
244 aMtf.Record( pOutputDev );
245
246 if (aPageColor != COL_TRANSPARENT)
247 {
248 pOutputDev->SetBackground(Wallpaper(aPageColor));
249 pOutputDev->Erase();
250 }
251
252 uno::Any aSelection( getSelection());
253 mxRenderable->render(nCurrentPage - 1, aSelection, renderProps );
254
255 aMtf.Stop();
256 aMtf.WindStart();
257 aMtf.SetPrefSize( aTargetSizePixel );
258
259 if (bExtOutDevData)
260 maChapterNames = aPDFExtOutDevData.GetChapterNames();
261
262 return Graphic(aMtf);
263 }
264
getChapterNames() const265 const std::vector<OUString>& DocumentToGraphicRenderer::getChapterNames() const
266 {
267 return maChapterNames;
268 }
269
getCurrentPage()270 sal_Int32 DocumentToGraphicRenderer::getCurrentPage()
271 {
272 if (hasSelection())
273 return 1;
274
275 if (isWriter())
276 return getCurrentPageWriter();
277
278 /* TODO: other application specific page detection? */
279 return 1;
280 }
281
getPageCount()282 sal_Int32 DocumentToGraphicRenderer::getPageCount()
283 {
284 Reference< awt::XDevice > xDevice(mxToolkit->createScreenCompatibleDevice( 32, 32 ) );
285
286 uno::Any selection( getSelection() );
287
288 PropertyValues renderProperties;
289
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;
299
300 sal_Int32 nPages = mxRenderable->getRendererCount( selection, renderProperties );
301
302 return nPages;
303 }
304
getCurrentPageWriter()305 sal_Int32 DocumentToGraphicRenderer::getCurrentPageWriter()
306 {
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;
312 }
313
314 // static
isShapeSelected(css::uno::Reference<css::drawing::XShapes> & rxShapes,css::uno::Reference<css::drawing::XShape> & rxShape,const css::uno::Reference<css::frame::XController> & rxController)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 )
319 {
320 bool bShape = false;
321 if (rxController.is())
322 {
323 uno::Reference< view::XSelectionSupplier > xSelectionSupplier( rxController, uno::UNO_QUERY);
324 if (xSelectionSupplier.is())
325 {
326 uno::Any aAny( xSelectionSupplier->getSelection());
327 if (aAny >>= rxShapes)
328 bShape = true;
329 else if (aAny >>= rxShape)
330 bShape = true;
331 }
332 }
333 return bShape;
334 }
335
isWriter() const336 bool DocumentToGraphicRenderer::isWriter() const
337 {
338 if (meDocType == WRITER)
339 return true;
340 else
341 return false;
342 }
343
isCalc() const344 bool DocumentToGraphicRenderer::isCalc() const
345 {
346 if (meDocType == CALC)
347 return true;
348 else
349 return false;
350 }
351
isImpress() const352 bool DocumentToGraphicRenderer::isImpress() const
353 {
354 if (meDocType == IMPRESS)
355 return true;
356 else
357 return false;
358 }
359
360 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
361