summaryrefslogtreecommitdiff
path: root/svtools/source/filter/DocumentToGraphicRenderer.cxx
blob: 6c95747ef09db027ce70373cff7fcb68105b103a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <svtools/DocumentToGraphicRenderer.hxx>

#include <vcl/graphicfilter.hxx>
#include <vcl/svapp.hxx>
#include <vcl/outdev.hxx>

#include <tools/fract.hxx>

#include <com/sun/star/awt/XDevice.hpp>
#include <com/sun/star/text/XPageCursor.hpp>
#include <com/sun/star/text/XTextViewCursorSupplier.hpp>
#include <com/sun/star/view/XSelectionSupplier.hpp>
#include <com/sun/star/beans/PropertyValues.hpp>

#include <toolkit/helper/vclunohelper.hxx>

using namespace css;
using namespace css::uno;
using namespace css::lang;
using namespace css::beans;

DocumentToGraphicRenderer::DocumentToGraphicRenderer( const Reference<XComponent>& rxDocument, bool bSelectionOnly ) :
    mxDocument(rxDocument),
    mxModel( mxDocument, uno::UNO_QUERY ),
    mxController( mxModel->getCurrentController() ),
    mxRenderable (mxDocument, uno::UNO_QUERY ),
    mxToolkit( VCLUnoHelper::CreateToolkit() ),
    mbSelectionOnly( bSelectionOnly )
{
}

DocumentToGraphicRenderer::~DocumentToGraphicRenderer()
{
}

Size DocumentToGraphicRenderer::getDocumentSizeInPixels(sal_Int32 aCurrentPage)
{
    Size aSize100mm = getDocumentSizeIn100mm(aCurrentPage);
    return Application::GetDefaultDevice()->LogicToPixel( aSize100mm, MapUnit::Map100thMM );
}

uno::Any DocumentToGraphicRenderer::getSelection() const
{
    uno::Any aSelection;
    aSelection <<= mxDocument;  // default: render whole document
    if (mbSelectionOnly && mxController.is())
    {
        try
        {
            uno::Reference< view::XSelectionSupplier > xSelSup( mxController, uno::UNO_QUERY);
            if (xSelSup.is())
            {
                uno::Any aViewSelection( xSelSup->getSelection());
                if (aViewSelection.hasValue())
                    aSelection = aViewSelection;
            }
        }
        catch (const uno::Exception&)
        {
        }
    }
    return aSelection;
}

Size DocumentToGraphicRenderer::getDocumentSizeIn100mm(sal_Int32 aCurrentPage)
{
    Reference< awt::XDevice > xDevice(mxToolkit->createScreenCompatibleDevice( 32, 32 ) );

    uno::Any selection( getSelection());

    PropertyValues renderProperties;

    renderProperties.realloc( 4 );
    renderProperties[0].Name = "IsPrinter";
    renderProperties[0].Value <<= true;
    renderProperties[1].Name = "RenderDevice";
    renderProperties[1].Value <<= xDevice;
    renderProperties[2].Name = "View";
    renderProperties[2].Value <<= mxController;
    renderProperties[3].Name = "RenderToGraphic";
    renderProperties[3].Value <<= true;

    awt::Size aSize;

    sal_Int32 nPages = mxRenderable->getRendererCount( selection, renderProperties );
    if (nPages >= aCurrentPage)
    {
        Sequence< beans::PropertyValue > aResult = mxRenderable->getRenderer(aCurrentPage - 1, selection, renderProperties );
        for( sal_Int32 nProperty = 0, nPropertyCount = aResult.getLength(); nProperty < nPropertyCount; ++nProperty )
        {
            if ( aResult[ nProperty ].Name == "PageSize" )
            {
                aResult[ nProperty ].Value >>= aSize;
            }
        }
    }

    return Size( aSize.Width, aSize.Height );
}

Graphic DocumentToGraphicRenderer::renderToGraphic(
    sal_Int32 aCurrentPage,
    Size aDocumentSizePixel,
    Size aTargetSizePixel,
    Color aPageColor)

{
    if (!mxModel.is() || !mxController.is() || !mxRenderable.is())
        return Graphic();

    Reference< awt::XDevice > xDevice(mxToolkit->createScreenCompatibleDevice( aTargetSizePixel.Width(), aTargetSizePixel.Height() ) );
    if (!xDevice.is())
        return Graphic();

    double fScaleX = aTargetSizePixel.Width()  / (double) aDocumentSizePixel.Width();
    double fScaleY = aTargetSizePixel.Height() / (double) aDocumentSizePixel.Height();

    PropertyValues renderProps;
    renderProps.realloc( 4 );
    renderProps[0].Name = "IsPrinter";
    renderProps[0].Value <<= true;
    renderProps[1].Name = "RenderDevice";
    renderProps[1].Value <<= xDevice;
    renderProps[2].Name = "View";
    renderProps[2].Value <<= mxController;
    renderProps[3].Name = "RenderToGraphic";
    renderProps[3].Value <<= true;

    GDIMetaFile aMtf;

    OutputDevice* pOutputDev = VCLUnoHelper::GetOutputDevice( xDevice );
    pOutputDev->SetAntialiasing(pOutputDev->GetAntialiasing() | AntialiasingFlags::EnableB2dDraw);
    MapMode mm = pOutputDev->GetMapMode();
    mm.SetScaleX( fScaleX );
    mm.SetScaleY( fScaleY );
    pOutputDev->SetMapMode( mm );

    aMtf.Record( pOutputDev );

    if (aPageColor != Color(COL_TRANSPARENT))
    {
        pOutputDev->SetBackground(Wallpaper(aPageColor));
        pOutputDev->Erase();
    }

    uno::Any aSelection( getSelection());
    mxRenderable->render(aCurrentPage - 1, aSelection, renderProps );

    aMtf.Stop();
    aMtf.WindStart();
    aMtf.SetPrefSize( aTargetSizePixel );

    return Graphic(aMtf);
}

sal_Int32 DocumentToGraphicRenderer::getCurrentPageWriter()
{
    Reference<text::XTextViewCursorSupplier> xTextViewCursorSupplier(mxModel->getCurrentController(), UNO_QUERY);
    if (!xTextViewCursorSupplier.is())
        return 1;
    Reference<text::XPageCursor> xCursor(xTextViewCursorSupplier->getViewCursor(), UNO_QUERY);
    return xCursor->getPage();
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */