source: trunk/src/svg/qsvgstyle_p.h@ 504

Last change on this file since 504 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 15.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtSvg module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QSVGSTYLE_P_H
43#define QSVGSTYLE_P_H
44
45//
46// W A R N I N G
47// -------------
48//
49// This file is not part of the Qt API. It exists purely as an
50// implementation detail. This header file may change from version to
51// version without notice, or even be removed.
52//
53// We mean it.
54//
55
56#include "QtGui/qpainter.h"
57
58#ifndef QT_NO_SVG
59
60#include "QtGui/qpen.h"
61#include "QtGui/qbrush.h"
62#include "QtGui/qmatrix.h"
63#include "QtGui/qcolor.h"
64#include "QtGui/qfont.h"
65#include <qdebug.h>
66
67QT_BEGIN_NAMESPACE
68
69class QPainter;
70class QSvgNode;
71class QSvgFont;
72class QSvgTinyDocument;
73
74template <class T> class QSvgRefCounter
75{
76public:
77 QSvgRefCounter() { t = 0; }
78 QSvgRefCounter(T *_t)
79 {
80 t = _t;
81 if (t)
82 t->ref();
83 }
84 QSvgRefCounter(const QSvgRefCounter &other)
85 {
86 t = other.t;
87 if (t)
88 t->ref();
89 }
90 QSvgRefCounter &operator =(T *_t)
91 {
92 if(_t)
93 _t->ref();
94 if (t)
95 t->deref();
96 t = _t;
97 return *this;
98 }
99 QSvgRefCounter &operator =(const QSvgRefCounter &other)
100 {
101 if(other.t)
102 other.t->ref();
103 if (t)
104 t->deref();
105 t = other.t;
106 return *this;
107 }
108 ~QSvgRefCounter()
109 {
110 if (t)
111 t->deref();
112 }
113
114 inline T *operator->() const { return t; }
115 inline operator T*() const { return t; }
116
117private:
118 T *t;
119};
120
121class QSvgRefCounted
122{
123public:
124 QSvgRefCounted() { _ref = 0; }
125 virtual ~QSvgRefCounted() {}
126 void ref() {
127 ++_ref;
128// qDebug() << this << ": adding ref, now " << _ref;
129 }
130 void deref() {
131// qDebug() << this << ": removing ref, now " << _ref;
132 if(!--_ref) {
133// qDebug(" deleting");
134 delete this;
135 }
136 }
137private:
138 int _ref;
139};
140
141struct QSvgExtraStates
142{
143 QSvgExtraStates();
144 qreal fillOpacity;
145};
146
147class QSvgStyleProperty : public QSvgRefCounted
148{
149public:
150 enum Type
151 {
152 QUALITY,
153 FILL,
154 VIEWPORT_FILL,
155 FONT,
156 STROKE,
157 SOLID_COLOR,
158 GRADIENT,
159 TRANSFORM,
160 ANIMATE_TRANSFORM,
161 ANIMATE_COLOR,
162 OPACITY,
163 COMP_OP
164 };
165public:
166 virtual ~QSvgStyleProperty();
167 virtual void apply(QPainter *p, const QRectF &, QSvgNode *node, QSvgExtraStates &states) =0;
168 virtual void revert(QPainter *p, QSvgExtraStates &states) =0;
169 virtual Type type() const=0;
170};
171
172class QSvgQualityStyle : public QSvgStyleProperty
173{
174public:
175 QSvgQualityStyle(int color);
176 virtual void apply(QPainter *p, const QRectF &, QSvgNode *node, QSvgExtraStates &states);
177 virtual void revert(QPainter *p, QSvgExtraStates &states);
178 virtual Type type() const;
179private:
180 // color-render ing v v 'auto' | 'optimizeSpeed' |
181 // 'optimizeQuality' | 'inherit'
182 int m_colorRendering;
183
184 // shape-rendering v v 'auto' | 'optimizeSpeed' | 'crispEdges' |
185 // 'geometricPrecision' | 'inherit'
186 //QSvgShapeRendering m_shapeRendering;
187
188
189 // text-rendering v v 'auto' | 'optimizeSpeed' | 'optimizeLegibility'
190 // | 'geometricPrecision' | 'inherit'
191 //QSvgTextRendering m_textRendering;
192
193
194 // vector-effect v x 'default' | 'non-scaling-stroke' | 'inherit'
195 //QSvgVectorEffect m_vectorEffect;
196
197 // image-rendering v v 'auto' | 'optimizeSpeed' | 'optimizeQuality' |
198 // 'inherit'
199 //QSvgImageRendering m_imageRendering;
200};
201
202
203
204class QSvgOpacityStyle : public QSvgStyleProperty
205{
206public:
207 QSvgOpacityStyle(qreal opacity);
208 virtual void apply(QPainter *p, const QRectF &, QSvgNode *node, QSvgExtraStates &states);
209 virtual void revert(QPainter *p, QSvgExtraStates &states);
210 virtual Type type() const;
211private:
212 qreal m_opacity;
213 qreal m_oldOpacity;
214};
215
216class QSvgFillStyle : public QSvgStyleProperty
217{
218public:
219 QSvgFillStyle(const QBrush &brush);
220 QSvgFillStyle(QSvgStyleProperty *style);
221 virtual void apply(QPainter *p, const QRectF &, QSvgNode *node, QSvgExtraStates &states);
222 virtual void revert(QPainter *p, QSvgExtraStates &states);
223 virtual Type type() const;
224
225 void setFillRule(Qt::FillRule f);
226 void setFillOpacity(qreal opacity);
227
228 const QBrush & qbrush() const
229 {
230 return m_fill;
231 }
232private:
233 // fill v v 'inherit' | <Paint.datatype>
234 // fill-opacity v v 'inherit' | <OpacityValue.datatype>
235 QBrush m_fill;
236 QBrush m_oldFill;
237 QSvgStyleProperty *m_style;
238
239 bool m_fillRuleSet;
240 Qt::FillRule m_fillRule;
241 bool m_fillOpacitySet;
242 qreal m_fillOpacity;
243 qreal m_oldOpacity;
244};
245
246class QSvgViewportFillStyle : public QSvgStyleProperty
247{
248public:
249 QSvgViewportFillStyle(const QBrush &brush);
250 virtual void apply(QPainter *p, const QRectF &, QSvgNode *node, QSvgExtraStates &states);
251 virtual void revert(QPainter *p, QSvgExtraStates &states);
252 virtual Type type() const;
253
254 const QBrush & qbrush() const
255 {
256 return m_viewportFill;
257 }
258private:
259 // viewport-fill v x 'inherit' | <Paint.datatype>
260 // viewport-fill-opacity v x 'inherit' | <OpacityValue.datatype>
261 QBrush m_viewportFill;
262
263 QBrush m_oldFill;
264};
265
266class QSvgFontStyle : public QSvgStyleProperty
267{
268public:
269 QSvgFontStyle(QSvgFont *font, QSvgTinyDocument *doc);
270 QSvgFontStyle(const QFont &font, QSvgTinyDocument *doc);
271 virtual void apply(QPainter *p, const QRectF &, QSvgNode *node, QSvgExtraStates &states);
272 virtual void revert(QPainter *p, QSvgExtraStates &states);
273 virtual Type type() const;
274
275 void setPointSize(qreal size);
276 qreal pointSize() const;
277
278 //### hack to avoid having a separate style element for text-anchor
279 QString textAnchor() const;
280 void setTextAnchor(const QString &anchor);
281
282 QSvgFont * svgFont() const
283 {
284 return m_font;
285 }
286 QSvgTinyDocument *doc() const
287 {
288 return m_doc;
289 }
290
291 const QFont & qfont() const
292 {
293 return m_qfont;
294 }
295private:
296 QSvgFont *m_font;
297 qreal m_pointSize;
298 QSvgTinyDocument *m_doc;
299
300 QString m_textAnchor;
301
302 QFont m_qfont;
303 QFont m_oldFont;
304};
305
306class QSvgStrokeStyle : public QSvgStyleProperty
307{
308public:
309 QSvgStrokeStyle(const QPen &pen);
310 virtual void apply(QPainter *p, const QRectF &, QSvgNode *node, QSvgExtraStates &states);
311 virtual void revert(QPainter *p, QSvgExtraStates &states);
312 virtual Type type() const;
313
314 const QPen & qpen() const
315 {
316 return m_stroke;
317 }
318private:
319 // stroke v v 'inherit' | <Paint.datatype>
320 // stroke-dasharray v v 'inherit' | <StrokeDashArrayValue.datatype>
321 // stroke-dashoffset v v 'inherit' | <StrokeDashOffsetValue.datatype>
322 // stroke-linecap v v 'butt' | 'round' | 'square' | 'inherit'
323 // stroke-linejoin v v 'miter' | 'round' | 'bevel' | 'inherit'
324 // stroke-miterlimit v v 'inherit' | <StrokeMiterLimitValue.datatype>
325 // stroke-opacity v v 'inherit' | <OpacityValue.datatype>
326 // stroke-width v v 'inherit' | <StrokeWidthValue.datatype>
327 QPen m_stroke;
328
329 QPen m_oldStroke;
330};
331
332
333class QSvgSolidColorStyle : public QSvgStyleProperty
334{
335public:
336 QSvgSolidColorStyle(const QColor &color);
337 virtual void apply(QPainter *p, const QRectF &, QSvgNode *node, QSvgExtraStates &states);
338 virtual void revert(QPainter *p, QSvgExtraStates &states);
339 virtual Type type() const;
340
341 const QColor & qcolor() const
342 {
343 return m_solidColor;
344 }
345private:
346 // solid-color v x 'inherit' | <SVGColor.datatype>
347 // solid-opacity v x 'inherit' | <OpacityValue.datatype>
348 QColor m_solidColor;
349
350 QBrush m_oldFill;
351 QPen m_oldStroke;
352};
353
354class QSvgGradientStyle : public QSvgStyleProperty
355{
356public:
357 QSvgGradientStyle(QGradient *grad);
358 ~QSvgGradientStyle() { delete m_gradient; }
359 virtual void apply(QPainter *p, const QRectF &, QSvgNode *node, QSvgExtraStates &states);
360 virtual void revert(QPainter *p, QSvgExtraStates &states);
361 virtual Type type() const;
362
363 void setStopLink(const QString &link, QSvgTinyDocument *doc);
364 QString stopLink() const { return m_link; }
365 void resolveStops();
366
367 void setMatrix(const QMatrix &matrix);
368 QMatrix qmatrix() const
369 {
370 return m_matrix;
371 }
372
373 QGradient *qgradient() const
374 {
375 return m_gradient;
376 }
377
378 void addResolve(qreal offset);
379
380 bool gradientStopsSet() const
381 {
382 return m_gradientStopsSet;
383 }
384
385 void setGradientStopsSet(bool set)
386 {
387 m_gradientStopsSet = set;
388 }
389private:
390 QGradient *m_gradient;
391 QList<qreal> m_resolvePoints;
392
393 QBrush m_oldFill;
394
395 QMatrix m_matrix;
396
397 QSvgTinyDocument *m_doc;
398 QString m_link;
399 bool m_gradientStopsSet;
400};
401
402class QSvgTransformStyle : public QSvgStyleProperty
403{
404public:
405 QSvgTransformStyle(const QTransform &transform);
406 virtual void apply(QPainter *p, const QRectF &, QSvgNode *node, QSvgExtraStates &states);
407 virtual void revert(QPainter *p, QSvgExtraStates &states);
408 virtual Type type() const;
409
410 const QTransform & qtransform() const
411 {
412 return m_transform;
413 }
414private:
415 //7.6 The transform attribute
416 QTransform m_transform;
417 QTransform m_oldWorldTransform;
418};
419
420
421class QSvgAnimateTransform : public QSvgStyleProperty
422{
423public:
424 enum TransformType
425 {
426 Empty,
427 Translate,
428 Scale,
429 Rotate,
430 SkewX,
431 SkewY
432 };
433public:
434 QSvgAnimateTransform(int startMs, int endMs, int by = 0);
435 void setArgs(TransformType type, const QVector<qreal> &args);
436 void setFreeze(bool freeze);
437 void setRepeatCount(qreal repeatCount);
438 virtual void apply(QPainter *p, const QRectF &, QSvgNode *node, QSvgExtraStates &states);
439 virtual void revert(QPainter *p, QSvgExtraStates &states);
440 virtual Type type() const;
441protected:
442 void resolveMatrix(QSvgNode *node);
443private:
444 qreal m_from, m_to, m_by;
445 qreal m_totalRunningTime;
446 TransformType m_type;
447 QVector<qreal> m_args;
448 int m_count;
449 QTransform m_transform;
450 QTransform m_oldWorldTransform;
451 bool m_finished;
452 bool m_freeze;
453 qreal m_repeatCount;
454};
455
456
457class QSvgAnimateColor : public QSvgStyleProperty
458{
459public:
460 QSvgAnimateColor(int startMs, int endMs, int by = 0);
461 void setArgs(bool fill, const QList<QColor> &colors);
462 void setFreeze(bool freeze);
463 void setRepeatCount(qreal repeatCount);
464 virtual void apply(QPainter *p, const QRectF &, QSvgNode *node, QSvgExtraStates &states);
465 virtual void revert(QPainter *p, QSvgExtraStates &states);
466 virtual Type type() const;
467private:
468 qreal m_from, m_to, m_by;
469 qreal m_totalRunningTime;
470 QList<QColor> m_colors;
471 QBrush m_oldBrush;
472 QPen m_oldPen;
473 bool m_fill;
474 bool m_finished;
475 bool m_freeze;
476 qreal m_repeatCount;
477};
478
479
480class QSvgCompOpStyle : public QSvgStyleProperty
481{
482public:
483 QSvgCompOpStyle(QPainter::CompositionMode mode);
484 virtual void apply(QPainter *p, const QRectF &, QSvgNode *node, QSvgExtraStates &states);
485 virtual void revert(QPainter *p, QSvgExtraStates &states);
486 virtual Type type() const;
487
488 const QPainter::CompositionMode & compOp() const
489 {
490 return m_mode;
491 }
492private:
493 //comp-op attribute
494 QPainter::CompositionMode m_mode;
495
496 QPainter::CompositionMode m_oldMode;
497};
498
499
500class QSvgStyle
501{
502public:
503 QSvgStyle()
504 : quality(0),
505 fill(0),
506 viewportFill(0),
507 font(0),
508 stroke(0),
509 solidColor(0),
510 gradient(0),
511 transform(0),
512 animateColor(0),
513 opacity(0),
514 compop(0)
515 {}
516 ~QSvgStyle();
517
518 void apply(QPainter *p, const QRectF &rect, QSvgNode *node, QSvgExtraStates &states);
519 void revert(QPainter *p, QSvgExtraStates &states);
520 QSvgRefCounter<QSvgQualityStyle> quality;
521 QSvgRefCounter<QSvgFillStyle> fill;
522 QSvgRefCounter<QSvgViewportFillStyle> viewportFill;
523 QSvgRefCounter<QSvgFontStyle> font;
524 QSvgRefCounter<QSvgStrokeStyle> stroke;
525 QSvgRefCounter<QSvgSolidColorStyle> solidColor;
526 QSvgRefCounter<QSvgGradientStyle> gradient;
527 QSvgRefCounter<QSvgTransformStyle> transform;
528 QSvgRefCounter<QSvgAnimateColor> animateColor;
529 QList<QSvgRefCounter<QSvgAnimateTransform> > animateTransforms;
530 QSvgRefCounter<QSvgOpacityStyle> opacity;
531 QSvgRefCounter<QSvgCompOpStyle> compop;
532};
533
534/********************************************************/
535// NOT implemented:
536
537// color v v 'inherit' | <Color.datatype>
538//QColor m_color;
539
540// display v x 'inline' | 'block' | 'list-item'
541// | 'run-in' | 'compact' | 'marker' |
542// 'table' | 'inline-table' |
543// 'table-row-group' | 'table-header-group' |
544// 'table-footer-group' | 'table-row' |
545// 'table-column-group' | 'table-column' |
546// 'table-cell' | 'table-caption' |
547// 'none' | 'inherit'
548//QSvgDisplayStyle m_display;
549
550// display-align v v 'auto' | 'before' | 'center' | 'after' | 'inherit'
551//QSvgDisplayAlign m_displayAlign;
552
553// line-increment v v 'auto' | 'inherit' | <Number.datatype>
554//int m_lineIncrement;
555
556// text-anchor v v 'start' | 'middle' | 'end' | 'inherit'
557//QSvgTextAnchor m_textAnchor;
558
559// visibility v v 'visible' | 'hidden' | 'inherit'
560//QSvgVisibility m_visibility;
561
562/******************************************************/
563// the following do not make sense for us
564
565// pointer-events v v 'visiblePainted' | 'visibleFill' | 'visibleStroke' |
566// 'visible' | 'painted' | 'fill' | 'stroke' | 'all' |
567// 'none' | 'inherit'
568//QSvgPointEvents m_pointerEvents;
569
570// audio-level v x 'inherit' | <Number.datatype>
571
572QT_END_NAMESPACE
573
574#endif // QT_NO_SVG
575#endif // QSVGSTYLE_P_H
Note: See TracBrowser for help on using the repository browser.