Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
27 :
28 :
29 :
30 : using namespace com::sun::star;
31 :
32 :
33 :
34 : namespace drawinglayer
35 : {
36 : namespace primitive2d
37 : {
38 0 : Primitive2DSequence PagePreviewPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
39 : {
40 0 : Primitive2DSequence xRetval;
41 0 : Primitive2DSequence aContent(getPageContent());
42 :
43 0 : if(aContent.hasElements()
44 0 : && basegfx::fTools::more(getContentWidth(), 0.0)
45 0 : && basegfx::fTools::more(getContentHeight(), 0.0))
46 : {
47 : // the decomposed matrix will be needed
48 0 : basegfx::B2DVector aScale, aTranslate;
49 : double fRotate, fShearX;
50 0 : getTransform().decompose(aScale, aTranslate, fRotate, fShearX);
51 :
52 0 : if(basegfx::fTools::more(aScale.getX(), 0.0) && basegfx::fTools::more(aScale.getY(), 0.0))
53 : {
54 : // check if content overlaps with tageted size and needs to be embedded with a
55 : // clipping primitive
56 0 : const basegfx::B2DRange aRealContentRange(getB2DRangeFromPrimitive2DSequence(aContent, rViewInformation));
57 0 : const basegfx::B2DRange aAllowedContentRange(0.0, 0.0, getContentWidth(), getContentHeight());
58 :
59 0 : if(!aAllowedContentRange.isInside(aRealContentRange))
60 : {
61 : const Primitive2DReference xReferenceA(
62 : new MaskPrimitive2D(
63 : basegfx::B2DPolyPolygon(
64 0 : basegfx::tools::createPolygonFromRect(aAllowedContentRange)), aContent));
65 0 : aContent = Primitive2DSequence(&xReferenceA, 1);
66 : }
67 :
68 : // create a mapping from content to object.
69 0 : basegfx::B2DHomMatrix aPageTrans;
70 :
71 0 : if(getKeepAspectRatio())
72 : {
73 : // #i101075# when keeping the aspect ratio is wanted, it is necessary to calculate
74 : // an equidistant scaling in X and Y and a corresponding translation to
75 : // center the output. Calculate needed scale factors
76 0 : const double fScaleX(aScale.getX() / getContentWidth());
77 0 : const double fScaleY(aScale.getY() / getContentHeight());
78 :
79 : // to keep the aspect, use the smaller scale and adapt missing size by translation
80 0 : if(fScaleX < fScaleY)
81 : {
82 : // height needs to be adapted
83 0 : const double fNeededHeight(aScale.getY() / fScaleX);
84 0 : const double fSpaceToAdd(fNeededHeight - getContentHeight());
85 :
86 0 : aPageTrans.translate(0.0, fSpaceToAdd * 0.5);
87 0 : aPageTrans.scale(fScaleX, aScale.getY() / fNeededHeight);
88 : }
89 : else
90 : {
91 : // width needs to be adapted
92 0 : const double fNeededWidth(aScale.getX() / fScaleY);
93 0 : const double fSpaceToAdd(fNeededWidth - getContentWidth());
94 :
95 0 : aPageTrans.translate(fSpaceToAdd * 0.5, 0.0);
96 0 : aPageTrans.scale(aScale.getX() / fNeededWidth, fScaleY);
97 : }
98 :
99 : // add the missing object transformation aspects
100 : const basegfx::B2DHomMatrix aCombined(basegfx::tools::createShearXRotateTranslateB2DHomMatrix(
101 0 : fShearX, fRotate, aTranslate.getX(), aTranslate.getY()));
102 0 : aPageTrans = aCombined * aPageTrans;
103 : }
104 : else
105 : {
106 : // completely scale to PageObject size. Scale to unit size.
107 0 : aPageTrans.scale(1.0/ getContentWidth(), 1.0 / getContentHeight());
108 :
109 : // apply object matrix
110 0 : aPageTrans *= getTransform();
111 : }
112 :
113 : // embed in necessary transformation to map from SdrPage to SdrPageObject
114 0 : const Primitive2DReference xReferenceB(new TransformPrimitive2D(aPageTrans, aContent));
115 0 : xRetval = Primitive2DSequence(&xReferenceB, 1);
116 0 : }
117 : }
118 :
119 0 : return xRetval;
120 : }
121 :
122 0 : PagePreviewPrimitive2D::PagePreviewPrimitive2D(
123 : const ::com::sun::star::uno::Reference< ::com::sun::star::drawing::XDrawPage >& rxDrawPage,
124 : const basegfx::B2DHomMatrix& rTransform,
125 : double fContentWidth,
126 : double fContentHeight,
127 : const Primitive2DSequence& rPageContent,
128 : bool bKeepAspectRatio)
129 : : BufferedDecompositionPrimitive2D(),
130 : mxDrawPage(rxDrawPage),
131 : maPageContent(rPageContent),
132 : maTransform(rTransform),
133 : mfContentWidth(fContentWidth),
134 : mfContentHeight(fContentHeight),
135 0 : mbKeepAspectRatio(bKeepAspectRatio)
136 : {
137 0 : }
138 :
139 0 : bool PagePreviewPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
140 : {
141 0 : if(BasePrimitive2D::operator==(rPrimitive))
142 : {
143 0 : const PagePreviewPrimitive2D& rCompare = static_cast< const PagePreviewPrimitive2D& >(rPrimitive);
144 :
145 0 : return (getXDrawPage() == rCompare.getXDrawPage()
146 0 : && getPageContent() == rCompare.getPageContent()
147 0 : && getTransform() == rCompare.getTransform()
148 0 : && getContentWidth() == rCompare.getContentWidth()
149 0 : && getContentHeight() == rCompare.getContentHeight()
150 0 : && getKeepAspectRatio() == rCompare.getKeepAspectRatio());
151 : }
152 :
153 0 : return false;
154 : }
155 :
156 0 : basegfx::B2DRange PagePreviewPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation`*/) const
157 : {
158 : // nothing is allowed to stick out of a PagePreviewPrimitive, thus we
159 : // can quickly deliver our range here
160 0 : basegfx::B2DRange aRetval(0.0, 0.0, 1.0, 1.0);
161 0 : aRetval.transform(getTransform());
162 0 : return aRetval;
163 : }
164 :
165 : // provide unique ID
166 0 : ImplPrimitive2DIDBlock(PagePreviewPrimitive2D, PRIMITIVE2D_ID_PAGEPREVIEWPRIMITIVE2D)
167 :
168 : } // end of namespace primitive2d
169 : } // end of namespace drawinglayer
170 :
171 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|