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 <drawinglayer/primitive2d/pagepreviewprimitive2d.hxx>
21 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
22 #include <drawinglayer/primitive2d/maskprimitive2d.hxx>
23 #include <basegfx/polygon/b2dpolygontools.hxx>
24 #include <basegfx/polygon/b2dpolygon.hxx>
25 #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
26 #include <basegfx/matrix/b2dhommatrixtools.hxx>
30 using namespace com::sun::star
;
33 namespace drawinglayer::primitive2d
35 Primitive2DReference
PagePreviewPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D
& rViewInformation
) const
37 Primitive2DContainer
aContent(getPageContent());
39 if(!(!aContent
.empty()
40 && getContentWidth() > 0.0)
41 && getContentHeight() > 0.0)
44 // the decomposed matrix will be needed
45 basegfx::B2DVector aScale
, aTranslate
;
46 double fRotate
, fShearX
;
47 getTransform().decompose(aScale
, aTranslate
, fRotate
, fShearX
);
49 if(!(aScale
.getX() > 0.0 && aScale
.getY() > 0.0))
52 // check if content overlaps with target size and needs to be embedded with a
54 const basegfx::B2DRange
aRealContentRange(aContent
.getB2DRange(rViewInformation
));
55 const basegfx::B2DRange
aAllowedContentRange(0.0, 0.0, getContentWidth(), getContentHeight());
57 if(!aAllowedContentRange
.isInside(aRealContentRange
))
59 const Primitive2DReference
xReferenceA(
61 basegfx::B2DPolyPolygon(
62 basegfx::utils::createPolygonFromRect(aAllowedContentRange
)), std::move(aContent
)));
63 aContent
= Primitive2DContainer
{ xReferenceA
};
66 // create a mapping from content to object.
67 basegfx::B2DHomMatrix aPageTrans
;
69 // #i101075# when keeping the aspect ratio is wanted, it is necessary to calculate
70 // an equidistant scaling in X and Y and a corresponding translation to
71 // center the output. Calculate needed scale factors
72 const double fScaleX(aScale
.getX() / getContentWidth());
73 const double fScaleY(aScale
.getY() / getContentHeight());
75 // to keep the aspect, use the smaller scale and adapt missing size by translation
78 // height needs to be adapted
79 const double fNeededHeight(aScale
.getY() / fScaleX
);
80 const double fSpaceToAdd(fNeededHeight
- getContentHeight());
82 aPageTrans
.translate(0.0, fSpaceToAdd
* 0.5);
83 aPageTrans
.scale(fScaleX
, aScale
.getY() / fNeededHeight
);
87 // width needs to be adapted
88 const double fNeededWidth(aScale
.getX() / fScaleY
);
89 const double fSpaceToAdd(fNeededWidth
- getContentWidth());
91 aPageTrans
.translate(fSpaceToAdd
* 0.5, 0.0);
92 aPageTrans
.scale(aScale
.getX() / fNeededWidth
, fScaleY
);
95 // add the missing object transformation aspects
96 const basegfx::B2DHomMatrix
aCombined(basegfx::utils::createShearXRotateTranslateB2DHomMatrix(
97 fShearX
, fRotate
, aTranslate
.getX(), aTranslate
.getY()));
98 aPageTrans
= aCombined
* aPageTrans
;
100 // embed in necessary transformation to map from SdrPage to SdrPageObject
101 return new TransformPrimitive2D(aPageTrans
, std::move(aContent
));
104 PagePreviewPrimitive2D::PagePreviewPrimitive2D(
105 css::uno::Reference
< css::drawing::XDrawPage
> xDrawPage
,
106 basegfx::B2DHomMatrix aTransform
,
107 double fContentWidth
,
108 double fContentHeight
,
109 Primitive2DContainer
&& rPageContent
)
110 : mxDrawPage(std::move(xDrawPage
)),
111 maPageContent(std::move(rPageContent
)),
112 maTransform(std::move(aTransform
)),
113 mfContentWidth(fContentWidth
),
114 mfContentHeight(fContentHeight
)
118 bool PagePreviewPrimitive2D::operator==(const BasePrimitive2D
& rPrimitive
) const
120 if(BasePrimitive2D::operator==(rPrimitive
))
122 const PagePreviewPrimitive2D
& rCompare
= static_cast< const PagePreviewPrimitive2D
& >(rPrimitive
);
124 return (getXDrawPage() == rCompare
.getXDrawPage()
125 && getPageContent() == rCompare
.getPageContent()
126 && getTransform() == rCompare
.getTransform()
127 && getContentWidth() == rCompare
.getContentWidth()
128 && getContentHeight() == rCompare
.getContentHeight());
134 basegfx::B2DRange
PagePreviewPrimitive2D::getB2DRange(const geometry::ViewInformation2D
& /*rViewInformation`*/) const
136 // nothing is allowed to stick out of a PagePreviewPrimitive, thus we
137 // can quickly deliver our range here
138 basegfx::B2DRange
aRetval(0.0, 0.0, 1.0, 1.0);
139 aRetval
.transform(getTransform());
144 sal_uInt32
PagePreviewPrimitive2D::getPrimitive2DID() const
146 return PRIMITIVE2D_ID_PAGEPREVIEWPRIMITIVE2D
;
149 } // end of namespace
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */