1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <comphelper/propertyvalue.hxx>
23 #include <vcl/gdimtf.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>
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() ),
59 uno::Reference
< lang::XServiceInfo
> xServiceInfo( mxDocument
, uno::UNO_QUERY
);
60 if (xServiceInfo
.is())
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"))
72 catch (const uno::Exception
&)
76 if (!(bSelectionOnly
&& mxController
.is()))
81 uno::Reference
< view::XSelectionSupplier
> xSelSup( mxController
, uno::UNO_QUERY
);
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. */
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
121 aSelection
= maSelection
;
123 aSelection
<<= mxDocument
; // default: render whole document
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
{ comphelper::makePropertyValue("IsPrinter", true),
135 comphelper::makePropertyValue("RenderDevice", xDevice
),
136 comphelper::makePropertyValue("View", mxController
),
137 comphelper::makePropertyValue("RenderToGraphic", true) };
140 awt::Size aCalcPageSize
;
144 sal_Int32 nPages
= mxRenderable
->getRendererCount( selection
, renderProperties
);
145 if (nPages
>= nCurrentPage
)
147 const Sequence
< beans::PropertyValue
> aResult
= mxRenderable
->getRenderer(nCurrentPage
- 1, selection
, renderProperties
);
148 for( const auto& rProperty
: aResult
)
150 if ( rProperty
.Name
== "PageSize" )
152 rProperty
.Value
>>= aSize
;
154 else if (rProperty
.Name
== "PagePos")
156 rProperty
.Value
>>= aPos
;
158 else if (rProperty
.Name
== "CalcPagePos")
160 rProperty
.Value
>>= aCalcPos
;
162 else if (rProperty
.Name
== "CalcPageContentSize")
164 rProperty
.Value
>>= aCalcPageSize
;
169 if (pDocumentPosition
)
171 *pDocumentPosition
= Point(aPos
.X
, aPos
.Y
);
173 if (pCalcPagePosition
)
175 *pCalcPagePosition
= Point(aCalcPos
.X
, aCalcPos
.Y
);
179 *pCalcPageSize
= Size(aCalcPageSize
.Width
, aCalcPageSize
.Height
);
182 return Size( aSize
.Width
, aSize
.Height
);
185 Graphic
DocumentToGraphicRenderer::renderToGraphic(
186 sal_Int32 nCurrentPage
,
187 Size aDocumentSizePixel
,
188 Size aTargetSizePixel
,
193 if (!mxModel
.is() || !mxController
.is() || !mxRenderable
.is())
196 Reference
< awt::XDevice
> xDevice(mxToolkit
->createScreenCompatibleDevice( aTargetSizePixel
.Width(), aTargetSizePixel
.Height() ) );
200 assert( !aDocumentSizePixel
.IsEmpty() && !aTargetSizePixel
.IsEmpty());
202 double fScaleX
= aTargetSizePixel
.Width() / static_cast<double>(aDocumentSizePixel
.Width());
203 double fScaleY
= aTargetSizePixel
.Height() / static_cast<double>(aDocumentSizePixel
.Height());
205 PropertyValues renderProps
{
206 comphelper::makePropertyValue("IsPrinter", true),
207 comphelper::makePropertyValue("RenderDevice", xDevice
),
208 comphelper::makePropertyValue("View", mxController
),
209 comphelper::makePropertyValue("RenderToGraphic", true),
210 comphelper::makePropertyValue("HasPDFExtOutDevData", bExtOutDevData
),
211 comphelper::makePropertyValue("PageRange", OUString::number(nCurrentPage
))
216 OutputDevice
* pOutputDev
= VCLUnoHelper::GetOutputDevice( xDevice
);
218 vcl::PDFExtOutDevData
aPDFExtOutDevData(*pOutputDev
);
221 aPDFExtOutDevData
.SetIsExportBookmarks(true);
222 pOutputDev
->SetExtOutDevData(&aPDFExtOutDevData
);
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
);
231 aMtf
.Record( pOutputDev
);
233 if (aPageColor
!= COL_TRANSPARENT
)
235 pOutputDev
->SetBackground(Wallpaper(aPageColor
));
239 uno::Any
aSelection( getSelection());
240 mxRenderable
->render(nCurrentPage
- 1, aSelection
, renderProps
);
244 aMtf
.SetPrefSize( aTargetSizePixel
);
247 maChapterNames
= aPDFExtOutDevData
.GetChapterNames();
249 return Graphic(aMtf
);
252 const std::vector
<OUString
>& DocumentToGraphicRenderer::getChapterNames() const
254 return maChapterNames
;
257 sal_Int32
DocumentToGraphicRenderer::getCurrentPage()
263 return getCurrentPageWriter();
265 /* TODO: other application specific page detection? */
269 sal_Int32
DocumentToGraphicRenderer::getPageCount()
271 Reference
< awt::XDevice
> xDevice(mxToolkit
->createScreenCompatibleDevice( 32, 32 ) );
273 uno::Any
selection( getSelection() );
275 PropertyValues renderProperties
{ comphelper::makePropertyValue("IsPrinter", true),
276 comphelper::makePropertyValue("RenderDevice", xDevice
),
277 comphelper::makePropertyValue("View", mxController
),
278 comphelper::makePropertyValue("RenderToGraphic", true) };
280 sal_Int32 nPages
= mxRenderable
->getRendererCount( selection
, renderProperties
);
285 sal_Int32
DocumentToGraphicRenderer::getCurrentPageWriter()
287 Reference
<text::XTextViewCursorSupplier
> xTextViewCursorSupplier(mxModel
->getCurrentController(), UNO_QUERY
);
288 if (!xTextViewCursorSupplier
.is())
290 Reference
<text::XPageCursor
> xCursor(xTextViewCursorSupplier
->getViewCursor(), UNO_QUERY
);
291 return xCursor
.is() ? xCursor
->getPage() : 1;
295 bool DocumentToGraphicRenderer::isShapeSelected(
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
)
301 if (rxController
.is())
303 uno::Reference
< view::XSelectionSupplier
> xSelectionSupplier( rxController
, uno::UNO_QUERY
);
304 if (xSelectionSupplier
.is())
306 uno::Any
aAny( xSelectionSupplier
->getSelection());
307 if (aAny
>>= rxShapes
)
309 else if (aAny
>>= rxShape
)
316 bool DocumentToGraphicRenderer::isWriter() const
318 if (meDocType
== WRITER
)
324 bool DocumentToGraphicRenderer::isCalc() const
326 if (meDocType
== CALC
)
332 bool DocumentToGraphicRenderer::isImpress() const
334 if (meDocType
== IMPRESS
)
340 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */