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 : #ifndef INCLUDED_DRAWINGLAYER_PRIMITIVE2D_SVGGRADIENTPRIMITIVE2D_HXX
21 : #define INCLUDED_DRAWINGLAYER_PRIMITIVE2D_SVGGRADIENTPRIMITIVE2D_HXX
22 :
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>
30 :
31 : //////////////////////////////////////////////////////////////////////////////
32 : // SvgGradientEntry class
33 :
34 : namespace drawinglayer
35 : {
36 : namespace primitive2d
37 : {
38 : /// a single GradientStop defining a color and opacity at a distance
39 0 : class SvgGradientEntry
40 : {
41 : private:
42 : double mfOffset;
43 : basegfx::BColor maColor;
44 : double mfOpacity;
45 :
46 : public:
47 0 : SvgGradientEntry(double fOffset, const basegfx::BColor& rColor = basegfx::BColor(0.0, 0.0, 0.0), double fOpacity = 1.0)
48 : : mfOffset(fOffset),
49 : maColor(rColor),
50 0 : mfOpacity(fOpacity)
51 : {
52 0 : }
53 :
54 0 : double getOffset() const { return mfOffset; }
55 0 : const basegfx::BColor& getColor() const { return maColor; }
56 0 : double getOpacity() const { return mfOpacity; }
57 :
58 0 : bool operator==(const SvgGradientEntry& rCompare) const
59 : {
60 0 : return (getOffset() == rCompare.getOffset()
61 0 : && getColor() == getColor()
62 0 : && getOpacity() == getOpacity());
63 : }
64 :
65 0 : bool operator<(const SvgGradientEntry& rCompare) const
66 : {
67 0 : return getOffset() < rCompare.getOffset();
68 : }
69 : };
70 :
71 : typedef ::std::vector< SvgGradientEntry > SvgGradientEntryVector;
72 :
73 : } // end of namespace primitive2d
74 : } // end of namespace drawinglayer
75 :
76 : //////////////////////////////////////////////////////////////////////////////
77 : // SvgGradientHelper class
78 :
79 : namespace drawinglayer
80 : {
81 : namespace primitive2d
82 : {
83 : enum SpreadMethod
84 : {
85 : Spread_pad = 0,
86 : Spread_reflect,
87 : Spread_repeat
88 : };
89 :
90 : /* helper for linear and radial gradient, both get derived from this
91 : to share common definitions and functionality
92 : **/
93 : class SvgGradientHelper
94 : {
95 : private:
96 : /// geometric definition, the geometry to be filled
97 : basegfx::B2DPolyPolygon maPolyPolygon;
98 :
99 : /// the gradient definition
100 : SvgGradientEntryVector maGradientEntries;
101 :
102 : /// start and/or center point
103 : basegfx::B2DPoint maStart;
104 :
105 : /// how to spread
106 : SpreadMethod maSpreadMethod;
107 :
108 : /// bitfield
109 : bool mbPreconditionsChecked : 1;
110 : bool mbCreatesContent : 1;
111 : bool mbSingleEntry : 1;
112 : bool mbFullyOpaque : 1;
113 :
114 : protected:
115 : /// local helpers
116 : Primitive2DSequence createSingleGradientEntryFill() const;
117 : virtual void createAtom(
118 : Primitive2DVector& rTargetColor,
119 : Primitive2DVector& rTargetOpacity,
120 : const SvgGradientEntry& rFrom,
121 : const SvgGradientEntry& rTo,
122 : sal_Int32 nOffset) const = 0;
123 : double createRun(
124 : Primitive2DVector& rTargetColor,
125 : Primitive2DVector& rTargetOpacity,
126 : double fPos,
127 : double fMax,
128 : const SvgGradientEntryVector& rEntries,
129 : sal_Int32 nOffset) const;
130 : virtual void checkPreconditions();
131 : Primitive2DSequence createResult(
132 : const Primitive2DVector& rTargetColor,
133 : const Primitive2DVector& rTargetOpacity,
134 : const basegfx::B2DHomMatrix& rUnitGradientToObject,
135 : bool bInvert = false) const;
136 0 : bool getCreatesContent() const { return mbCreatesContent; }
137 0 : bool getSingleEntry() const { return mbSingleEntry; }
138 0 : void setSingleEntry() { mbSingleEntry = true; }
139 0 : bool getPreconditionsChecked() const { return mbPreconditionsChecked; }
140 : bool getFullyOpaque() const { return mbFullyOpaque; }
141 :
142 : public:
143 : /// constructor
144 : SvgGradientHelper(
145 : const basegfx::B2DPolyPolygon& rPolyPolygon,
146 : const SvgGradientEntryVector& rGradientEntries,
147 : const basegfx::B2DPoint& rStart,
148 : SpreadMethod aSpreadMethod = Spread_pad);
149 0 : virtual ~SvgGradientHelper() {}
150 :
151 : /// data read access
152 0 : const basegfx::B2DPolyPolygon& getPolyPolygon() const { return maPolyPolygon; }
153 0 : const SvgGradientEntryVector& getGradientEntries() const { return maGradientEntries; }
154 0 : const basegfx::B2DPoint& getStart() const { return maStart; }
155 0 : SpreadMethod getSpreadMethod() const { return maSpreadMethod; }
156 :
157 : /// compare operator
158 : bool equalTo(const SvgGradientHelper& rSvgGradientHelper) const;
159 : };
160 : } // end of namespace primitive2d
161 : } // end of namespace drawinglayer
162 :
163 : //////////////////////////////////////////////////////////////////////////////
164 : // SvgLinearGradientPrimitive2D class
165 :
166 : namespace drawinglayer
167 : {
168 : namespace primitive2d
169 : {
170 : /// the basic linear gradient primitive
171 : class DRAWINGLAYER_DLLPUBLIC SvgLinearGradientPrimitive2D : public BufferedDecompositionPrimitive2D, public SvgGradientHelper
172 : {
173 : private:
174 : /// the end point for linear gradient
175 : basegfx::B2DPoint maEnd;
176 :
177 : protected:
178 : /// local helpers
179 : virtual void createAtom(
180 : Primitive2DVector& rTargetColor,
181 : Primitive2DVector& rTargetOpacity,
182 : const SvgGradientEntry& rFrom,
183 : const SvgGradientEntry& rTo,
184 : sal_Int32 nOffset) const;
185 : virtual void checkPreconditions();
186 :
187 : /// local decomposition.
188 : virtual Primitive2DSequence create2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const;
189 :
190 : public:
191 : /// constructor
192 : SvgLinearGradientPrimitive2D(
193 : const basegfx::B2DPolyPolygon& rPolyPolygon,
194 : const SvgGradientEntryVector& rGradientEntries,
195 : const basegfx::B2DPoint& rStart,
196 : const basegfx::B2DPoint& rEnd,
197 : SpreadMethod aSpreadMethod = Spread_pad);
198 0 : virtual ~SvgLinearGradientPrimitive2D() {}
199 :
200 : /// data read access
201 0 : const basegfx::B2DPoint& getEnd() const { return maEnd; }
202 :
203 : /// compare operator
204 : virtual bool operator==(const BasePrimitive2D& rPrimitive) const;
205 :
206 : /// get range
207 : virtual basegfx::B2DRange getB2DRange(const geometry::ViewInformation2D& rViewInformation) const;
208 :
209 : /// provide unique ID
210 : DeclPrimitrive2DIDBlock()
211 : };
212 : } // end of namespace primitive2d
213 : } // end of namespace drawinglayer
214 :
215 : //////////////////////////////////////////////////////////////////////////////
216 : // SvgRadialGradientPrimitive2D class
217 :
218 : namespace drawinglayer
219 : {
220 : namespace primitive2d
221 : {
222 : /// the basic radial gradient primitive
223 0 : class DRAWINGLAYER_DLLPUBLIC SvgRadialGradientPrimitive2D : public BufferedDecompositionPrimitive2D, public SvgGradientHelper
224 : {
225 : private:
226 : /// the geometric definition
227 : double mfRadius;
228 :
229 : /// Focal only used when focal is set at all, see constructors
230 : basegfx::B2DPoint maFocal;
231 : basegfx::B2DVector maFocalVector;
232 : double maFocalLength;
233 :
234 : // internal helper for case Spread_reflect
235 : SvgGradientEntryVector maMirroredGradientEntries;
236 :
237 : /// bitfield
238 : bool mbFocalSet : 1;
239 :
240 : /// local helpers
241 : const SvgGradientEntryVector& getMirroredGradientEntries() const;
242 : void createMirroredGradientEntries();
243 :
244 : protected:
245 : /// local helpers
246 : virtual void createAtom(
247 : Primitive2DVector& rTargetColor,
248 : Primitive2DVector& rTargetOpacity,
249 : const SvgGradientEntry& rFrom,
250 : const SvgGradientEntry& rTo,
251 : sal_Int32 nOffset) const;
252 : virtual void checkPreconditions();
253 :
254 : /// local decomposition.
255 : virtual Primitive2DSequence create2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const;
256 :
257 : public:
258 : /// constructor
259 : SvgRadialGradientPrimitive2D(
260 : const basegfx::B2DPolyPolygon& rPolyPolygon,
261 : const SvgGradientEntryVector& rGradientEntries,
262 : const basegfx::B2DPoint& rStart,
263 : double fRadius,
264 : SpreadMethod aSpreadMethod = Spread_pad,
265 : const basegfx::B2DPoint* pFocal = 0);
266 :
267 : /// data read access
268 0 : double getRadius() const { return mfRadius; }
269 0 : const basegfx::B2DPoint& getFocal() const { return maFocal; }
270 0 : bool isFocalSet() const { return mbFocalSet; }
271 :
272 : /// compare operator
273 : virtual bool operator==(const BasePrimitive2D& rPrimitive) const;
274 :
275 : /// get range
276 : virtual basegfx::B2DRange getB2DRange(const geometry::ViewInformation2D& rViewInformation) const;
277 :
278 : /// provide unique ID
279 : DeclPrimitrive2DIDBlock()
280 : };
281 : } // end of namespace primitive2d
282 : } // end of namespace drawinglayer
283 :
284 : //////////////////////////////////////////////////////////////////////////////
285 : // SvgLinearAtomPrimitive2D class
286 :
287 : namespace drawinglayer
288 : {
289 : namespace primitive2d
290 : {
291 : /* basic primitive for a single linear GradientRun in unit coordiantes.
292 : It's derived from DiscreteMetricDependentPrimitive2D to allow view-dependent
293 : decompositions allowing reduced color steps
294 : **/
295 0 : class DRAWINGLAYER_DLLPUBLIC SvgLinearAtomPrimitive2D : public DiscreteMetricDependentPrimitive2D
296 : {
297 : private:
298 : /// the geometric definition in unit coordiantes
299 : basegfx::BColor maColorA;
300 : basegfx::BColor maColorB;
301 : double mfOffsetA;
302 : double mfOffsetB;
303 :
304 : protected:
305 :
306 : /// local decomposition.
307 : virtual Primitive2DSequence create2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const;
308 :
309 : public:
310 : /// constructor
311 : SvgLinearAtomPrimitive2D(
312 : const basegfx::BColor& aColorA, double fOffsetA,
313 : const basegfx::BColor& aColorB, double fOffsetB);
314 :
315 : /// data read access
316 0 : const basegfx::BColor& getColorA() const { return maColorA; }
317 0 : const basegfx::BColor& getColorB() const { return maColorB; }
318 0 : double getOffsetA() const { return mfOffsetA; }
319 0 : double getOffsetB() const { return mfOffsetB; }
320 :
321 : /// compare operator
322 : virtual bool operator==(const BasePrimitive2D& rPrimitive) const;
323 :
324 : /// provide unique ID
325 : DeclPrimitrive2DIDBlock()
326 : };
327 : } // end of namespace primitive2d
328 : } // end of namespace drawinglayer
329 :
330 : //////////////////////////////////////////////////////////////////////////////
331 : // SvgRadialAtomPrimitive2D class
332 :
333 : namespace drawinglayer
334 : {
335 : namespace primitive2d
336 : {
337 : /* basic primitive for a single radial GradientRun in unit coordiantes.
338 : It's derived from DiscreteMetricDependentPrimitive2D to allow view-dependent
339 : decompositions allowing reduced color steps
340 : **/
341 : class DRAWINGLAYER_DLLPUBLIC SvgRadialAtomPrimitive2D : public DiscreteMetricDependentPrimitive2D
342 : {
343 : private:
344 : /// the geometric definition in unit coordiantes
345 : basegfx::BColor maColorA;
346 : basegfx::BColor maColorB;
347 : double mfScaleA;
348 : double mfScaleB;
349 :
350 : // helper to hold translation vectors when given (for focal)
351 0 : struct VectorPair
352 : {
353 : basegfx::B2DVector maTranslateA;
354 : basegfx::B2DVector maTranslateB;
355 :
356 0 : VectorPair(const basegfx::B2DVector& rTranslateA, const basegfx::B2DVector& rTranslateB)
357 : : maTranslateA(rTranslateA),
358 0 : maTranslateB(rTranslateB)
359 : {
360 0 : }
361 : };
362 :
363 : /// Only used when focal is set
364 : VectorPair* mpTranslate;
365 :
366 : protected:
367 :
368 : /// local decomposition.
369 : virtual Primitive2DSequence create2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const;
370 :
371 : public:
372 : /// constructor
373 : SvgRadialAtomPrimitive2D(
374 : const basegfx::BColor& aColorA, double fScaleA, const basegfx::B2DVector& rTranslateA,
375 : const basegfx::BColor& aColorB, double fScaleB, const basegfx::B2DVector& rTranslateB);
376 : SvgRadialAtomPrimitive2D(
377 : const basegfx::BColor& aColorA, double fScaleA,
378 : const basegfx::BColor& aColorB, double fScaleB);
379 : virtual ~SvgRadialAtomPrimitive2D();
380 :
381 : /// data read access
382 0 : const basegfx::BColor& getColorA() const { return maColorA; }
383 0 : const basegfx::BColor& getColorB() const { return maColorB; }
384 0 : double getScaleA() const { return mfScaleA; }
385 0 : double getScaleB() const { return mfScaleB; }
386 0 : bool isTranslateSet() const { return (0 != mpTranslate); }
387 0 : basegfx::B2DVector getTranslateA() const { if(mpTranslate) return mpTranslate->maTranslateA; return basegfx::B2DVector(); }
388 0 : basegfx::B2DVector getTranslateB() const { if(mpTranslate) return mpTranslate->maTranslateB; return basegfx::B2DVector(); }
389 :
390 : /// compare operator
391 : virtual bool operator==(const BasePrimitive2D& rPrimitive) const;
392 :
393 : /// provide unique ID
394 : DeclPrimitrive2DIDBlock()
395 : };
396 : } // end of namespace primitive2d
397 : } // end of namespace drawinglayer
398 :
399 : //////////////////////////////////////////////////////////////////////////////
400 :
401 : #endif //INCLUDED_DRAWINGLAYER_PRIMITIVE2D_SVGGRADIENTPRIMITIVE2D_HXX
402 :
403 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|