source: trunk/src/plugins/imageformats/svg/qsvgiohandler.cpp@ 595

Last change on this file since 595 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 5.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the plugins of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qsvgiohandler.h"
43
44#ifndef QT_NO_SVGRENDERER
45
46#include "qsvgrenderer.h"
47#include "qimage.h"
48#include "qpixmap.h"
49#include "qpainter.h"
50#include "qvariant.h"
51#include "qdebug.h"
52
53QT_BEGIN_NAMESPACE
54
55class QSvgIOHandlerPrivate
56{
57public:
58 QSvgIOHandlerPrivate()
59 : r(new QSvgRenderer()), loaded(false)
60 {}
61 ~QSvgIOHandlerPrivate()
62 {
63 delete r;
64 }
65
66 bool load(QIODevice *device);
67 static bool findSvgTag(QIODevice *device);
68
69 QSvgRenderer *r;
70 QSize defaultSize;
71 QSize currentSize;
72 bool loaded;
73};
74
75bool QSvgIOHandlerPrivate::load(QIODevice *device)
76{
77 if (loaded)
78 return true;
79
80 if (r->load(device->readAll())) {
81 defaultSize = QSize(r->viewBox().width(), r->viewBox().height());
82 if (currentSize.isEmpty())
83 currentSize = defaultSize;
84 }
85 loaded = r->isValid();
86
87 return loaded;
88}
89
90bool QSvgIOHandlerPrivate::findSvgTag(QIODevice *device)
91{
92 qint64 pos = device->pos();
93 device->seek(0);
94 char buffer[256];
95 const char svg_tag[] = "<svg";
96
97 while (1) {
98 int size = device->read(buffer, 256);
99 for (int i=0; i<size - 5; ++i) {
100 if (!memcmp(buffer + i, svg_tag, 4)) {
101 if (buffer[i+4] == ' ' || buffer[i+4] == '\t'
102 || buffer[i+4] == '\n' || buffer[i+4] == '\r')
103 {
104 device->seek(pos);
105 return true;
106 }
107 }
108 }
109 if (device->atEnd())
110 break;
111 device->seek(device->pos()-4);
112 }
113 device->seek(pos);
114 return false;
115}
116
117QSvgIOHandler::QSvgIOHandler()
118 : d(new QSvgIOHandlerPrivate())
119{
120
121}
122
123
124QSvgIOHandler::~QSvgIOHandler()
125{
126 delete d;
127}
128
129
130bool QSvgIOHandler::canRead() const
131{
132 return QSvgIOHandlerPrivate::findSvgTag(device());
133}
134
135
136QByteArray QSvgIOHandler::name() const
137{
138 return "svg";
139}
140
141
142bool QSvgIOHandler::read(QImage *image)
143{
144 if (d->load(device())) {
145 *image = QImage(d->currentSize, QImage::Format_ARGB32_Premultiplied);
146 if (!d->currentSize.isEmpty()) {
147 image->fill(0x00000000);
148 QPainter p(image);
149 d->r->render(&p);
150 p.end();
151 }
152 return true;
153 }
154
155 return false;
156}
157
158
159QVariant QSvgIOHandler::option(ImageOption option) const
160{
161 switch(option) {
162 case ImageFormat:
163 return QImage::Format_ARGB32_Premultiplied;
164 break;
165 case Size:
166 d->load(device());
167 return d->defaultSize;
168 break;
169 case ScaledSize:
170 return d->currentSize;
171 break;
172 default:
173 break;
174 }
175 return QVariant();
176}
177
178
179void QSvgIOHandler::setOption(ImageOption option, const QVariant & value)
180{
181 switch(option) {
182 case Size:
183 d->defaultSize = value.toSize();
184 d->currentSize = value.toSize();
185 break;
186 case ScaledSize:
187 d->currentSize = value.toSize();
188 break;
189 default:
190 break;
191 }
192}
193
194
195bool QSvgIOHandler::supportsOption(ImageOption option) const
196{
197 switch(option)
198 {
199 case ImageFormat:
200 case Size:
201 case ScaledSize:
202 return true;
203 default:
204 break;
205 }
206 return false;
207}
208
209bool QSvgIOHandler::canRead(QIODevice *device)
210{
211 return QSvgIOHandlerPrivate::findSvgTag(device);
212}
213
214QT_END_NAMESPACE
215
216#endif // QT_NO_SVGRENDERER
Note: See TracBrowser for help on using the repository browser.