src/plugins/imageformats/svg/qsvgiohandler.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the plugins of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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 
       
    53 QT_BEGIN_NAMESPACE
       
    54 
       
    55 class QSvgIOHandlerPrivate
       
    56 {
       
    57 public:
       
    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 
       
    75 bool 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 
       
    90 bool 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 
       
   117 QSvgIOHandler::QSvgIOHandler()
       
   118     : d(new QSvgIOHandlerPrivate())
       
   119 {
       
   120 
       
   121 }
       
   122 
       
   123 
       
   124 QSvgIOHandler::~QSvgIOHandler()
       
   125 {
       
   126     delete d;
       
   127 }
       
   128 
       
   129 
       
   130 bool QSvgIOHandler::canRead() const
       
   131 {
       
   132     return QSvgIOHandlerPrivate::findSvgTag(device());
       
   133 }
       
   134 
       
   135 
       
   136 QByteArray QSvgIOHandler::name() const
       
   137 {
       
   138     return "svg";
       
   139 }
       
   140 
       
   141 
       
   142 bool 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 
       
   159 QVariant 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 
       
   179 void 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 
       
   195 bool 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 
       
   209 bool QSvgIOHandler::canRead(QIODevice *device)
       
   210 {
       
   211     return QSvgIOHandlerPrivate::findSvgTag(device);
       
   212 }
       
   213 
       
   214 QT_END_NAMESPACE
       
   215 
       
   216 #endif // QT_NO_SVGRENDERER