bump product version to 7.6.3.2-android
[LibreOffice.git] / svx / source / sdr / contact / viewcontactofsdrpathobj.cxx
blob0c1ed2af250b12b92dacc0a4f6cb365c4b0a1fac
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 .
21 #include <sdr/contact/viewcontactofsdrpathobj.hxx>
22 #include <svx/svdopath.hxx>
23 #include <sdr/primitive2d/sdrattributecreator.hxx>
24 #include <basegfx/polygon/b2dpolypolygontools.hxx>
25 #include <sdr/primitive2d/sdrpathprimitive2d.hxx>
26 #include <basegfx/matrix/b2dhommatrixtools.hxx>
27 #include <basegfx/polygon/b2dpolygontools.hxx>
28 #include <osl/diagnose.h>
29 #include <unotools/configmgr.hxx>
30 #include <vcl/canvastools.hxx>
32 namespace sdr::contact
34 ViewContactOfSdrPathObj::ViewContactOfSdrPathObj(SdrPathObj& rPathObj)
35 : ViewContactOfTextObj(rPathObj)
39 ViewContactOfSdrPathObj::~ViewContactOfSdrPathObj()
43 /// return true if polycount == 1
44 static bool ensureGeometry(basegfx::B2DPolyPolygon& rUnitPolyPolygon)
46 sal_uInt32 nPolyCount(rUnitPolyPolygon.count());
47 sal_uInt32 nPointCount(0);
49 for(auto const& rPolygon : std::as_const(rUnitPolyPolygon))
51 nPointCount += rPolygon.count();
52 // return early if we definitely have geometry
53 if (nPointCount > 1)
54 return nPolyCount == 1;
57 if(!nPointCount)
59 OSL_FAIL("PolyPolygon object without geometry detected, this should not be created (!)");
60 basegfx::B2DPolygon aFallbackLine;
61 aFallbackLine.append(basegfx::B2DPoint(0.0, 0.0));
62 aFallbackLine.append(basegfx::B2DPoint(1000.0, 1000.0));
63 rUnitPolyPolygon = basegfx::B2DPolyPolygon(aFallbackLine);
65 nPolyCount = 1;
68 return nPolyCount == 1;
71 void ViewContactOfSdrPathObj::createViewIndependentPrimitive2DSequence(drawinglayer::primitive2d::Primitive2DDecompositionVisitor& rVisitor) const
73 const SfxItemSet& rItemSet = GetPathObj().GetMergedItemSet();
74 const drawinglayer::attribute::SdrLineFillEffectsTextAttribute aAttribute(
75 drawinglayer::primitive2d::createNewSdrLineFillEffectsTextAttribute(
76 rItemSet,
77 GetPathObj().getText(0),
78 false));
79 basegfx::B2DPolyPolygon aUnitPolyPolygon(GetPathObj().GetPathPoly());
80 bool bPolyCountIsOne(ensureGeometry(aUnitPolyPolygon));
82 // prepare object transformation and unit polygon (direct model data)
83 basegfx::B2DHomMatrix aObjectMatrix;
84 basegfx::B2DPolyPolygon aUnitDefinitionPolyPolygon;
85 const bool bIsLine(
86 !aUnitPolyPolygon.areControlPointsUsed()
87 && bPolyCountIsOne
88 && 2 == aUnitPolyPolygon.getB2DPolygon(0).count());
90 if(bIsLine)
92 // special handling for single line mode (2 points)
93 const basegfx::B2DPolygon aSubPolygon(aUnitPolyPolygon.getB2DPolygon(0));
94 const basegfx::B2DPoint aStart(aSubPolygon.getB2DPoint(0));
95 const basegfx::B2DPoint aEnd(aSubPolygon.getB2DPoint(1));
96 const basegfx::B2DVector aLine(aEnd - aStart);
98 // #i102548# create new unit polygon for line (horizontal)
99 basegfx::B2DPolygon aNewPolygon;
100 aNewPolygon.append(basegfx::B2DPoint(0.0, 0.0));
101 aNewPolygon.append(basegfx::B2DPoint(1.0, 0.0));
102 aUnitPolyPolygon.setB2DPolygon(0, aNewPolygon);
104 // #i102548# fill objectMatrix with rotation and offset (no shear for lines)
105 aObjectMatrix = basegfx::utils::createScaleShearXRotateTranslateB2DHomMatrix(
106 aLine.getLength(), 1.0,
107 0.0,
108 atan2(aLine.getY(), aLine.getX()),
109 aStart.getX(), aStart.getY());
111 else
113 // #i102548# create unscaled, unsheared, unrotated and untranslated polygon
114 // (unit polygon) by creating the object matrix and back-transforming the polygon
115 const basegfx::B2DRange aObjectRange(basegfx::utils::getRange(aUnitPolyPolygon));
116 const GeoStat& rGeoStat(GetPathObj().GetGeoStat());
117 const double fWidth(aObjectRange.getWidth());
118 const double fHeight(aObjectRange.getHeight());
119 const double fScaleX(basegfx::fTools::equalZero(fWidth) ? 1.0 : fWidth);
120 const double fScaleY(basegfx::fTools::equalZero(fHeight) ? 1.0 : fHeight);
122 aObjectMatrix = basegfx::utils::createScaleShearXRotateTranslateB2DHomMatrix(
123 fScaleX, fScaleY,
124 -rGeoStat.mfTanShearAngle,
125 rGeoStat.nRotationAngle ? toRadians(36000_deg100 - rGeoStat.nRotationAngle) : 0.0,
126 aObjectRange.getMinX(), aObjectRange.getMinY());
128 // create unit polygon from object's absolute path
129 basegfx::B2DHomMatrix aInverse(aObjectMatrix);
130 aInverse.invert();
131 aUnitPolyPolygon.transform(aInverse);
133 // OperationSmiley: Check if a FillGeometryDefiningShape is set
134 const SdrObject* pFillGeometryDefiningShape(GetPathObj().getFillGeometryDefiningShape());
136 if(nullptr != pFillGeometryDefiningShape)
138 // If yes, get it's BoundRange and use as defining Geometry for the FillStyle.
139 // If no, aUnitDefinitionPolyPolygon will just be empty and thus be interpreted
140 // as unused.
141 // Using SnapRect will make the FillDefinition to always be extended e.g.
142 // for rotated/sheared objects.
143 const tools::Rectangle& rSnapRect(pFillGeometryDefiningShape->GetSnapRect());
145 aUnitDefinitionPolyPolygon.append(
146 basegfx::utils::createPolygonFromRect(
147 vcl::unotools::b2DRectangleFromRectangle(rSnapRect)));
149 // use same coordinate system as the shape geometry -> this
150 // makes it relative to shape's unit geometry and thus freely
151 // transformable with the shape
152 aUnitDefinitionPolyPolygon.transform(aInverse);
156 // create primitive. Always create primitives to allow the decomposition of
157 // SdrPathPrimitive2D to create needed invisible elements for HitTest and/or BoundRect
158 const drawinglayer::primitive2d::Primitive2DReference xReference(
159 new drawinglayer::primitive2d::SdrPathPrimitive2D(
160 aObjectMatrix,
161 aAttribute,
162 std::move(aUnitPolyPolygon),
163 std::move(aUnitDefinitionPolyPolygon)));
165 rVisitor.visit(xReference);
168 } // end of namespace
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */