cid#1606940 Check of thread-shared field evades lock acquisition
[LibreOffice.git] / svx / source / sdr / contact / viewcontactofsdrobjcustomshape.cxx
blob710510882204274828c1b1a96def4e18b523a53b
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 <sdr/contact/viewcontactofsdrobjcustomshape.hxx>
21 #include <svx/svdoashp.hxx>
22 #include <svx/sdooitm.hxx>
23 #include <sdr/primitive2d/sdrattributecreator.hxx>
24 #include <sdr/primitive2d/sdrcustomshapeprimitive2d.hxx>
25 #include <basegfx/matrix/b2dhommatrixtools.hxx>
26 #include <vcl/canvastools.hxx>
29 namespace sdr::contact
31 ViewContactOfSdrObjCustomShape::ViewContactOfSdrObjCustomShape(SdrObjCustomShape& rCustomShape)
32 : ViewContactOfTextObj(rCustomShape)
36 ViewContactOfSdrObjCustomShape::~ViewContactOfSdrObjCustomShape()
40 basegfx::B2DRange ViewContactOfSdrObjCustomShape::getCorrectedTextBoundRect() const
42 const tools::Rectangle aObjectBound(GetCustomShapeObj().GetGeoRect());
43 tools::Rectangle aTextBound(aObjectBound);
44 GetCustomShapeObj().GetTextBounds(aTextBound);
45 basegfx::B2DRange aTextRange = vcl::unotools::b2DRectangleFromRectangle(aTextBound);
46 const basegfx::B2DRange aObjectRange = vcl::unotools::b2DRectangleFromRectangle(aObjectBound);
48 // no need to correct if no extra text range
49 if(aTextRange != aObjectRange)
51 const GeoStat& rGeoStat(GetCustomShapeObj().GetGeoStat());
53 // only correct when rotation and/or shear is used
54 if(rGeoStat.m_nShearAngle || rGeoStat.m_nRotationAngle )
56 // text range needs to be corrected by
57 // aObjectRange.getCenter() - aRotObjectRange.getCenter() since it's
58 // defined differently by using rotation around object center. Start
59 // with positive part
60 basegfx::B2DVector aTranslation(aObjectRange.getCenter());
62 // get rotated and sheared object's range
63 basegfx::B2DRange aRotObjectRange(aObjectRange);
64 basegfx::B2DHomMatrix aRotMatrix;
66 aRotMatrix.translate(-aObjectRange.getMinimum().getX(), -aObjectRange.getMinimum().getY());
68 if(rGeoStat.m_nShearAngle)
70 aRotMatrix.shearX(-rGeoStat.mfTanShearAngle);
73 if(rGeoStat.m_nRotationAngle)
75 aRotMatrix.rotate(toRadians(36000_deg100 - rGeoStat.m_nRotationAngle));
78 aRotMatrix.translate(aObjectRange.getMinimum().getX(), aObjectRange.getMinimum().getY());
79 aRotObjectRange.transform(aRotMatrix);
81 // add negative translation part
82 aTranslation -= aRotObjectRange.getCenter();
84 // create new range
85 aTextRange = basegfx::B2DRange(
86 aTextRange.getMinX() + aTranslation.getX(), aTextRange.getMinY() + aTranslation.getY(),
87 aTextRange.getMaxX() + aTranslation.getX(), aTextRange.getMaxY() + aTranslation.getY());
90 // NbcMirror() of SdrTextObj (from which SdrObjCustomShape is derived), adds a
91 // 180deg rotation around the shape center to GeoStat.nRotationAngle. So remove here the
92 // 180° rotation, which was added by GetTextBounds().
93 if(GetCustomShapeObj().IsMirroredY())
95 basegfx::B2DHomMatrix aRotMatrix(basegfx::utils::createRotateAroundPoint(
96 aObjectRange.getCenterX(), aObjectRange.getCenterY(), M_PI));
97 aTextRange.transform(aRotMatrix);
101 return aTextRange;
104 void ViewContactOfSdrObjCustomShape::createViewIndependentPrimitive2DSequence(drawinglayer::primitive2d::Primitive2DDecompositionVisitor& rVisitor) const
106 const SfxItemSet& rItemSet = GetCustomShapeObj().GetMergedItemSet();
108 // #i98072# Get shadow and text; eventually suppress the text if it's
109 // a TextPath FontworkGallery object
110 const drawinglayer::attribute::SdrEffectsTextAttribute aAttribute(
111 drawinglayer::primitive2d::createNewSdrEffectsTextAttribute(
112 rItemSet,
113 GetCustomShapeObj().getText(0),
114 GetCustomShapeObj().IsTextPath()));
115 drawinglayer::primitive2d::Primitive2DContainer xGroup;
116 bool bHasText(!aAttribute.getText().isDefault());
118 // create Primitive2DContainer from sub-geometry
119 const SdrObject* pSdrObjRepresentation = GetCustomShapeObj().GetSdrObjectFromCustomShape();
120 bool b3DShape(false);
122 if(pSdrObjRepresentation)
124 // tdf#118498 The processing of SdrObjListIter for SdrIterMode::DeepNoGroups
125 // did change for 3D-Objects, it now correctly enters and iterates the
126 // SdrObjects in the E3dScene (same as for SdrObjGroup). This is more correct
127 // as the old version which just checked for dynamic_cast<const SdrObjGroup*>
128 // and *only* entered these, ignoring E3dScene as grouping-object.
129 // But how to fix that? Taking back the SdrObjListIter change would be easy, but
130 // not correct. After checking ViewContactOfE3dScene and ViewContactOfGroup
131 // I see that both traverse their children by themselves (on VC-Level,
132 // see createViewIndependentPrimitive2DSequence implementations and usage of
133 // GetObjectCount()). Thus in principle iterating here (esp. 'deep') seems to
134 // be wrong anyways, it might have even created wrong and double geometries
135 // (only with complex CustomShapes with multiple representation SdrObjects and
136 // only visible when transparency involved, but runtime-expensive).
137 // Thus: Just do not iterate, will check behaviour deeply.
138 b3DShape = (nullptr != DynCastE3dObject(pSdrObjRepresentation));
139 pSdrObjRepresentation->GetViewContact().getViewIndependentPrimitive2DContainer(xGroup);
142 if(bHasText || !xGroup.empty())
144 // prepare text box geometry
145 basegfx::B2DHomMatrix aTextBoxMatrix;
146 bool bWordWrap(false);
148 // take unrotated snap rect as default, then get the
149 // unrotated text box. Rotation needs to be done centered
150 const tools::Rectangle aObjectBound(GetCustomShapeObj().GetGeoRect());
151 const basegfx::B2DRange aObjectRange = vcl::unotools::b2DRectangleFromRectangle(aObjectBound);
153 if(bHasText)
155 // #i101684# get the text range unrotated and absolute to the object range
156 const basegfx::B2DRange aTextRange(getCorrectedTextBoundRect());
158 // Rotation before scaling
159 if(!basegfx::fTools::equalZero(GetCustomShapeObj().GetExtraTextRotation(true)))
161 basegfx::B2DVector aTranslation(0.5, 0.5);
162 aTextBoxMatrix.translate( -aTranslation.getX(), -aTranslation.getY() );
163 aTextBoxMatrix.rotate(basegfx::deg2rad(
164 360.0 - GetCustomShapeObj().GetExtraTextRotation(true)));
165 aTextBoxMatrix.translate( aTranslation.getX(), aTranslation.getY() );
167 // give text object a size
168 aTextBoxMatrix.scale(aTextRange.getWidth(), aTextRange.getHeight());
170 // check if we have a rotation/shear at all to take care of
171 const double fExtraTextRotation(GetCustomShapeObj().GetExtraTextRotation());
172 const GeoStat& rGeoStat(GetCustomShapeObj().GetGeoStat());
174 if(rGeoStat.m_nShearAngle || rGeoStat.m_nRotationAngle || !basegfx::fTools::equalZero(fExtraTextRotation))
176 if(aObjectRange != aTextRange)
178 // move relative to unrotated object range
179 aTextBoxMatrix.translate(
180 aTextRange.getMinX() - aObjectRange.getMinimum().getX(),
181 aTextRange.getMinY() - aObjectRange.getMinimum().getY());
184 if(!basegfx::fTools::equalZero(fExtraTextRotation))
186 basegfx::B2DVector aTranslation(
187 ( aTextRange.getWidth() / 2 ) + ( aTextRange.getMinX() - aObjectRange.getMinimum().getX() ),
188 ( aTextRange.getHeight() / 2 ) + ( aTextRange.getMinY() - aObjectRange.getMinimum().getY() ) );
189 aTextBoxMatrix.translate( -aTranslation.getX(), -aTranslation.getY() );
190 aTextBoxMatrix.rotate(basegfx::deg2rad(360.0 - fExtraTextRotation));
191 aTextBoxMatrix.translate( aTranslation.getX(), aTranslation.getY() );
194 if(rGeoStat.m_nShearAngle)
196 aTextBoxMatrix.shearX(-rGeoStat.mfTanShearAngle);
199 if(rGeoStat.m_nRotationAngle)
201 aTextBoxMatrix.rotate(toRadians(36000_deg100 - rGeoStat.m_nRotationAngle));
204 // give text it's target position
205 aTextBoxMatrix.translate(aObjectRange.getMinimum().getX(), aObjectRange.getMinimum().getY());
207 else
209 aTextBoxMatrix.translate(aTextRange.getMinX(), aTextRange.getMinY());
212 // check if SdrTextWordWrapItem is set
213 bWordWrap = GetCustomShapeObj().GetMergedItem(SDRATTR_TEXT_WORDWRAP).GetValue();
216 // fill object matrix
217 const basegfx::B2DHomMatrix aObjectMatrix(basegfx::utils::createScaleShearXRotateTranslateB2DHomMatrix(
218 aObjectRange.getWidth(), aObjectRange.getHeight(),
219 /*fShearX=*/0, /*fRotate=*/0,
220 aObjectRange.getMinX(), aObjectRange.getMinY()));
222 // create primitive
223 const drawinglayer::primitive2d::Primitive2DReference xReference(
224 new drawinglayer::primitive2d::SdrCustomShapePrimitive2D(
225 aAttribute,
226 std::move(xGroup),
227 aTextBoxMatrix,
228 bWordWrap,
229 b3DShape,
230 aObjectMatrix));
231 rVisitor.visit(xReference);
235 } // end of namespace
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */