/src/libreoffice/drawinglayer/source/primitive2d/pagepreviewprimitive2d.cxx
Line | Count | Source |
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 | | #include <utility> |
28 | | |
29 | | |
30 | | using namespace com::sun::star; |
31 | | |
32 | | |
33 | | namespace drawinglayer::primitive2d |
34 | | { |
35 | | Primitive2DReference PagePreviewPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const |
36 | 0 | { |
37 | 0 | Primitive2DContainer aContent(getPageContent()); |
38 | |
|
39 | 0 | if(!(!aContent.empty() |
40 | 0 | && getContentWidth() > 0.0) |
41 | 0 | && getContentHeight() > 0.0) |
42 | 0 | return nullptr; |
43 | | |
44 | | // the decomposed matrix will be needed |
45 | 0 | basegfx::B2DVector aScale, aTranslate; |
46 | 0 | double fRotate, fShearX; |
47 | 0 | getTransform().decompose(aScale, aTranslate, fRotate, fShearX); |
48 | |
|
49 | 0 | if(!(aScale.getX() > 0.0 && aScale.getY() > 0.0)) |
50 | 0 | return nullptr; |
51 | | |
52 | | // check if content overlaps with target size and needs to be embedded with a |
53 | | // clipping primitive |
54 | 0 | const basegfx::B2DRange aRealContentRange(aContent.getB2DRange(rViewInformation)); |
55 | 0 | const basegfx::B2DRange aAllowedContentRange(0.0, 0.0, getContentWidth(), getContentHeight()); |
56 | |
|
57 | 0 | if(!aAllowedContentRange.isInside(aRealContentRange)) |
58 | 0 | { |
59 | 0 | const Primitive2DReference xReferenceA( |
60 | 0 | new MaskPrimitive2D( |
61 | 0 | basegfx::B2DPolyPolygon( |
62 | 0 | basegfx::utils::createPolygonFromRect(aAllowedContentRange)), std::move(aContent))); |
63 | 0 | aContent = Primitive2DContainer { xReferenceA }; |
64 | 0 | } |
65 | | |
66 | | // create a mapping from content to object. |
67 | 0 | basegfx::B2DHomMatrix aPageTrans; |
68 | | |
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 | 0 | const double fScaleX(aScale.getX() / getContentWidth()); |
73 | 0 | const double fScaleY(aScale.getY() / getContentHeight()); |
74 | | |
75 | | // to keep the aspect, use the smaller scale and adapt missing size by translation |
76 | 0 | if(fScaleX < fScaleY) |
77 | 0 | { |
78 | | // height needs to be adapted |
79 | 0 | const double fNeededHeight(aScale.getY() / fScaleX); |
80 | 0 | const double fSpaceToAdd(fNeededHeight - getContentHeight()); |
81 | |
|
82 | 0 | aPageTrans.translate(0.0, fSpaceToAdd * 0.5); |
83 | 0 | aPageTrans.scale(fScaleX, aScale.getY() / fNeededHeight); |
84 | 0 | } |
85 | 0 | else |
86 | 0 | { |
87 | | // width needs to be adapted |
88 | 0 | const double fNeededWidth(aScale.getX() / fScaleY); |
89 | 0 | const double fSpaceToAdd(fNeededWidth - getContentWidth()); |
90 | |
|
91 | 0 | aPageTrans.translate(fSpaceToAdd * 0.5, 0.0); |
92 | 0 | aPageTrans.scale(aScale.getX() / fNeededWidth, fScaleY); |
93 | 0 | } |
94 | | |
95 | | // add the missing object transformation aspects |
96 | 0 | const basegfx::B2DHomMatrix aCombined(basegfx::utils::createShearXRotateTranslateB2DHomMatrix( |
97 | 0 | fShearX, fRotate, aTranslate.getX(), aTranslate.getY())); |
98 | 0 | aPageTrans = aCombined * aPageTrans; |
99 | | |
100 | | // embed in necessary transformation to map from SdrPage to SdrPageObject |
101 | 0 | return new TransformPrimitive2D(aPageTrans, std::move(aContent)); |
102 | 0 | } |
103 | | |
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 | 0 | : mxDrawPage(std::move(xDrawPage)), |
111 | 0 | maPageContent(std::move(rPageContent)), |
112 | 0 | maTransform(std::move(aTransform)), |
113 | 0 | mfContentWidth(fContentWidth), |
114 | 0 | mfContentHeight(fContentHeight) |
115 | 0 | { |
116 | 0 | } |
117 | | |
118 | | bool PagePreviewPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const |
119 | 0 | { |
120 | 0 | if(BasePrimitive2D::operator==(rPrimitive)) |
121 | 0 | { |
122 | 0 | const PagePreviewPrimitive2D& rCompare = static_cast< const PagePreviewPrimitive2D& >(rPrimitive); |
123 | |
|
124 | 0 | return (getXDrawPage() == rCompare.getXDrawPage() |
125 | 0 | && getPageContent() == rCompare.getPageContent() |
126 | 0 | && getTransform() == rCompare.getTransform() |
127 | 0 | && getContentWidth() == rCompare.getContentWidth() |
128 | 0 | && getContentHeight() == rCompare.getContentHeight()); |
129 | 0 | } |
130 | | |
131 | 0 | return false; |
132 | 0 | } |
133 | | |
134 | | basegfx::B2DRange PagePreviewPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation`*/) const |
135 | 0 | { |
136 | | // nothing is allowed to stick out of a PagePreviewPrimitive, thus we |
137 | | // can quickly deliver our range here |
138 | 0 | basegfx::B2DRange aRetval(0.0, 0.0, 1.0, 1.0); |
139 | 0 | aRetval.transform(getTransform()); |
140 | 0 | return aRetval; |
141 | 0 | } |
142 | | |
143 | | // provide unique ID |
144 | | sal_uInt32 PagePreviewPrimitive2D::getPrimitive2DID() const |
145 | 0 | { |
146 | 0 | return PRIMITIVE2D_ID_PAGEPREVIEWPRIMITIVE2D; |
147 | 0 | } |
148 | | |
149 | | } // end of namespace |
150 | | |
151 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |