LibreOffice Module svtools (master) 1
DocumentToGraphicRenderer.cxx
Go to the documentation of this file.
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
21
23#include <vcl/gdimtf.hxx>
24#include <vcl/svapp.hxx>
25#include <vcl/outdev.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
43
44using namespace css;
45using namespace css::uno;
46using namespace css::lang;
47using namespace css::beans;
48
49DocumentToGraphicRenderer::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"))
64 else if (xServiceInfo->supportsService("com.sun.star.sheet.SpreadsheetDocument"))
66 else if (xServiceInfo->supportsService("com.sun.star.presentation.PresentationDocument"))
68 else
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
103{
104}
105
107{
108 Size aSize100mm = getDocumentSizeIn100mm(nCurrentPage);
109 return Application::GetDefaultDevice()->LogicToPixel(aSize100mm, MapMode(MapUnit::Map100thMM));
110}
111
113{
114 return maSelection.hasValue();
115}
116
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
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{ comphelper::makePropertyValue("IsPrinter", true),
135 comphelper::makePropertyValue("RenderDevice", xDevice),
137 comphelper::makePropertyValue("RenderToGraphic", true) };
138
139 awt::Size aSize;
140 awt::Size aCalcPageSize;
141 awt::Point aPos;
142 awt::Point aCalcPos;
143
144 sal_Int32 nPages = mxRenderable->getRendererCount( selection, renderProperties );
145 if (nPages >= nCurrentPage)
146 {
147 const Sequence< beans::PropertyValue > aResult = mxRenderable->getRenderer(nCurrentPage - 1, selection, renderProperties );
148 for( const auto& rProperty : aResult )
149 {
150 if ( rProperty.Name == "PageSize" )
151 {
152 rProperty.Value >>= aSize;
153 }
154 else if (rProperty.Name == "PagePos")
155 {
156 rProperty.Value >>= aPos;
157 }
158 else if (rProperty.Name == "CalcPagePos")
159 {
160 rProperty.Value >>= aCalcPos;
161 }
162 else if (rProperty.Name == "CalcPageContentSize")
163 {
164 rProperty.Value >>= aCalcPageSize;
165 }
166 }
167 }
168
169 if (pDocumentPosition)
170 {
171 *pDocumentPosition = Point(aPos.X, aPos.Y);
172 }
173 if (pCalcPagePosition)
174 {
175 *pCalcPagePosition = Point(aCalcPos.X, aCalcPos.Y);
176 }
177 if (pCalcPageSize)
178 {
179 *pCalcPageSize = Size(aCalcPageSize.Width, aCalcPageSize.Height);
180 }
181
182 return Size( aSize.Width, aSize.Height );
183}
184
186 sal_Int32 nCurrentPage,
187 Size aDocumentSizePixel,
188 Size aTargetSizePixel,
189 Color aPageColor,
190 bool bExtOutDevData)
191
192{
193 if (!mxModel.is() || !mxController.is() || !mxRenderable.is())
194 return Graphic();
195
196 Reference< awt::XDevice > xDevice(mxToolkit->createScreenCompatibleDevice( aTargetSizePixel.Width(), aTargetSizePixel.Height() ) );
197 if (!xDevice.is())
198 return Graphic();
199
200 assert( !aDocumentSizePixel.IsEmpty() && !aTargetSizePixel.IsEmpty());
201
202 double fScaleX = aTargetSizePixel.Width() / static_cast<double>(aDocumentSizePixel.Width());
203 double fScaleY = aTargetSizePixel.Height() / static_cast<double>(aDocumentSizePixel.Height());
204
205 PropertyValues renderProps{
206 comphelper::makePropertyValue("IsPrinter", true),
207 comphelper::makePropertyValue("RenderDevice", xDevice),
209 comphelper::makePropertyValue("RenderToGraphic", true),
210 comphelper::makePropertyValue("HasPDFExtOutDevData", bExtOutDevData),
211 comphelper::makePropertyValue("PageRange", OUString::number(nCurrentPage))
212 };
213
214 GDIMetaFile aMtf;
215
216 OutputDevice* pOutputDev = VCLUnoHelper::GetOutputDevice( xDevice );
217
218 vcl::PDFExtOutDevData aPDFExtOutDevData(*pOutputDev);
219 if (bExtOutDevData)
220 {
221 aPDFExtOutDevData.SetIsExportBookmarks(true);
222 pOutputDev->SetExtOutDevData(&aPDFExtOutDevData);
223 }
224
225 pOutputDev->SetAntialiasing(pOutputDev->GetAntialiasing() | AntialiasingFlags::Enable);
226 MapMode mm = pOutputDev->GetMapMode();
227 mm.SetScaleX( Fraction(fScaleX) );
228 mm.SetScaleY( Fraction(fScaleY) );
229 pOutputDev->SetMapMode( mm );
230
231 aMtf.Record( pOutputDev );
232
233 if (aPageColor != COL_TRANSPARENT)
234 {
235 pOutputDev->SetBackground(Wallpaper(aPageColor));
236 pOutputDev->Erase();
237 }
238
239 uno::Any aSelection( getSelection());
240 mxRenderable->render(nCurrentPage - 1, aSelection, renderProps );
241
242 aMtf.Stop();
243 aMtf.WindStart();
244 aMtf.SetPrefSize( aTargetSizePixel );
245
246 if (bExtOutDevData)
247 maChapterNames = aPDFExtOutDevData.GetChapterNames();
248
249 return Graphic(aMtf);
250}
251
252const std::vector<OUString>& DocumentToGraphicRenderer::getChapterNames() const
253{
254 return maChapterNames;
255}
256
258{
259 if (hasSelection())
260 return 1;
261
262 if (isWriter())
263 return getCurrentPageWriter();
264
265 /* TODO: other application specific page detection? */
266 return 1;
267}
268
270{
271 Reference< awt::XDevice > xDevice(mxToolkit->createScreenCompatibleDevice( 32, 32 ) );
272
273 uno::Any selection( getSelection() );
274
275 PropertyValues renderProperties{ comphelper::makePropertyValue("IsPrinter", true),
276 comphelper::makePropertyValue("RenderDevice", xDevice),
278 comphelper::makePropertyValue("RenderToGraphic", true) };
279
280 sal_Int32 nPages = mxRenderable->getRendererCount( selection, renderProperties );
281
282 return nPages;
283}
284
286{
287 Reference<text::XTextViewCursorSupplier> xTextViewCursorSupplier(mxModel->getCurrentController(), UNO_QUERY);
288 if (!xTextViewCursorSupplier.is())
289 return 1;
290 Reference<text::XPageCursor> xCursor(xTextViewCursorSupplier->getViewCursor(), UNO_QUERY);
291 return xCursor.is() ? xCursor->getPage() : 1;
292}
293
294// static
296 css::uno::Reference< css::drawing::XShapes > & rxShapes,
297 css::uno::Reference< css::drawing::XShape > & rxShape,
298 const css::uno::Reference< css::frame::XController > & rxController )
299{
300 bool bShape = false;
301 if (rxController.is())
302 {
303 uno::Reference< view::XSelectionSupplier > xSelectionSupplier( rxController, uno::UNO_QUERY);
304 if (xSelectionSupplier.is())
305 {
306 uno::Any aAny( xSelectionSupplier->getSelection());
307 if (aAny >>= rxShapes)
308 bShape = true;
309 else if (aAny >>= rxShape)
310 bShape = true;
311 }
312 }
313 return bShape;
314}
315
317{
318 if (meDocType == WRITER)
319 return true;
320 else
321 return false;
322}
323
325{
326 if (meDocType == CALC)
327 return true;
328 else
329 return false;
330}
331
333{
334 if (meDocType == IMPRESS)
335 return true;
336 else
337 return false;
338}
339
340/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::frame::XModel2 > mxModel
static OutputDevice * GetDefaultDevice()
const css::uno::Reference< css::lang::XComponent > & mxDocument
Size getDocumentSizeIn100mm(sal_Int32 nCurrentPage, Point *pDocumentPosition=nullptr, Point *pCalcPagePosition=nullptr, Size *pCalcPageSize=nullptr)
std::vector< OUString > maChapterNames
const std::vector< OUString > & getChapterNames() const
Get list of chapter names for a page, current page is set by renderToGraphic().
css::uno::Reference< css::awt::XToolkit > mxToolkit
css::uno::Reference< css::view::XRenderable > mxRenderable
Graphic renderToGraphic(sal_Int32 nCurrentPage, Size aDocumentSizePixel, Size aTargetSizePixel, Color aPageColor, bool bExtOutDevData)
Size getDocumentSizeInPixels(sal_Int32 nCurrentPage)
css::uno::Reference< css::frame::XModel > mxModel
css::uno::Reference< css::frame::XController > mxController
static bool isShapeSelected(css::uno::Reference< css::drawing::XShapes > &rxShapes, css::uno::Reference< css::drawing::XShape > &rxShape, const css::uno::Reference< css::frame::XController > &rxController)
Determine whether rxController has a css::view::XSelectionSupplier at which either a css::drawing::XS...
css::uno::Any getSelection() const
Always something even if hasSelection() is false (in which case the selection is mxDocument).
DocumentToGraphicRenderer(const css::uno::Reference< css::lang::XComponent > &xDocument, bool bSelectionOnly)
void WindStart()
void Record(OutputDevice *pOutDev)
void SetPrefSize(const Size &rSize)
void SetAntialiasing(AntialiasingFlags nMode)
void SetMapMode()
void SetExtOutDevData(vcl::ExtOutDevData *pExtOutDevData)
SAL_WARN_UNUSED_RESULT Point LogicToPixel(const Point &rLogicPt) const
const MapMode & GetMapMode() const
void SetBackground()
AntialiasingFlags GetAntialiasing() const
bool IsEmpty() const
constexpr tools::Long Height() const
constexpr tools::Long Width() const
static OutputDevice * GetOutputDevice(const css::uno::Reference< css::awt::XDevice > &rxDevice)
void SetIsExportBookmarks(const bool bExportBookmarks)
const std::vector< OUString > & GetChapterNames() const
constexpr ::Color COL_TRANSPARENT(ColorTransparency, 0xFF, 0xFF, 0xFF, 0xFF)
UNKNOWN
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
std::map< sal_Int32, std::shared_ptr< SetItemPropertyStorage > > PropertyValues
bool hasValue()