/src/libreoffice/svx/source/sdr/contact/viewcontactofsdrpathobj.cxx
Line | Count | Source (jump to first uncovered line) |
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 | | |
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 <drawinglayer/primitive2d/invertprimitive2d.hxx> |
27 | | #include <basegfx/matrix/b2dhommatrixtools.hxx> |
28 | | #include <basegfx/polygon/b2dpolygontools.hxx> |
29 | | #include <osl/diagnose.h> |
30 | | #include <vcl/canvastools.hxx> |
31 | | |
32 | | namespace sdr::contact |
33 | | { |
34 | | ViewContactOfSdrPathObj::ViewContactOfSdrPathObj(SdrPathObj& rPathObj) |
35 | 150k | : ViewContactOfTextObj(rPathObj) |
36 | 150k | { |
37 | 150k | } |
38 | | |
39 | | ViewContactOfSdrPathObj::~ViewContactOfSdrPathObj() |
40 | 150k | { |
41 | 150k | } |
42 | | |
43 | | /// return true if polycount == 1 |
44 | | static bool ensureGeometry(basegfx::B2DPolyPolygon& rUnitPolyPolygon) |
45 | 357 | { |
46 | 357 | sal_uInt32 nPolyCount(rUnitPolyPolygon.count()); |
47 | 357 | sal_uInt32 nPointCount(0); |
48 | | |
49 | 357 | for(auto const& rPolygon : std::as_const(rUnitPolyPolygon)) |
50 | 357 | { |
51 | 357 | nPointCount += rPolygon.count(); |
52 | | // return early if we definitely have geometry |
53 | 357 | if (nPointCount > 1) |
54 | 357 | return nPolyCount == 1; |
55 | 357 | } |
56 | | |
57 | 0 | if(!nPointCount) |
58 | 0 | { |
59 | 0 | OSL_FAIL("PolyPolygon object without geometry detected, this should not be created (!)"); |
60 | 0 | basegfx::B2DPolygon aFallbackLine; |
61 | 0 | aFallbackLine.append(basegfx::B2DPoint(0.0, 0.0)); |
62 | 0 | aFallbackLine.append(basegfx::B2DPoint(1000.0, 1000.0)); |
63 | 0 | rUnitPolyPolygon = basegfx::B2DPolyPolygon(aFallbackLine); |
64 | |
|
65 | 0 | nPolyCount = 1; |
66 | 0 | } |
67 | |
|
68 | 0 | return nPolyCount == 1; |
69 | 357 | } |
70 | | |
71 | | void ViewContactOfSdrPathObj::createViewIndependentPrimitive2DSequence(drawinglayer::primitive2d::Primitive2DDecompositionVisitor& rVisitor) const |
72 | 357 | { |
73 | 357 | const SfxItemSet& rItemSet = GetPathObj().GetMergedItemSet(); |
74 | 357 | const drawinglayer::attribute::SdrLineFillEffectsTextAttribute aAttribute( |
75 | 357 | drawinglayer::primitive2d::createNewSdrLineFillEffectsTextAttribute( |
76 | 357 | rItemSet, |
77 | 357 | GetPathObj().getText(0), |
78 | 357 | false)); |
79 | 357 | basegfx::B2DPolyPolygon aUnitPolyPolygon(GetPathObj().GetPathPoly()); |
80 | 357 | bool bPolyCountIsOne(ensureGeometry(aUnitPolyPolygon)); |
81 | | |
82 | | // prepare object transformation and unit polygon (direct model data) |
83 | 357 | basegfx::B2DHomMatrix aObjectMatrix; |
84 | 357 | basegfx::B2DPolyPolygon aUnitDefinitionPolyPolygon; |
85 | 357 | const bool bIsLine( |
86 | 357 | !aUnitPolyPolygon.areControlPointsUsed() |
87 | 357 | && bPolyCountIsOne |
88 | 357 | && 2 == aUnitPolyPolygon.getB2DPolygon(0).count()); |
89 | | |
90 | 357 | if(bIsLine) |
91 | 321 | { |
92 | | // special handling for single line mode (2 points) |
93 | 321 | const basegfx::B2DPolygon & rSubPolygon(aUnitPolyPolygon.getB2DPolygon(0)); |
94 | 321 | const basegfx::B2DPoint aStart(rSubPolygon.getB2DPoint(0)); |
95 | 321 | const basegfx::B2DPoint aEnd(rSubPolygon.getB2DPoint(1)); |
96 | 321 | const basegfx::B2DVector aLine(aEnd - aStart); |
97 | | |
98 | | // #i102548# create new unit polygon for line (horizontal) |
99 | 321 | static const basegfx::B2DPolygon aNewPolygon{basegfx::B2DPoint(0.0, 0.0), basegfx::B2DPoint(1.0, 0.0)}; |
100 | 321 | aUnitPolyPolygon.setB2DPolygon(0, aNewPolygon); |
101 | | |
102 | | // #i102548# fill objectMatrix with rotation and offset (no shear for lines) |
103 | 321 | aObjectMatrix = basegfx::utils::createScaleShearXRotateTranslateB2DHomMatrix( |
104 | 321 | aLine.getLength(), 1.0, |
105 | 321 | 0.0, |
106 | 321 | atan2(aLine.getY(), aLine.getX()), |
107 | 321 | aStart.getX(), aStart.getY()); |
108 | 321 | } |
109 | 36 | else |
110 | 36 | { |
111 | | // #i102548# create unscaled, unsheared, unrotated and untranslated polygon |
112 | | // (unit polygon) by creating the object matrix and back-transforming the polygon |
113 | 36 | const basegfx::B2DRange aObjectRange(basegfx::utils::getRange(aUnitPolyPolygon)); |
114 | 36 | const GeoStat& rGeoStat(GetPathObj().GetGeoStat()); |
115 | 36 | const double fWidth(aObjectRange.getWidth()); |
116 | 36 | const double fHeight(aObjectRange.getHeight()); |
117 | 36 | const double fScaleX(basegfx::fTools::equalZero(fWidth) ? 1.0 : fWidth); |
118 | 36 | const double fScaleY(basegfx::fTools::equalZero(fHeight) ? 1.0 : fHeight); |
119 | | |
120 | 36 | aObjectMatrix = basegfx::utils::createScaleShearXRotateTranslateB2DHomMatrix( |
121 | 36 | fScaleX, fScaleY, |
122 | 36 | -rGeoStat.mfTanShearAngle, |
123 | 36 | rGeoStat.m_nRotationAngle ? toRadians(36000_deg100 - rGeoStat.m_nRotationAngle) : 0.0, |
124 | 36 | aObjectRange.getMinX(), aObjectRange.getMinY()); |
125 | | |
126 | | // create unit polygon from object's absolute path |
127 | 36 | basegfx::B2DHomMatrix aInverse(aObjectMatrix); |
128 | 36 | aInverse.invert(); |
129 | 36 | aUnitPolyPolygon.transform(aInverse); |
130 | | |
131 | | // OperationSmiley: Check if a FillGeometryDefiningShape is set |
132 | 36 | const SdrObject* pFillGeometryDefiningShape(GetPathObj().getFillGeometryDefiningShape()); |
133 | | |
134 | 36 | if(nullptr != pFillGeometryDefiningShape) |
135 | 0 | { |
136 | | // If yes, get it's BoundRange and use as defining Geometry for the FillStyle. |
137 | | // If no, aUnitDefinitionPolyPolygon will just be empty and thus be interpreted |
138 | | // as unused. |
139 | | // Using SnapRect will make the FillDefinition to always be extended e.g. |
140 | | // for rotated/sheared objects. |
141 | 0 | const tools::Rectangle& rSnapRect(pFillGeometryDefiningShape->GetSnapRect()); |
142 | |
|
143 | 0 | aUnitDefinitionPolyPolygon.append( |
144 | 0 | basegfx::utils::createPolygonFromRect( |
145 | 0 | vcl::unotools::b2DRectangleFromRectangle(rSnapRect))); |
146 | | |
147 | | // use same coordinate system as the shape geometry -> this |
148 | | // makes it relative to shape's unit geometry and thus freely |
149 | | // transformable with the shape |
150 | 0 | aUnitDefinitionPolyPolygon.transform(aInverse); |
151 | 0 | } |
152 | 36 | } |
153 | | |
154 | | // create primitive. Always create primitives to allow the decomposition of |
155 | | // SdrPathPrimitive2D to create needed invisible elements for HitTest and/or BoundRect |
156 | 357 | const drawinglayer::primitive2d::Primitive2DReference xReference( |
157 | 357 | new drawinglayer::primitive2d::SdrPathPrimitive2D( |
158 | 357 | aObjectMatrix, |
159 | 357 | aAttribute, |
160 | 357 | std::move(aUnitPolyPolygon), |
161 | 357 | std::move(aUnitDefinitionPolyPolygon))); |
162 | | |
163 | | #ifdef DBG_UTIL |
164 | | // helper to create something that uses InvertPrimitive2D to be able |
165 | | // to check/debug implementations in SDPRs. There is not much left |
166 | | // doing InvertPrimitive2D nowadays (luckily). To use, create a polygon |
167 | | // using one of the polygon tools. |
168 | | static bool bTestInvert(false); |
169 | | if (bTestInvert) |
170 | | { |
171 | | drawinglayer::primitive2d::Primitive2DContainer aContainer; |
172 | | aContainer.push_back(xReference); |
173 | | const drawinglayer::primitive2d::Primitive2DReference xReference2( |
174 | | new drawinglayer::primitive2d::InvertPrimitive2D(std::move(aContainer))); |
175 | | rVisitor.visit(xReference2); |
176 | | return; |
177 | | } |
178 | | #endif |
179 | 357 | rVisitor.visit(xReference); |
180 | 357 | } |
181 | | |
182 | | } // end of namespace |
183 | | |
184 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |