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>
29 using namespace com::sun::star
;
32 namespace drawinglayer
36 void PagePreviewPrimitive2D::create2DDecomposition(Primitive2DContainer
& rContainer
, const geometry::ViewInformation2D
& rViewInformation
) const
38 Primitive2DContainer
aContent(getPageContent());
41 && basegfx::fTools::more(getContentWidth(), 0.0)
42 && basegfx::fTools::more(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(basegfx::fTools::more(aScale
.getX(), 0.0) && basegfx::fTools::more(aScale
.getY(), 0.0))
51 // check if content overlaps with target size and needs to be embedded with a
53 const basegfx::B2DRange
aRealContentRange(aContent
.getB2DRange(rViewInformation
));
54 const basegfx::B2DRange
aAllowedContentRange(0.0, 0.0, getContentWidth(), getContentHeight());
56 if(!aAllowedContentRange
.isInside(aRealContentRange
))
58 const Primitive2DReference
xReferenceA(
60 basegfx::B2DPolyPolygon(
61 basegfx::utils::createPolygonFromRect(aAllowedContentRange
)), aContent
));
62 aContent
= Primitive2DContainer
{ xReferenceA
};
65 // create a mapping from content to object.
66 basegfx::B2DHomMatrix aPageTrans
;
68 // #i101075# when keeping the aspect ratio is wanted, it is necessary to calculate
69 // an equidistant scaling in X and Y and a corresponding translation to
70 // center the output. Calculate needed scale factors
71 const double fScaleX(aScale
.getX() / getContentWidth());
72 const double fScaleY(aScale
.getY() / getContentHeight());
74 // to keep the aspect, use the smaller scale and adapt missing size by translation
77 // height needs to be adapted
78 const double fNeededHeight(aScale
.getY() / fScaleX
);
79 const double fSpaceToAdd(fNeededHeight
- getContentHeight());
81 aPageTrans
.translate(0.0, fSpaceToAdd
* 0.5);
82 aPageTrans
.scale(fScaleX
, aScale
.getY() / fNeededHeight
);
86 // width needs to be adapted
87 const double fNeededWidth(aScale
.getX() / fScaleY
);
88 const double fSpaceToAdd(fNeededWidth
- getContentWidth());
90 aPageTrans
.translate(fSpaceToAdd
* 0.5, 0.0);
91 aPageTrans
.scale(aScale
.getX() / fNeededWidth
, fScaleY
);
94 // add the missing object transformation aspects
95 const basegfx::B2DHomMatrix
aCombined(basegfx::utils::createShearXRotateTranslateB2DHomMatrix(
96 fShearX
, fRotate
, aTranslate
.getX(), aTranslate
.getY()));
97 aPageTrans
= aCombined
* aPageTrans
;
99 // embed in necessary transformation to map from SdrPage to SdrPageObject
100 rContainer
.push_back(new TransformPrimitive2D(aPageTrans
, aContent
));
105 PagePreviewPrimitive2D::PagePreviewPrimitive2D(
106 const css::uno::Reference
< css::drawing::XDrawPage
>& rxDrawPage
,
107 const basegfx::B2DHomMatrix
& rTransform
,
108 double fContentWidth
,
109 double fContentHeight
,
110 const Primitive2DContainer
& rPageContent
)
111 : BufferedDecompositionPrimitive2D(),
112 mxDrawPage(rxDrawPage
),
113 maPageContent(rPageContent
),
114 maTransform(rTransform
),
115 mfContentWidth(fContentWidth
),
116 mfContentHeight(fContentHeight
)
120 bool PagePreviewPrimitive2D::operator==(const BasePrimitive2D
& rPrimitive
) const
122 if(BasePrimitive2D::operator==(rPrimitive
))
124 const PagePreviewPrimitive2D
& rCompare
= static_cast< const PagePreviewPrimitive2D
& >(rPrimitive
);
126 return (getXDrawPage() == rCompare
.getXDrawPage()
127 && getPageContent() == rCompare
.getPageContent()
128 && getTransform() == rCompare
.getTransform()
129 && getContentWidth() == rCompare
.getContentWidth()
130 && getContentHeight() == rCompare
.getContentHeight());
136 basegfx::B2DRange
PagePreviewPrimitive2D::getB2DRange(const geometry::ViewInformation2D
& /*rViewInformation`*/) const
138 // nothing is allowed to stick out of a PagePreviewPrimitive, thus we
139 // can quickly deliver our range here
140 basegfx::B2DRange
aRetval(0.0, 0.0, 1.0, 1.0);
141 aRetval
.transform(getTransform());
146 ImplPrimitive2DIDBlock(PagePreviewPrimitive2D
, PRIMITIVE2D_ID_PAGEPREVIEWPRIMITIVE2D
)
148 } // end of namespace primitive2d
149 } // end of namespace drawinglayer
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */