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 <vcl/graphicfilter.hxx>
23 #include <vcl/svapp.hxx>
24 #include <vcl/outdev.hxx>
26 #include <com/sun/star/awt/XDevice.hpp>
27 #include <com/sun/star/text/XPageCursor.hpp>
28 #include <com/sun/star/text/XTextViewCursorSupplier.hpp>
29 #include <com/sun/star/beans/PropertyValues.hpp>
31 #include <toolkit/helper/vclunohelper.hxx>
34 using namespace css::uno
;
35 using namespace css::lang
;
36 using namespace css::beans
;
38 DocumentToGraphicRenderer::DocumentToGraphicRenderer( const Reference
<XComponent
>& rxDocument
) :
39 mxDocument(rxDocument
),
40 mxModel( mxDocument
, uno::UNO_QUERY
),
41 mxController( mxModel
->getCurrentController() ),
42 mxRenderable (mxDocument
, uno::UNO_QUERY
),
43 mxToolkit( VCLUnoHelper::CreateToolkit() )
47 DocumentToGraphicRenderer::~DocumentToGraphicRenderer()
51 Size
DocumentToGraphicRenderer::getDocumentSizeInPixels(sal_Int32 aCurrentPage
)
53 Size aSize100mm
= getDocumentSizeIn100mm(aCurrentPage
);
54 return Size( Application::GetDefaultDevice()->LogicToPixel( aSize100mm
, MAP_100TH_MM
) );
57 Size
DocumentToGraphicRenderer::getDocumentSizeIn100mm(sal_Int32 aCurrentPage
)
59 Reference
< awt::XDevice
> xDevice(mxToolkit
->createScreenCompatibleDevice( 32, 32 ) );
62 selection
<<= mxDocument
;
64 PropertyValues renderProperties
;
66 renderProperties
.realloc( 3 );
67 renderProperties
[0].Name
= "IsPrinter";
68 renderProperties
[0].Value
<<= sal_True
;
69 renderProperties
[1].Name
= "RenderDevice";
70 renderProperties
[1].Value
<<= xDevice
;
71 renderProperties
[2].Name
= "View";
72 renderProperties
[2].Value
<<= mxController
;
76 sal_Int32 nPages
= mxRenderable
->getRendererCount( selection
, renderProperties
);
77 if (nPages
>= aCurrentPage
)
79 Sequence
< beans::PropertyValue
> aResult
= mxRenderable
->getRenderer(aCurrentPage
- 1, selection
, renderProperties
);
80 for( sal_Int32 nProperty
= 0, nPropertyCount
= aResult
.getLength(); nProperty
< nPropertyCount
; ++nProperty
)
82 if ( aResult
[ nProperty
].Name
== "PageSize" )
84 aResult
[ nProperty
].Value
>>= aSize
;
89 return Size( aSize
.Width
, aSize
.Height
);
92 Graphic
DocumentToGraphicRenderer::renderToGraphic(
93 sal_Int32 aCurrentPage
,
94 Size aDocumentSizePixel
,
95 Size aTargetSizePixel
)
98 if (!mxModel
.is() || !mxController
.is() || !mxRenderable
.is())
101 Reference
< awt::XDevice
> xDevice(mxToolkit
->createScreenCompatibleDevice( aTargetSizePixel
.Width(), aTargetSizePixel
.Height() ) );
105 double fScaleX
= aTargetSizePixel
.Width() / (double) aDocumentSizePixel
.Width();
106 double fScaleY
= aTargetSizePixel
.Height() / (double) aDocumentSizePixel
.Height();
108 PropertyValues renderProps
;
109 renderProps
.realloc( 3 );
110 renderProps
[0].Name
= "IsPrinter";
111 renderProps
[0].Value
<<= sal_True
;
112 renderProps
[1].Name
= "RenderDevice";
113 renderProps
[1].Value
<<= xDevice
;
114 renderProps
[2].Name
= "View";
115 renderProps
[2].Value
<<= mxController
;
119 OutputDevice
* pOutputDev
= VCLUnoHelper::GetOutputDevice( xDevice
);
120 pOutputDev
->SetAntialiasing(pOutputDev
->GetAntialiasing() | ANTIALIASING_ENABLE_B2DDRAW
);
121 MapMode mm
= pOutputDev
->GetMapMode();
122 mm
.SetScaleX( fScaleX
);
123 mm
.SetScaleY( fScaleY
);
124 pOutputDev
->SetMapMode( mm
);
126 aMtf
.Record( pOutputDev
);
129 aSelection
<<= mxDocument
;
130 mxRenderable
->render(aCurrentPage
- 1, aSelection
, renderProps
);
134 aMtf
.SetPrefSize( aTargetSizePixel
);
136 return Graphic(aMtf
);
139 sal_Int32
DocumentToGraphicRenderer::getCurrentPageWriter()
141 Reference
<text::XTextViewCursorSupplier
> xTextViewCursorSupplier(mxModel
->getCurrentController(), UNO_QUERY
);
142 Reference
<text::XPageCursor
> xCursor(xTextViewCursorSupplier
->getViewCursor(), UNO_QUERY
);
143 return xCursor
->getPage();
146 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */