remove assert looking for new compatibilityMode DOCX
[LibreOffice.git] / svtools / source / filter / DocumentToGraphicRenderer.cxx
blob16668b7e9e3b808f67cb4bfa3212d72303404d2e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
44 using namespace css;
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() ),
55 meDocType( UNKNOWN )
57 try
59 uno::Reference< lang::XServiceInfo > xServiceInfo( mxDocument, uno::UNO_QUERY);
60 if (xServiceInfo.is())
62 if (xServiceInfo->supportsService(u"com.sun.star.text.TextDocument"_ustr))
63 meDocType = WRITER;
64 else if (xServiceInfo->supportsService(u"com.sun.star.sheet.SpreadsheetDocument"_ustr))
65 meDocType = CALC;
66 else if (xServiceInfo->supportsService(u"com.sun.star.presentation.PresentationDocument"_ustr))
67 meDocType = IMPRESS;
68 else
69 meDocType = UNKNOWN;
72 catch (const uno::Exception&)
76 if (!(bSelectionOnly && mxController.is()))
77 return;
79 try
81 uno::Reference< view::XSelectionSupplier > xSelSup( mxController, uno::UNO_QUERY);
82 if (xSelSup.is())
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. */
92 if (!isWriter())
93 maSelection = std::move(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
119 uno::Any aSelection;
120 if (hasSelection())
121 aSelection = maSelection;
122 else
123 aSelection <<= mxDocument; // default: render whole document
124 return aSelection;
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(u"IsPrinter"_ustr, true),
135 comphelper::makePropertyValue(u"RenderDevice"_ustr, xDevice),
136 comphelper::makePropertyValue(u"View"_ustr, mxController),
137 comphelper::makePropertyValue(u"RenderToGraphic"_ustr, true) };
139 awt::Size aSize;
140 awt::Size aCalcPageSize;
141 awt::Point aPos;
142 awt::Point aCalcPos;
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);
177 if (pCalcPageSize)
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,
189 Color aPageColor,
190 bool bExtOutDevData)
193 if (!mxModel.is() || !mxController.is() || !mxRenderable.is())
194 return Graphic();
196 Reference< awt::XDevice > xDevice(mxToolkit->createScreenCompatibleDevice( aTargetSizePixel.Width(), aTargetSizePixel.Height() ) );
197 if (!xDevice.is())
198 return Graphic();
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(u"IsPrinter"_ustr, true),
207 comphelper::makePropertyValue(u"RenderDevice"_ustr, xDevice),
208 comphelper::makePropertyValue(u"View"_ustr, mxController),
209 comphelper::makePropertyValue(u"RenderToGraphic"_ustr, true),
210 comphelper::makePropertyValue(u"HasPDFExtOutDevData"_ustr, bExtOutDevData),
211 comphelper::makePropertyValue(u"IsLastPage"_ustr, true), // see PrinterController::abortJob
212 comphelper::makePropertyValue(u"PageRange"_ustr, OUString::number(nCurrentPage))
215 GDIMetaFile aMtf;
217 OutputDevice* pOutputDev = VCLUnoHelper::GetOutputDevice( xDevice );
219 vcl::PDFExtOutDevData aPDFExtOutDevData(*pOutputDev);
220 if (bExtOutDevData)
222 aPDFExtOutDevData.SetIsExportBookmarks(true);
223 pOutputDev->SetExtOutDevData(&aPDFExtOutDevData);
226 pOutputDev->SetAntialiasing(pOutputDev->GetAntialiasing() | AntialiasingFlags::Enable);
227 MapMode mm = pOutputDev->GetMapMode();
228 mm.SetScaleX( Fraction(fScaleX) );
229 mm.SetScaleY( Fraction(fScaleY) );
230 pOutputDev->SetMapMode( mm );
232 aMtf.Record( pOutputDev );
234 if (aPageColor != COL_TRANSPARENT)
236 pOutputDev->SetBackground(Wallpaper(aPageColor));
237 pOutputDev->Erase();
240 uno::Any aSelection( getSelection());
241 mxRenderable->render(nCurrentPage - 1, aSelection, renderProps );
243 aMtf.Stop();
244 aMtf.WindStart();
245 aMtf.SetPrefSize( aTargetSizePixel );
247 if (bExtOutDevData)
248 maChapterNames = aPDFExtOutDevData.GetChapterNames();
250 return Graphic(aMtf);
253 const std::vector<OUString>& DocumentToGraphicRenderer::getChapterNames() const
255 return maChapterNames;
258 sal_Int32 DocumentToGraphicRenderer::getCurrentPage()
260 if (hasSelection())
261 return 1;
263 if (isWriter())
264 return getCurrentPageWriter();
266 /* TODO: other application specific page detection? */
267 return 1;
270 sal_Int32 DocumentToGraphicRenderer::getPageCount()
272 Reference< awt::XDevice > xDevice(mxToolkit->createScreenCompatibleDevice( 32, 32 ) );
274 uno::Any selection( getSelection() );
276 PropertyValues renderProperties{ comphelper::makePropertyValue(u"IsPrinter"_ustr, true),
277 comphelper::makePropertyValue(u"RenderDevice"_ustr, xDevice),
278 comphelper::makePropertyValue(u"View"_ustr, mxController),
279 comphelper::makePropertyValue(u"RenderToGraphic"_ustr, true) };
281 sal_Int32 nPages = mxRenderable->getRendererCount( selection, renderProperties );
283 return nPages;
286 sal_Int32 DocumentToGraphicRenderer::getCurrentPageWriter()
288 Reference<text::XTextViewCursorSupplier> xTextViewCursorSupplier(mxModel->getCurrentController(), UNO_QUERY);
289 if (!xTextViewCursorSupplier.is())
290 return 1;
291 Reference<text::XPageCursor> xCursor(xTextViewCursorSupplier->getViewCursor(), UNO_QUERY);
292 return xCursor.is() ? xCursor->getPage() : 1;
295 // static
296 bool DocumentToGraphicRenderer::isShapeSelected(
297 css::uno::Reference< css::drawing::XShapes > & rxShapes,
298 css::uno::Reference< css::drawing::XShape > & rxShape,
299 const css::uno::Reference< css::frame::XController > & rxController )
301 bool bShape = false;
302 if (rxController.is())
304 uno::Reference< view::XSelectionSupplier > xSelectionSupplier( rxController, uno::UNO_QUERY);
305 if (xSelectionSupplier.is())
307 uno::Any aAny( xSelectionSupplier->getSelection());
308 if (aAny >>= rxShapes)
309 bShape = true;
310 else if (aAny >>= rxShape)
311 bShape = true;
314 return bShape;
317 bool DocumentToGraphicRenderer::isWriter() const
319 if (meDocType == WRITER)
320 return true;
321 else
322 return false;
325 bool DocumentToGraphicRenderer::isCalc() const
327 if (meDocType == CALC)
328 return true;
329 else
330 return false;
333 bool DocumentToGraphicRenderer::isImpress() const
335 if (meDocType == IMPRESS)
336 return true;
337 else
338 return false;
341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */