LibreOffice Module svx (master)  1
gradtrns.cxx
Go to the documentation of this file.
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 "gradtrns.hxx"
22 #include <svx/svdobj.hxx>
26 #include <tools/helpers.hxx>
27 #include <vcl/canvastools.hxx>
28 
29 
31 {
32  // handle start color
33  rV.aCol1 = rG.aGradient.GetStartColor();
34  if(100 != rG.aGradient.GetStartIntens())
35  {
36  const double fFact(static_cast<double>(rG.aGradient.GetStartIntens()) / 100.0);
37  rV.aCol1 = Color(rV.aCol1.getBColor() * fFact);
38  }
39 
40  // handle end color
41  rV.aCol2 = rG.aGradient.GetEndColor();
42  if(100 != rG.aGradient.GetEndIntens())
43  {
44  const double fFact(static_cast<double>(rG.aGradient.GetEndIntens()) / 100.0);
45  rV.aCol2 = Color(rV.aCol2.getBColor() * fFact);
46  }
47 
48  // calc the basic positions
49  const tools::Rectangle aObjectSnapRectangle(pObj->GetSnapRect());
50  const basegfx::B2DRange aRange = vcl::unotools::b2DRectangleFromRectangle(aObjectSnapRectangle);
51  const basegfx::B2DPoint aCenter(aRange.getCenter());
52  basegfx::B2DPoint aStartPos, aEndPos;
53 
54  switch(rG.aGradient.GetGradientStyle())
55  {
56  case css::awt::GradientStyle_LINEAR :
57  {
58  aStartPos = basegfx::B2DPoint(aCenter.getX(), aRange.getMinY());
59  aEndPos = basegfx::B2DPoint(aCenter.getX(), aRange.getMaximum().getY());
60 
61  if(rG.aGradient.GetBorder())
62  {
63  basegfx::B2DVector aFullVec(aStartPos - aEndPos);
64  const double fLen = (aFullVec.getLength() * (100.0 - static_cast<double>(rG.aGradient.GetBorder()))) / 100.0;
65  aFullVec.normalize();
66  aStartPos = aEndPos + (aFullVec * fLen);
67  }
68 
69  if(rG.aGradient.GetAngle())
70  {
71  const double fAngle = basegfx::deg2rad(rG.aGradient.GetAngle().get() / 10.0);
72  const basegfx::B2DHomMatrix aTransformation(basegfx::utils::createRotateAroundPoint(aCenter, -fAngle));
73 
74  aStartPos *= aTransformation;
75  aEndPos *= aTransformation;
76  }
77  break;
78  }
79  case css::awt::GradientStyle_AXIAL :
80  {
81  aStartPos = aCenter;
82  aEndPos = basegfx::B2DPoint(aCenter.getX(), aRange.getMaximum().getY());
83 
84  if(rG.aGradient.GetBorder())
85  {
86  basegfx::B2DVector aFullVec(aEndPos - aStartPos);
87  const double fLen = (aFullVec.getLength() * (100.0 - static_cast<double>(rG.aGradient.GetBorder()))) / 100.0;
88  aFullVec.normalize();
89  aEndPos = aStartPos + (aFullVec * fLen);
90  }
91 
92  if(rG.aGradient.GetAngle())
93  {
94  const double fAngle = basegfx::deg2rad(rG.aGradient.GetAngle().get() / 10.0);
95  const basegfx::B2DHomMatrix aTransformation(basegfx::utils::createRotateAroundPoint(aCenter, -fAngle));
96 
97  aStartPos *= aTransformation;
98  aEndPos *= aTransformation;
99  }
100  break;
101  }
102  case css::awt::GradientStyle_RADIAL :
103  case css::awt::GradientStyle_SQUARE :
104  {
105  aStartPos = basegfx::B2DPoint(aRange.getMinX(), aRange.getMaximum().getY());
106  aEndPos = basegfx::B2DPoint(aRange.getMinX(), aRange.getMinY());
107 
108  if(rG.aGradient.GetBorder())
109  {
110  basegfx::B2DVector aFullVec(aStartPos - aEndPos);
111  const double fLen = (aFullVec.getLength() * (100.0 - static_cast<double>(rG.aGradient.GetBorder()))) / 100.0;
112  aFullVec.normalize();
113  aStartPos = aEndPos + (aFullVec * fLen);
114  }
115 
116  if(rG.aGradient.GetAngle())
117  {
118  const double fAngle = basegfx::deg2rad(rG.aGradient.GetAngle().get() / 10.0);
119  const basegfx::B2DHomMatrix aTransformation(basegfx::utils::createRotateAroundPoint(aEndPos, -fAngle));
120 
121  aStartPos *= aTransformation;
122  aEndPos *= aTransformation;
123  }
124 
125  if(rG.aGradient.GetXOffset() || rG.aGradient.GetYOffset())
126  {
127  basegfx::B2DPoint aOffset(
128  (aRange.getWidth() * rG.aGradient.GetXOffset()) / 100.0,
129  (aRange.getHeight() * rG.aGradient.GetYOffset()) / 100.0);
130 
131  aStartPos += aOffset;
132  aEndPos += aOffset;
133  }
134 
135  break;
136  }
137  case css::awt::GradientStyle_ELLIPTICAL :
138  case css::awt::GradientStyle_RECT :
139  {
140  aStartPos = basegfx::B2DPoint(aRange.getMinX(), aCenter.getY());
141  aEndPos = basegfx::B2DPoint(aRange.getMinX(), aRange.getMinY());
142 
143  if(rG.aGradient.GetBorder())
144  {
145  basegfx::B2DVector aFullVec(aStartPos - aEndPos);
146  const double fLen = (aFullVec.getLength() * (100.0 - static_cast<double>(rG.aGradient.GetBorder()))) / 100.0;
147  aFullVec.normalize();
148  aStartPos = aEndPos + (aFullVec * fLen);
149  }
150 
151  if(rG.aGradient.GetAngle())
152  {
153  const double fAngle = basegfx::deg2rad(rG.aGradient.GetAngle().get() / 10.0);
154  const basegfx::B2DHomMatrix aTransformation(basegfx::utils::createRotateAroundPoint(aEndPos, -fAngle));
155 
156  aStartPos *= aTransformation;
157  aEndPos *= aTransformation;
158  }
159 
160  if(rG.aGradient.GetXOffset() || rG.aGradient.GetYOffset())
161  {
162  basegfx::B2DPoint aOffset(
163  (aRange.getWidth() * rG.aGradient.GetXOffset()) / 100.0,
164  (aRange.getHeight() * rG.aGradient.GetYOffset()) / 100.0);
165 
166  aStartPos += aOffset;
167  aEndPos += aOffset;
168  }
169 
170  break;
171  }
172  default:
173  break;
174  }
175 
176  // set values for vector positions now
177  rV.maPositionA = aStartPos;
178  rV.maPositionB = aEndPos;
179 }
180 
181 
183  bool bMoveSingle, bool bMoveFirst)
184 {
185  // fill old gradient to new gradient to have a base
186  rG = rGOld;
187 
188  // handle color changes
189  if(rV.aCol1 != rGOld.aGradient.GetStartColor())
190  {
192  rG.aGradient.SetStartIntens(100);
193  }
194  if(rV.aCol2 != rGOld.aGradient.GetEndColor())
195  {
196  rG.aGradient.SetEndColor(rV.aCol2);
197  rG.aGradient.SetEndIntens(100);
198  }
199 
200  // calc the basic positions
201  const tools::Rectangle aObjectSnapRectangle(pObj->GetSnapRect());
202  const basegfx::B2DRange aRange = vcl::unotools::b2DRectangleFromRectangle(aObjectSnapRectangle);
203  const basegfx::B2DPoint aCenter(aRange.getCenter());
204  basegfx::B2DPoint aStartPos(rV.maPositionA);
205  basegfx::B2DPoint aEndPos(rV.maPositionB);
206 
207  switch(rG.aGradient.GetGradientStyle())
208  {
209  case css::awt::GradientStyle_LINEAR :
210  {
211  if(!bMoveSingle || !bMoveFirst)
212  {
213  basegfx::B2DVector aFullVec(aEndPos - aStartPos);
214 
215  if(bMoveSingle)
216  {
217  aFullVec = aEndPos - aCenter;
218  }
219 
220  aFullVec.normalize();
221 
222  double fNewFullAngle(basegfx::rad2deg(atan2(aFullVec.getY(), aFullVec.getX())));
223  fNewFullAngle *= -10.0;
224  fNewFullAngle += 900.0;
225 
226  // clip
227  while(fNewFullAngle < 0.0)
228  {
229  fNewFullAngle += 3600.0;
230  }
231 
232  while(fNewFullAngle >= 3600.0)
233  {
234  fNewFullAngle -= 3600.0;
235  }
236 
237  // to int and set
238  Degree10 nNewAngle( FRound(fNewFullAngle));
239 
240  if(nNewAngle != rGOld.aGradient.GetAngle())
241  {
242  rG.aGradient.SetAngle(nNewAngle);
243  }
244  }
245 
246  if(!bMoveSingle || bMoveFirst)
247  {
248  const basegfx::B2DVector aFullVec(aEndPos - aStartPos);
249  const basegfx::B2DPoint aBottomLeft(aRange.getMinX(), aRange.getMaximum().getY());
250  const basegfx::B2DPoint aTopLeft(aRange.getMinX(), aRange.getMinY());
251  const basegfx::B2DVector aOldVec(aBottomLeft - aTopLeft);
252  const double fFullLen(aFullVec.getLength());
253  const double fOldLen(aOldVec.getLength());
254  const double fNewBorder((fFullLen * 100.0) / fOldLen);
255  sal_Int32 nNewBorder(100 - FRound(fNewBorder));
256 
257  // clip
258  if(nNewBorder < 0)
259  {
260  nNewBorder = 0;
261  }
262 
263  if(nNewBorder > 100)
264  {
265  nNewBorder = 100;
266  }
267 
268  // set
269  if(nNewBorder != rG.aGradient.GetBorder())
270  {
271  rG.aGradient.SetBorder(static_cast<sal_uInt16>(nNewBorder));
272  }
273  }
274 
275  break;
276  }
277  case css::awt::GradientStyle_AXIAL :
278  {
279  if(!bMoveSingle || !bMoveFirst)
280  {
281  basegfx::B2DVector aFullVec(aEndPos - aCenter);
282  const basegfx::B2DVector aOldVec(basegfx::B2DPoint(aCenter.getX(), aRange.getMaximum().getY()) - aCenter);
283  const double fFullLen(aFullVec.getLength());
284  const double fOldLen(aOldVec.getLength());
285  const double fNewBorder((fFullLen * 100.0) / fOldLen);
286  sal_Int32 nNewBorder = 100 - FRound(fNewBorder);
287 
288  // clip
289  if(nNewBorder < 0)
290  {
291  nNewBorder = 0;
292  }
293 
294  if(nNewBorder > 100)
295  {
296  nNewBorder = 100;
297  }
298 
299  // set
300  if(nNewBorder != rG.aGradient.GetBorder())
301  {
302  rG.aGradient.SetBorder(static_cast<sal_uInt16>(nNewBorder));
303  }
304 
305  aFullVec.normalize();
306  double fNewFullAngle(basegfx::rad2deg(atan2(aFullVec.getY(), aFullVec.getX())));
307  fNewFullAngle *= -10.0;
308  fNewFullAngle += 900.0;
309 
310  // clip
311  while(fNewFullAngle < 0.0)
312  {
313  fNewFullAngle += 3600.0;
314  }
315 
316  while(fNewFullAngle >= 3600.0)
317  {
318  fNewFullAngle -= 3600.0;
319  }
320 
321  // to int and set
322  const Degree10 nNewAngle(FRound(fNewFullAngle));
323 
324  if(nNewAngle != rGOld.aGradient.GetAngle())
325  {
326  rG.aGradient.SetAngle(nNewAngle);
327  }
328  }
329 
330  break;
331  }
332  case css::awt::GradientStyle_RADIAL :
333  case css::awt::GradientStyle_SQUARE :
334  {
335  if(!bMoveSingle || !bMoveFirst)
336  {
337  const basegfx::B2DPoint aTopLeft(aRange.getMinX(), aRange.getMinY());
338  const basegfx::B2DPoint aOffset(aEndPos - aTopLeft);
339  sal_Int32 nNewXOffset(FRound((aOffset.getX() * 100.0) / aRange.getWidth()));
340  sal_Int32 nNewYOffset(FRound((aOffset.getY() * 100.0) / aRange.getHeight()));
341 
342  // clip
343  if(nNewXOffset < 0)
344  {
345  nNewXOffset = 0;
346  }
347 
348  if(nNewXOffset > 100)
349  {
350  nNewXOffset = 100;
351  }
352 
353  if(nNewYOffset < 0)
354  {
355  nNewYOffset = 0;
356  }
357 
358  if(nNewYOffset > 100)
359  {
360  nNewYOffset = 100;
361  }
362 
363  rG.aGradient.SetXOffset(static_cast<sal_uInt16>(nNewXOffset));
364  rG.aGradient.SetYOffset(static_cast<sal_uInt16>(nNewYOffset));
365 
366  aStartPos -= aOffset;
367  aEndPos -= aOffset;
368  }
369 
370  if(!bMoveSingle || bMoveFirst)
371  {
372  basegfx::B2DVector aFullVec(aStartPos - aEndPos);
373  const basegfx::B2DPoint aBottomLeft(aRange.getMinX(), aRange.getMaximum().getY());
374  const basegfx::B2DPoint aTopLeft(aRange.getMinX(), aRange.getMinY());
375  const basegfx::B2DVector aOldVec(aBottomLeft - aTopLeft);
376  const double fFullLen(aFullVec.getLength());
377  const double fOldLen(aOldVec.getLength());
378  const double fNewBorder((fFullLen * 100.0) / fOldLen);
379  sal_Int32 nNewBorder(100 - FRound(fNewBorder));
380 
381  // clip
382  if(nNewBorder < 0)
383  {
384  nNewBorder = 0;
385  }
386 
387  if(nNewBorder > 100)
388  {
389  nNewBorder = 100;
390  }
391 
392  // set
393  if(nNewBorder != rG.aGradient.GetBorder())
394  {
395  rG.aGradient.SetBorder(static_cast<sal_uInt16>(nNewBorder));
396  }
397 
398  // angle is not definitely necessary for these modes, but it makes
399  // controlling more fun for the user
400  aFullVec.normalize();
401  double fNewFullAngle(basegfx::rad2deg(atan2(aFullVec.getY(), aFullVec.getX())));
402  fNewFullAngle *= -10.0;
403  fNewFullAngle += 900.0;
404 
405  // clip
406  while(fNewFullAngle < 0.0)
407  {
408  fNewFullAngle += 3600.0;
409  }
410 
411  while(fNewFullAngle >= 3600.0)
412  {
413  fNewFullAngle -= 3600.0;
414  }
415 
416  // to int and set
417  const Degree10 nNewAngle(FRound(fNewFullAngle));
418 
419  if(nNewAngle != rGOld.aGradient.GetAngle())
420  {
421  rG.aGradient.SetAngle(nNewAngle);
422  }
423  }
424 
425  break;
426  }
427  case css::awt::GradientStyle_ELLIPTICAL :
428  case css::awt::GradientStyle_RECT :
429  {
430  if(!bMoveSingle || !bMoveFirst)
431  {
432  const basegfx::B2DPoint aTopLeft(aRange.getMinX(), aRange.getMinY());
433  const basegfx::B2DPoint aOffset(aEndPos - aTopLeft);
434  sal_Int32 nNewXOffset(FRound((aOffset.getX() * 100.0) / aRange.getWidth()));
435  sal_Int32 nNewYOffset(FRound((aOffset.getY() * 100.0) / aRange.getHeight()));
436 
437  // clip
438  if(nNewXOffset < 0)
439  {
440  nNewXOffset = 0;
441  }
442 
443  if(nNewXOffset > 100)
444  {
445  nNewXOffset = 100;
446  }
447 
448  if(nNewYOffset < 0)
449  {
450  nNewYOffset = 0;
451  }
452 
453  if(nNewYOffset > 100)
454  {
455  nNewYOffset = 100;
456  }
457 
458  rG.aGradient.SetXOffset(static_cast<sal_uInt16>(nNewXOffset));
459  rG.aGradient.SetYOffset(static_cast<sal_uInt16>(nNewYOffset));
460 
461  aStartPos -= aOffset;
462  aEndPos -= aOffset;
463  }
464 
465  if(!bMoveSingle || bMoveFirst)
466  {
467  basegfx::B2DVector aFullVec(aStartPos - aEndPos);
468  const basegfx::B2DPoint aTopLeft(aRange.getMinX(), aRange.getMinY());
469  const basegfx::B2DPoint aCenterLeft(aRange.getMinX(), aCenter.getY());
470  const basegfx::B2DVector aOldVec(aCenterLeft - aTopLeft);
471  const double fFullLen(aFullVec.getLength());
472  const double fOldLen(aOldVec.getLength());
473  const double fNewBorder((fFullLen * 100.0) / fOldLen);
474  sal_Int32 nNewBorder(100 - FRound(fNewBorder));
475 
476  // clip
477  if(nNewBorder < 0)
478  {
479  nNewBorder = 0;
480  }
481 
482  if(nNewBorder > 100)
483  {
484  nNewBorder = 100;
485  }
486 
487  // set
488  if(nNewBorder != rG.aGradient.GetBorder())
489  {
490  rG.aGradient.SetBorder(static_cast<sal_uInt16>(nNewBorder));
491  }
492 
493  // angle is not definitely necessary for these modes, but it makes
494  // controlling more fun for the user
495  aFullVec.normalize();
496  double fNewFullAngle(basegfx::rad2deg(atan2(aFullVec.getY(), aFullVec.getX())));
497  fNewFullAngle *= -10.0;
498  fNewFullAngle += 900.0;
499 
500  // clip
501  while(fNewFullAngle < 0.0)
502  {
503  fNewFullAngle += 3600.0;
504  }
505 
506  while(fNewFullAngle >= 3600.0)
507  {
508  fNewFullAngle -= 3600.0;
509  }
510 
511  // to int and set
512  const Degree10 nNewAngle(FRound(fNewFullAngle));
513 
514  if(nNewAngle != rGOld.aGradient.GetAngle())
515  {
516  rG.aGradient.SetAngle(nNewAngle);
517  }
518  }
519 
520  break;
521  }
522  default:
523  break;
524  }
525 }
526 
527 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XGradient aGradient
Definition: gradtrns.hxx:40
Degree10 GetAngle() const
Definition: xgrad.hxx:71
basegfx::B2DPoint maPositionB
Definition: gradtrns.hxx:32
void SetBorder(sal_uInt16 nNewBorder)
Definition: xgrad.hxx:61
double getX() const
static void VecToGrad(GradTransVector const &rV, GradTransGradient &rG, GradTransGradient const &rGOld, const SdrObject *pObj, bool bMoveSingle, bool bMoveFirst)
Definition: gradtrns.cxx:182
void SetEndColor(const Color &rColor)
Definition: xgrad.hxx:59
double getY() const
constexpr double rad2deg(double v)
sal_uInt16 GetStartIntens() const
Definition: xgrad.hxx:75
virtual const tools::Rectangle & GetSnapRect() const
Definition: svdobj.cxx:1598
const Color & GetStartColor() const
Definition: xgrad.hxx:69
sal_uInt16 GetYOffset() const
Definition: xgrad.hxx:74
B2DVector & normalize()
sal_uInt16 GetXOffset() const
Definition: xgrad.hxx:73
void SetEndIntens(sal_uInt16 nNewIntens)
Definition: xgrad.hxx:65
UNDERLYING_TYPE get() const
sal_uInt16 GetBorder() const
Definition: xgrad.hxx:72
void SetStartColor(const Color &rColor)
Definition: xgrad.hxx:58
tools::Long FRound(double fVal)
basegfx::B2DPoint maPositionA
Definition: gradtrns.hxx:31
void SetYOffset(sal_uInt16 nNewOffset)
Definition: xgrad.hxx:63
constexpr double deg2rad(double v)
Abstract DrawObject.
Definition: svdobj.hxx:260
basegfx::B2DRange b2DRectangleFromRectangle(const ::tools::Rectangle &rRect)
void SetStartIntens(sal_uInt16 nNewIntens)
Definition: xgrad.hxx:64
css::awt::GradientStyle GetGradientStyle() const
Definition: xgrad.hxx:68
void SetXOffset(sal_uInt16 nNewOffset)
Definition: xgrad.hxx:62
B2DHomMatrix createRotateAroundPoint(double fPointX, double fPointY, double fRadiant)
basegfx::BColor getBColor() const
double getLength() const
static void GradToVec(GradTransGradient const &rG, GradTransVector &rV, const SdrObject *pObj)
Definition: gradtrns.cxx:30
sal_uInt16 GetEndIntens() const
Definition: xgrad.hxx:76
void SetAngle(Degree10 nNewAngle)
Definition: xgrad.hxx:60
const Color & GetEndColor() const
Definition: xgrad.hxx:70