Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / drawinglayer / primitive2d / svggradientprimitive2d.hxx
blob8bef69e49825b14eddeb783e94e40a2e1e5f5511
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 #ifndef INCLUDED_DRAWINGLAYER_PRIMITIVE2D_SVGGRADIENTPRIMITIVE2D_HXX
21 #define INCLUDED_DRAWINGLAYER_PRIMITIVE2D_SVGGRADIENTPRIMITIVE2D_HXX
23 #include <drawinglayer/drawinglayerdllapi.h>
24 #include <drawinglayer/primitive2d/baseprimitive2d.hxx>
25 #include <basegfx/color/bcolor.hxx>
26 #include <basegfx/polygon/b2dpolypolygon.hxx>
27 #include <basegfx/matrix/b2dhommatrix.hxx>
28 #include <drawinglayer/primitive2d/primitivetools2d.hxx>
29 #include <vector>
32 // SvgGradientEntry class
34 namespace drawinglayer
36 namespace primitive2d
38 /// a single GradientStop defining a color and opacity at a distance
39 class SvgGradientEntry
41 private:
42 double mfOffset;
43 basegfx::BColor maColor;
44 double mfOpacity;
46 public:
47 SvgGradientEntry(double fOffset, const basegfx::BColor& rColor, double fOpacity)
48 : mfOffset(fOffset),
49 maColor(rColor),
50 mfOpacity(fOpacity)
54 double getOffset() const { return mfOffset; }
55 const basegfx::BColor& getColor() const { return maColor; }
56 double getOpacity() const { return mfOpacity; }
58 bool operator==(const SvgGradientEntry& rCompare) const
60 return (getOffset() == rCompare.getOffset()
61 && getColor() == rCompare.getColor()
62 && getOpacity() == rCompare.getOpacity());
65 bool operator<(const SvgGradientEntry& rCompare) const
67 return getOffset() < rCompare.getOffset();
71 typedef ::std::vector< SvgGradientEntry > SvgGradientEntryVector;
73 } // end of namespace primitive2d
74 } // end of namespace drawinglayer
77 // SvgGradientHelper class
79 namespace drawinglayer
81 namespace primitive2d
83 enum class SpreadMethod
85 Pad,
86 Reflect,
87 Repeat
90 /* helper for linear and radial gradient, both get derived from this
91 to share common definitions and functionality
92 **/
93 class SvgGradientHelper
95 private:
96 /// the extra gradient transform
97 basegfx::B2DHomMatrix maGradientTransform;
99 /// geometric definition, the geometry to be filled
100 basegfx::B2DPolyPolygon maPolyPolygon;
102 /// the gradient definition
103 SvgGradientEntryVector maGradientEntries;
105 /// start and/or center point
106 basegfx::B2DPoint maStart;
108 /// how to spread
109 SpreadMethod maSpreadMethod;
111 bool mbPreconditionsChecked : 1;
112 bool mbCreatesContent : 1;
113 bool mbSingleEntry : 1;
114 bool mbFullyOpaque : 1;
116 // true = interpret in unit coordinate system -> object aspect ratio will scale result
117 // false = interpret in object coordinate system -> object aspect ratio will not scale result
118 // (related to SVG's gradientUnits (userSpaceOnUse|objectBoundingBox)
119 bool mbUseUnitCoordinates : 1;
121 protected:
122 /// local helpers
123 void createSingleGradientEntryFill(Primitive2DContainer& rContainer) const;
124 virtual void createAtom(
125 Primitive2DContainer& rTargetColor,
126 Primitive2DContainer& rTargetOpacity,
127 const SvgGradientEntry& rFrom,
128 const SvgGradientEntry& rTo,
129 sal_Int32 nOffset) const = 0;
130 double createRun(
131 Primitive2DContainer& rTargetColor,
132 Primitive2DContainer& rTargetOpacity,
133 double fPos,
134 double fMax,
135 const SvgGradientEntryVector& rEntries,
136 sal_Int32 nOffset) const;
137 virtual void checkPreconditions();
138 void createResult(
139 Primitive2DContainer& rContainer,
140 const Primitive2DContainer& rTargetColor,
141 const Primitive2DContainer& rTargetOpacity,
142 const basegfx::B2DHomMatrix& rUnitGradientToObject,
143 bool bInvert = false) const;
144 bool getCreatesContent() const { return mbCreatesContent; }
145 bool getSingleEntry() const { return mbSingleEntry; }
146 void setSingleEntry() { mbSingleEntry = true; }
147 bool getPreconditionsChecked() const { return mbPreconditionsChecked; }
148 bool getFullyOpaque() const { return mbFullyOpaque; }
150 public:
151 /// constructor
152 SvgGradientHelper(
153 const basegfx::B2DHomMatrix& rGradientTransform,
154 const basegfx::B2DPolyPolygon& rPolyPolygon,
155 const SvgGradientEntryVector& rGradientEntries,
156 const basegfx::B2DPoint& rStart,
157 bool bUseUnitCoordinates,
158 SpreadMethod aSpreadMethod);
159 virtual ~SvgGradientHelper();
161 /// data read access
162 const basegfx::B2DHomMatrix& getGradientTransform() const { return maGradientTransform; }
163 const basegfx::B2DPolyPolygon& getPolyPolygon() const { return maPolyPolygon; }
164 const SvgGradientEntryVector& getGradientEntries() const { return maGradientEntries; }
165 const basegfx::B2DPoint& getStart() const { return maStart; }
166 bool getUseUnitCoordinates() const { return mbUseUnitCoordinates; }
167 SpreadMethod getSpreadMethod() const { return maSpreadMethod; }
169 /// compare operator
170 bool operator==(const SvgGradientHelper& rSvgGradientHelper) const;
172 } // end of namespace primitive2d
173 } // end of namespace drawinglayer
176 // SvgLinearGradientPrimitive2D class
178 namespace drawinglayer
180 namespace primitive2d
182 /// the basic linear gradient primitive
183 class DRAWINGLAYER_DLLPUBLIC SvgLinearGradientPrimitive2D : public BufferedDecompositionPrimitive2D, public SvgGradientHelper
185 private:
186 /// the end point for linear gradient
187 basegfx::B2DPoint maEnd;
189 protected:
190 /// local helpers
191 virtual void createAtom(
192 Primitive2DContainer& rTargetColor,
193 Primitive2DContainer& rTargetOpacity,
194 const SvgGradientEntry& rFrom,
195 const SvgGradientEntry& rTo,
196 sal_Int32 nOffset) const override;
197 virtual void checkPreconditions() override;
199 /// local decomposition.
200 virtual void create2DDecomposition(Primitive2DContainer& rContainer, const geometry::ViewInformation2D& rViewInformation) const override;
202 public:
203 /// constructor
204 SvgLinearGradientPrimitive2D(
205 const basegfx::B2DHomMatrix& rGradientTransform,
206 const basegfx::B2DPolyPolygon& rPolyPolygon,
207 const SvgGradientEntryVector& rGradientEntries,
208 const basegfx::B2DPoint& rStart,
209 const basegfx::B2DPoint& rEnd,
210 bool bUseUnitCoordinates,
211 SpreadMethod aSpreadMethod);
212 virtual ~SvgLinearGradientPrimitive2D() override;
214 /// data read access
215 const basegfx::B2DPoint& getEnd() const { return maEnd; }
217 /// compare operator
218 virtual bool operator==(const BasePrimitive2D& rPrimitive) const override;
220 /// get range
221 virtual basegfx::B2DRange getB2DRange(const geometry::ViewInformation2D& rViewInformation) const override;
223 /// provide unique ID
224 DeclPrimitive2DIDBlock()
226 } // end of namespace primitive2d
227 } // end of namespace drawinglayer
230 // SvgRadialGradientPrimitive2D class
232 namespace drawinglayer
234 namespace primitive2d
236 /// the basic radial gradient primitive
237 class DRAWINGLAYER_DLLPUBLIC SvgRadialGradientPrimitive2D : public BufferedDecompositionPrimitive2D, public SvgGradientHelper
239 private:
240 /// the geometric definition
241 double mfRadius;
243 /// Focal only used when focal is set at all, see constructors
244 basegfx::B2DPoint maFocal;
245 basegfx::B2DVector maFocalVector;
246 double maFocalLength;
248 // internal helper for case SpreadMethod::Reflect
249 SvgGradientEntryVector maMirroredGradientEntries;
251 bool mbFocalSet : 1;
253 /// local helpers
254 const SvgGradientEntryVector& getMirroredGradientEntries() const;
255 void createMirroredGradientEntries();
257 protected:
258 /// local helpers
259 virtual void createAtom(
260 Primitive2DContainer& rTargetColor,
261 Primitive2DContainer& rTargetOpacity,
262 const SvgGradientEntry& rFrom,
263 const SvgGradientEntry& rTo,
264 sal_Int32 nOffset) const override;
265 virtual void checkPreconditions() override;
267 /// local decomposition.
268 virtual void create2DDecomposition(Primitive2DContainer& rContainer, const geometry::ViewInformation2D& rViewInformation) const override;
270 public:
271 /// constructor
272 SvgRadialGradientPrimitive2D(
273 const basegfx::B2DHomMatrix& rGradientTransform,
274 const basegfx::B2DPolyPolygon& rPolyPolygon,
275 const SvgGradientEntryVector& rGradientEntries,
276 const basegfx::B2DPoint& rStart,
277 double fRadius,
278 bool bUseUnitCoordinates,
279 SpreadMethod aSpreadMethod,
280 const basegfx::B2DPoint* pFocal);
281 virtual ~SvgRadialGradientPrimitive2D() override;
283 /// data read access
284 double getRadius() const { return mfRadius; }
285 const basegfx::B2DPoint& getFocal() const { return maFocal; }
286 bool isFocalSet() const { return mbFocalSet; }
288 /// compare operator
289 virtual bool operator==(const BasePrimitive2D& rPrimitive) const override;
291 /// get range
292 virtual basegfx::B2DRange getB2DRange(const geometry::ViewInformation2D& rViewInformation) const override;
294 /// provide unique ID
295 DeclPrimitive2DIDBlock()
297 } // end of namespace primitive2d
298 } // end of namespace drawinglayer
301 // SvgLinearAtomPrimitive2D class
303 namespace drawinglayer
305 namespace primitive2d
307 /* basic primitive for a single linear GradientRun in unit coordinates.
308 It's derived from DiscreteMetricDependentPrimitive2D to allow view-dependent
309 decompositions allowing reduced color steps
311 class DRAWINGLAYER_DLLPUBLIC SvgLinearAtomPrimitive2D : public DiscreteMetricDependentPrimitive2D
313 private:
314 /// the geometric definition in unit coordinates
315 basegfx::BColor maColorA;
316 basegfx::BColor maColorB;
317 double mfOffsetA;
318 double mfOffsetB;
320 protected:
322 /// local decomposition.
323 virtual void create2DDecomposition(Primitive2DContainer& rContainer, const geometry::ViewInformation2D& rViewInformation) const override;
325 public:
326 /// constructor
327 SvgLinearAtomPrimitive2D(
328 const basegfx::BColor& aColorA, double fOffsetA,
329 const basegfx::BColor& aColorB, double fOffsetB);
331 /// data read access
332 const basegfx::BColor& getColorA() const { return maColorA; }
333 const basegfx::BColor& getColorB() const { return maColorB; }
334 double getOffsetA() const { return mfOffsetA; }
335 double getOffsetB() const { return mfOffsetB; }
337 /// compare operator
338 virtual bool operator==(const BasePrimitive2D& rPrimitive) const override;
340 /// provide unique ID
341 DeclPrimitive2DIDBlock()
343 } // end of namespace primitive2d
344 } // end of namespace drawinglayer
347 // SvgRadialAtomPrimitive2D class
349 namespace drawinglayer
351 namespace primitive2d
353 /* basic primitive for a single radial GradientRun in unit coordinates.
354 It's derived from DiscreteMetricDependentPrimitive2D to allow view-dependent
355 decompositions allowing reduced color steps
357 class DRAWINGLAYER_DLLPUBLIC SvgRadialAtomPrimitive2D : public DiscreteMetricDependentPrimitive2D
359 private:
360 /// the geometric definition in unit coordinates
361 basegfx::BColor maColorA;
362 basegfx::BColor maColorB;
363 double mfScaleA;
364 double mfScaleB;
366 // helper to hold translation vectors when given (for focal)
367 struct VectorPair
369 basegfx::B2DVector maTranslateA;
370 basegfx::B2DVector maTranslateB;
372 VectorPair(const basegfx::B2DVector& rTranslateA, const basegfx::B2DVector& rTranslateB)
373 : maTranslateA(rTranslateA),
374 maTranslateB(rTranslateB)
379 /// Only used when focal is set
380 VectorPair* mpTranslate;
382 protected:
384 /// local decomposition.
385 virtual void create2DDecomposition(Primitive2DContainer& rContainer, const geometry::ViewInformation2D& rViewInformation) const override;
387 public:
388 /// constructor
389 SvgRadialAtomPrimitive2D(
390 const basegfx::BColor& aColorA, double fScaleA, const basegfx::B2DVector& rTranslateA,
391 const basegfx::BColor& aColorB, double fScaleB, const basegfx::B2DVector& rTranslateB);
392 SvgRadialAtomPrimitive2D(
393 const basegfx::BColor& aColorA, double fScaleA,
394 const basegfx::BColor& aColorB, double fScaleB);
395 virtual ~SvgRadialAtomPrimitive2D() override;
397 /// data read access
398 const basegfx::BColor& getColorA() const { return maColorA; }
399 const basegfx::BColor& getColorB() const { return maColorB; }
400 double getScaleA() const { return mfScaleA; }
401 double getScaleB() const { return mfScaleB; }
402 bool isTranslateSet() const { return (nullptr != mpTranslate); }
403 basegfx::B2DVector getTranslateA() const { if(mpTranslate) return mpTranslate->maTranslateA; return basegfx::B2DVector(); }
404 basegfx::B2DVector getTranslateB() const { if(mpTranslate) return mpTranslate->maTranslateB; return basegfx::B2DVector(); }
406 /// compare operator
407 virtual bool operator==(const BasePrimitive2D& rPrimitive) const override;
409 /// provide unique ID
410 DeclPrimitive2DIDBlock()
412 } // end of namespace primitive2d
413 } // end of namespace drawinglayer
416 #endif //INCLUDED_DRAWINGLAYER_PRIMITIVE2D_SVGGRADIENTPRIMITIVE2D_HXX
418 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */