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 <svx/sdr/primitive2d/sdrmeasureprimitive2d.hxx>
21 : #include <svx/sdr/primitive2d/sdrdecompositiontools.hxx>
22 : #include <basegfx/matrix/b2dhommatrix.hxx>
23 : #include <svx/sdr/primitive2d/sdrtextprimitive2d.hxx>
24 : #include <svx/sdr/attribute/sdrtextattribute.hxx>
25 : #include <basegfx/polygon/b2dpolypolygontools.hxx>
26 : #include <basegfx/tools/canvastools.hxx>
27 : #include <drawinglayer/primitive2d/groupprimitive2d.hxx>
28 : #include <svx/sdr/primitive2d/svx_primitivetypes2d.hxx>
29 : #include <basegfx/matrix/b2dhommatrixtools.hxx>
30 : #include <drawinglayer/primitive2d/hiddengeometryprimitive2d.hxx>
31 :
32 :
33 :
34 : using namespace com::sun::star;
35 :
36 :
37 :
38 : namespace drawinglayer
39 : {
40 : namespace primitive2d
41 : {
42 0 : Primitive2DReference SdrMeasurePrimitive2D::impCreatePart(
43 : const attribute::SdrLineAttribute& rLineAttribute,
44 : const basegfx::B2DHomMatrix& rObjectMatrix,
45 : const basegfx::B2DPoint& rStart,
46 : const basegfx::B2DPoint& rEnd,
47 : bool bLeftActive,
48 : bool bRightActive) const
49 : {
50 0 : const attribute::SdrLineStartEndAttribute& rLineStartEnd = getSdrLSTAttribute().getLineStartEnd();
51 0 : basegfx::B2DPolygon aPolygon;
52 :
53 0 : aPolygon.append(rStart);
54 0 : aPolygon.append(rEnd);
55 0 : aPolygon.transform(rObjectMatrix);
56 :
57 0 : if(rLineStartEnd.isDefault() || (!bLeftActive && !bRightActive))
58 : {
59 : return createPolygonLinePrimitive(
60 : aPolygon,
61 : rLineAttribute,
62 0 : attribute::SdrLineStartEndAttribute());
63 : }
64 :
65 0 : if(bLeftActive && bRightActive)
66 : {
67 : return createPolygonLinePrimitive(
68 : aPolygon,
69 : rLineAttribute,
70 0 : rLineStartEnd);
71 : }
72 :
73 0 : const basegfx::B2DPolyPolygon aEmpty;
74 : const attribute::SdrLineStartEndAttribute aLineStartEnd(
75 : bLeftActive ? rLineStartEnd.getStartPolyPolygon() : aEmpty, bRightActive ? rLineStartEnd.getEndPolyPolygon() : aEmpty,
76 : bLeftActive ? rLineStartEnd.getStartWidth() : 0.0, bRightActive ? rLineStartEnd.getEndWidth() : 0.0,
77 0 : bLeftActive ? rLineStartEnd.isStartActive() : false, bRightActive ? rLineStartEnd.isEndActive() : false,
78 0 : bLeftActive ? rLineStartEnd.isStartCentered() : false, bRightActive? rLineStartEnd.isEndCentered() : false);
79 :
80 : return createPolygonLinePrimitive(
81 : aPolygon,
82 : rLineAttribute,
83 0 : aLineStartEnd);
84 : }
85 :
86 0 : Primitive2DSequence SdrMeasurePrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& aViewInformation) const
87 : {
88 0 : Primitive2DSequence aRetval;
89 0 : SdrBlockTextPrimitive2D* pBlockText = 0;
90 0 : basegfx::B2DRange aTextRange;
91 0 : double fTextX((getStart().getX() + getEnd().getX()) * 0.5);
92 0 : double fTextY((getStart().getX() + getEnd().getX()) * 0.5);
93 0 : const basegfx::B2DVector aLine(getEnd() - getStart());
94 0 : const double fDistance(aLine.getLength());
95 0 : const double fAngle(atan2(aLine.getY(), aLine.getX()));
96 0 : bool bAutoUpsideDown(false);
97 0 : const attribute::SdrTextAttribute rTextAttribute = getSdrLSTAttribute().getText();
98 : const basegfx::B2DHomMatrix aObjectMatrix(
99 0 : basegfx::tools::createShearXRotateTranslateB2DHomMatrix(0.0, fAngle, getStart()));
100 :
101 : // preapare text, but do not add yet; it needs to be aligned to
102 : // the line geometry
103 0 : if(!rTextAttribute.isDefault())
104 : {
105 0 : basegfx::B2DHomMatrix aTextMatrix;
106 0 : double fTestAngle(fAngle);
107 :
108 0 : if(getTextRotation())
109 : {
110 0 : aTextMatrix.rotate(-90.0 * F_PI180);
111 0 : fTestAngle -= (90.0 * F_PI180);
112 :
113 0 : if(getTextAutoAngle() && fTestAngle < -F_PI)
114 : {
115 0 : fTestAngle += F_2PI;
116 : }
117 : }
118 :
119 0 : if(getTextAutoAngle())
120 : {
121 0 : if(fTestAngle > (F_PI / 4.0) || fTestAngle < (-F_PI * (3.0 / 4.0)))
122 : {
123 0 : bAutoUpsideDown = true;
124 : }
125 : }
126 :
127 : // create primitive and get text range
128 : pBlockText = new SdrBlockTextPrimitive2D(
129 : &rTextAttribute.getSdrText(),
130 : rTextAttribute.getOutlinerParaObject(),
131 : aTextMatrix,
132 : SDRTEXTHORZADJUST_CENTER,
133 : SDRTEXTVERTADJUST_CENTER,
134 0 : rTextAttribute.isScroll(),
135 : false,
136 : false,
137 : false,
138 0 : false);
139 :
140 0 : aTextRange = pBlockText->getB2DRange(aViewInformation);
141 : }
142 :
143 : // prepare line attribute and result
144 : {
145 0 : const attribute::SdrLineAttribute rLineAttribute(getSdrLSTAttribute().getLine());
146 0 : bool bArrowsOutside(false);
147 0 : bool bMainLineSplitted(false);
148 0 : const attribute::SdrLineStartEndAttribute& rLineStartEnd = getSdrLSTAttribute().getLineStartEnd();
149 0 : double fStartArrowW(0.0);
150 0 : double fStartArrowH(0.0);
151 0 : double fEndArrowW(0.0);
152 0 : double fEndArrowH(0.0);
153 :
154 0 : if(!rLineStartEnd.isDefault())
155 : {
156 0 : if(rLineStartEnd.isStartActive())
157 : {
158 0 : const basegfx::B2DRange aArrowRange(basegfx::tools::getRange(rLineStartEnd.getStartPolyPolygon()));
159 0 : fStartArrowW = rLineStartEnd.getStartWidth();
160 0 : fStartArrowH = aArrowRange.getHeight() * fStartArrowW / aArrowRange.getWidth();
161 :
162 0 : if(rLineStartEnd.isStartCentered())
163 : {
164 0 : fStartArrowH *= 0.5;
165 : }
166 : }
167 :
168 0 : if(rLineStartEnd.isEndActive())
169 : {
170 0 : const basegfx::B2DRange aArrowRange(basegfx::tools::getRange(rLineStartEnd.getEndPolyPolygon()));
171 0 : fEndArrowW = rLineStartEnd.getEndWidth();
172 0 : fEndArrowH = aArrowRange.getHeight() * fEndArrowW / aArrowRange.getWidth();
173 :
174 0 : if(rLineStartEnd.isEndCentered())
175 : {
176 0 : fEndArrowH *= 0.5;
177 : }
178 : }
179 : }
180 :
181 0 : const double fSpaceNeededByArrows(fStartArrowH + fEndArrowH + ((fStartArrowW + fEndArrowW) * 0.5));
182 0 : const double fArrowsOutsideLen((fStartArrowH + fEndArrowH + fStartArrowW + fEndArrowW) * 0.5);
183 0 : const double fHalfLineWidth(rLineAttribute.getWidth() * 0.5);
184 :
185 0 : if(fSpaceNeededByArrows > fDistance)
186 : {
187 0 : bArrowsOutside = true;
188 : }
189 :
190 0 : MeasureTextPosition eHorizontal(getHorizontal());
191 0 : MeasureTextPosition eVertical(getVertical());
192 :
193 0 : if(MEASURETEXTPOSITION_AUTOMATIC == eVertical)
194 : {
195 0 : eVertical = MEASURETEXTPOSITION_NEGATIVE;
196 : }
197 :
198 0 : if(MEASURETEXTPOSITION_CENTERED == eVertical)
199 : {
200 0 : bMainLineSplitted = true;
201 : }
202 :
203 0 : if(MEASURETEXTPOSITION_AUTOMATIC == eHorizontal)
204 : {
205 0 : if(aTextRange.getWidth() > fDistance)
206 : {
207 0 : eHorizontal = MEASURETEXTPOSITION_NEGATIVE;
208 : }
209 : else
210 : {
211 0 : eHorizontal = MEASURETEXTPOSITION_CENTERED;
212 : }
213 :
214 0 : if(bMainLineSplitted)
215 : {
216 0 : if(aTextRange.getWidth() + fSpaceNeededByArrows > fDistance)
217 : {
218 0 : bArrowsOutside = true;
219 : }
220 : }
221 : else
222 : {
223 0 : const double fSmallArrowNeed(fStartArrowH + fEndArrowH + ((fStartArrowW + fEndArrowW) * 0.125));
224 :
225 0 : if(aTextRange.getWidth() + fSmallArrowNeed > fDistance)
226 : {
227 0 : bArrowsOutside = true;
228 : }
229 : }
230 : }
231 :
232 0 : if(MEASURETEXTPOSITION_CENTERED != eHorizontal)
233 : {
234 0 : bArrowsOutside = true;
235 : }
236 :
237 : // switch text above/below?
238 0 : if(getBelow() || (bAutoUpsideDown && !getTextRotation()))
239 : {
240 0 : if(MEASURETEXTPOSITION_NEGATIVE == eVertical)
241 : {
242 0 : eVertical = MEASURETEXTPOSITION_POSITIVE;
243 : }
244 0 : else if(MEASURETEXTPOSITION_POSITIVE == eVertical)
245 : {
246 0 : eVertical = MEASURETEXTPOSITION_NEGATIVE;
247 : }
248 : }
249 :
250 0 : const double fMainLineOffset(getBelow() ? getDistance() : -getDistance());
251 0 : const basegfx::B2DPoint aMainLeft(0.0, fMainLineOffset);
252 0 : const basegfx::B2DPoint aMainRight(fDistance, fMainLineOffset);
253 :
254 : // main line
255 0 : if(bArrowsOutside)
256 : {
257 0 : double fLenLeft(fArrowsOutsideLen);
258 0 : double fLenRight(fArrowsOutsideLen);
259 :
260 0 : if(!bMainLineSplitted)
261 : {
262 0 : if(MEASURETEXTPOSITION_NEGATIVE == eHorizontal)
263 : {
264 0 : fLenLeft = fStartArrowH + aTextRange.getWidth();
265 : }
266 0 : else if(MEASURETEXTPOSITION_POSITIVE == eHorizontal)
267 : {
268 0 : fLenRight = fEndArrowH + aTextRange.getWidth();
269 : }
270 : }
271 :
272 0 : const basegfx::B2DPoint aMainLeftLeft(aMainLeft.getX() - fLenLeft, aMainLeft.getY());
273 0 : const basegfx::B2DPoint aMainRightRight(aMainRight.getX() + fLenRight, aMainRight.getY());
274 :
275 0 : appendPrimitive2DReferenceToPrimitive2DSequence(aRetval, impCreatePart(rLineAttribute, aObjectMatrix, aMainLeftLeft, aMainLeft, false, true));
276 0 : appendPrimitive2DReferenceToPrimitive2DSequence(aRetval, impCreatePart(rLineAttribute, aObjectMatrix, aMainRight, aMainRightRight, true, false));
277 :
278 0 : if(!bMainLineSplitted || MEASURETEXTPOSITION_CENTERED != eHorizontal)
279 : {
280 0 : appendPrimitive2DReferenceToPrimitive2DSequence(aRetval, impCreatePart(rLineAttribute, aObjectMatrix, aMainLeft, aMainRight, false, false));
281 0 : }
282 : }
283 : else
284 : {
285 0 : if(bMainLineSplitted)
286 : {
287 0 : const double fHalfLength((fDistance - (aTextRange.getWidth() + (fStartArrowH + fEndArrowH) * 0.25)) * 0.5);
288 0 : const basegfx::B2DPoint aMainInnerLeft(aMainLeft.getX() + fHalfLength, aMainLeft.getY());
289 0 : const basegfx::B2DPoint aMainInnerRight(aMainRight.getX() - fHalfLength, aMainRight.getY());
290 :
291 0 : appendPrimitive2DReferenceToPrimitive2DSequence(aRetval, impCreatePart(rLineAttribute, aObjectMatrix, aMainLeft, aMainInnerLeft, true, false));
292 0 : appendPrimitive2DReferenceToPrimitive2DSequence(aRetval, impCreatePart(rLineAttribute, aObjectMatrix, aMainInnerRight, aMainRight, false, true));
293 : }
294 : else
295 : {
296 0 : appendPrimitive2DReferenceToPrimitive2DSequence(aRetval, impCreatePart(rLineAttribute, aObjectMatrix, aMainLeft, aMainRight, true, true));
297 : }
298 : }
299 :
300 : // left/right help line value preparation
301 0 : const double fTopEdge(getBelow() ? getUpper() + getDistance() : -getUpper() - getDistance());
302 0 : const double fBottomLeft(getBelow() ? getLower() - getLeftDelta() : getLeftDelta() - getLower());
303 0 : const double fBottomRight(getBelow() ? getLower() - getRightDelta() : getRightDelta() - getLower());
304 :
305 : // left help line
306 0 : const basegfx::B2DPoint aLeftUp(0.0, fTopEdge);
307 0 : const basegfx::B2DPoint aLeftDown(0.0, fBottomLeft);
308 :
309 0 : appendPrimitive2DReferenceToPrimitive2DSequence(aRetval, impCreatePart(rLineAttribute, aObjectMatrix, aLeftDown, aLeftUp, false, false));
310 :
311 : // right help line
312 0 : const basegfx::B2DPoint aRightUp(fDistance, fTopEdge);
313 0 : const basegfx::B2DPoint aRightDown(fDistance, fBottomRight);
314 :
315 0 : appendPrimitive2DReferenceToPrimitive2DSequence(aRetval, impCreatePart(rLineAttribute, aObjectMatrix, aRightDown, aRightUp, false, false));
316 :
317 : // text horizontal position
318 0 : if(MEASURETEXTPOSITION_NEGATIVE == eHorizontal)
319 : {
320 : // left
321 0 : const double fSmall(fArrowsOutsideLen * 0.18);
322 0 : fTextX = aMainLeft.getX() - (fStartArrowH + aTextRange.getWidth() + fSmall + fHalfLineWidth);
323 :
324 0 : if(bMainLineSplitted)
325 : {
326 0 : fTextX -= (fArrowsOutsideLen - fStartArrowH);
327 : }
328 :
329 0 : if(!rTextAttribute.isDefault())
330 : {
331 0 : fTextX -= rTextAttribute.getTextRightDistance();
332 : }
333 : }
334 0 : else if(MEASURETEXTPOSITION_POSITIVE == eHorizontal)
335 : {
336 : // right
337 0 : const double fSmall(fArrowsOutsideLen * 0.18);
338 0 : fTextX = aMainRight.getX() + (fEndArrowH + fSmall + fHalfLineWidth);
339 :
340 0 : if(bMainLineSplitted)
341 : {
342 0 : fTextX += (fArrowsOutsideLen - fEndArrowH);
343 : }
344 :
345 0 : if(!rTextAttribute.isDefault())
346 : {
347 0 : fTextX += rTextAttribute.getTextLeftDistance();
348 : }
349 : }
350 : else // MEASURETEXTPOSITION_CENTERED
351 : {
352 : // centered
353 0 : fTextX = aMainLeft.getX() + ((fDistance - aTextRange.getWidth()) * 0.5);
354 :
355 0 : if(!rTextAttribute.isDefault())
356 : {
357 0 : fTextX += (rTextAttribute.getTextLeftDistance() - rTextAttribute.getTextRightDistance()) / 2L;
358 : }
359 : }
360 :
361 : // text vertical position
362 0 : if(MEASURETEXTPOSITION_NEGATIVE == eVertical)
363 : {
364 : // top
365 0 : const double fSmall(fArrowsOutsideLen * 0.10);
366 0 : fTextY = aMainLeft.getY() - (aTextRange.getHeight() + fSmall + fHalfLineWidth);
367 :
368 0 : if(!rTextAttribute.isDefault())
369 : {
370 0 : fTextY -= rTextAttribute.getTextLowerDistance();
371 : }
372 : }
373 0 : else if(MEASURETEXTPOSITION_POSITIVE == eVertical)
374 : {
375 : // bottom
376 0 : const double fSmall(fArrowsOutsideLen * 0.10);
377 0 : fTextY = aMainLeft.getY() + (fSmall + fHalfLineWidth);
378 :
379 0 : if(!rTextAttribute.isDefault())
380 : {
381 0 : fTextY += rTextAttribute.getTextUpperDistance();
382 : }
383 : }
384 : else // MEASURETEXTPOSITION_CENTERED
385 : {
386 : // centered
387 0 : fTextY = aMainLeft.getY() - (aTextRange.getHeight() * 0.5);
388 :
389 0 : if(!rTextAttribute.isDefault())
390 : {
391 0 : fTextY += (rTextAttribute.getTextUpperDistance() - rTextAttribute.getTextLowerDistance()) / 2L;
392 : }
393 0 : }
394 : }
395 :
396 0 : if(getSdrLSTAttribute().getLine().isDefault())
397 : {
398 : // embed line geometry to invisible (100% transparent) line group for HitTest
399 0 : const Primitive2DReference xHiddenLines(new HiddenGeometryPrimitive2D(aRetval));
400 :
401 0 : aRetval = Primitive2DSequence(&xHiddenLines, 1);
402 : }
403 :
404 0 : if(pBlockText)
405 : {
406 : // create transformation to text primitive end position
407 0 : basegfx::B2DHomMatrix aChange;
408 :
409 : // handle auto text rotation
410 0 : if(bAutoUpsideDown)
411 : {
412 0 : aChange.rotate(F_PI);
413 : }
414 :
415 : // move from aTextRange.TopLeft to fTextX, fTextY
416 0 : aChange.translate(fTextX - aTextRange.getMinX(), fTextY - aTextRange.getMinY());
417 :
418 : // apply object matrix
419 0 : aChange *= aObjectMatrix;
420 :
421 : // apply to existing text primitive
422 0 : SdrTextPrimitive2D* pNewBlockText = pBlockText->createTransformedClone(aChange);
423 : OSL_ENSURE(pNewBlockText, "SdrMeasurePrimitive2D::create2DDecomposition: Could not create transformed clone of text primitive (!)");
424 0 : delete pBlockText;
425 :
426 : // add to local primitives
427 0 : appendPrimitive2DReferenceToPrimitive2DSequence(aRetval, Primitive2DReference(pNewBlockText));
428 : }
429 :
430 : // add shadow
431 0 : if(!getSdrLSTAttribute().getShadow().isDefault())
432 : {
433 0 : aRetval = createEmbeddedShadowPrimitive(
434 : aRetval,
435 0 : getSdrLSTAttribute().getShadow());
436 : }
437 :
438 0 : return aRetval;
439 : }
440 :
441 0 : SdrMeasurePrimitive2D::SdrMeasurePrimitive2D(
442 : const attribute::SdrLineShadowTextAttribute& rSdrLSTAttribute,
443 : const basegfx::B2DPoint& rStart,
444 : const basegfx::B2DPoint& rEnd,
445 : MeasureTextPosition eHorizontal,
446 : MeasureTextPosition eVertical,
447 : double fDistance,
448 : double fUpper,
449 : double fLower,
450 : double fLeftDelta,
451 : double fRightDelta,
452 : bool bBelow,
453 : bool bTextRotation,
454 : bool bTextAutoAngle)
455 : : BufferedDecompositionPrimitive2D(),
456 : maSdrLSTAttribute(rSdrLSTAttribute),
457 : maStart(rStart),
458 : maEnd(rEnd),
459 : meHorizontal(eHorizontal),
460 : meVertical(eVertical),
461 : mfDistance(fDistance),
462 : mfUpper(fUpper),
463 : mfLower(fLower),
464 : mfLeftDelta(fLeftDelta),
465 : mfRightDelta(fRightDelta),
466 : mbBelow(bBelow),
467 : mbTextRotation(bTextRotation),
468 0 : mbTextAutoAngle(bTextAutoAngle)
469 : {
470 0 : }
471 :
472 0 : bool SdrMeasurePrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
473 : {
474 0 : if(BufferedDecompositionPrimitive2D::operator==(rPrimitive))
475 : {
476 0 : const SdrMeasurePrimitive2D& rCompare = (SdrMeasurePrimitive2D&)rPrimitive;
477 :
478 0 : return (getStart() == rCompare.getStart()
479 0 : && getEnd() == rCompare.getEnd()
480 0 : && getHorizontal() == rCompare.getHorizontal()
481 0 : && getVertical() == rCompare.getVertical()
482 0 : && getDistance() == rCompare.getDistance()
483 0 : && getUpper() == rCompare.getUpper()
484 0 : && getLower() == rCompare.getLower()
485 0 : && getLeftDelta() == rCompare.getLeftDelta()
486 0 : && getRightDelta() == rCompare.getRightDelta()
487 0 : && getBelow() == rCompare.getBelow()
488 0 : && getTextRotation() == rCompare.getTextRotation()
489 0 : && getTextAutoAngle() == rCompare.getTextAutoAngle()
490 0 : && getSdrLSTAttribute() == rCompare.getSdrLSTAttribute());
491 : }
492 :
493 0 : return false;
494 : }
495 :
496 : // provide unique ID
497 0 : ImplPrimitive2DIDBlock(SdrMeasurePrimitive2D, PRIMITIVE2D_ID_SDRMEASUREPRIMITIVE2D)
498 :
499 : } // end of namespace primitive2d
500 : } // end of namespace drawinglayer
501 :
502 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|