Home | History | Annotate | Download | only in svg
      1 /*
      2  * Copyright (C) 2004, 2005, 2008 Nikolas Zimmermann <zimmermann (at) kde.org>
      3  * Copyright (C) 2004, 2005, 2006, 2007, 2010 Rob Buis <buis (at) kde.org>
      4  *
      5  * This library is free software; you can redistribute it and/or
      6  * modify it under the terms of the GNU Library General Public
      7  * License as published by the Free Software Foundation; either
      8  * version 2 of the License, or (at your option) any later version.
      9  *
     10  * This library is distributed in the hope that it will be useful,
     11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13  * Library General Public License for more details.
     14  *
     15  * You should have received a copy of the GNU Library General Public License
     16  * along with this library; see the file COPYING.LIB.  If not, write to
     17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     18  * Boston, MA 02110-1301, USA.
     19  */
     20 
     21 #include "config.h"
     22 
     23 #if ENABLE(SVG)
     24 #include "SVGFitToViewBox.h"
     25 
     26 #include "AffineTransform.h"
     27 #include "Attr.h"
     28 #include "Document.h"
     29 #include "FloatRect.h"
     30 #include "SVGDocumentExtensions.h"
     31 #include "SVGNames.h"
     32 #include "SVGParserUtilities.h"
     33 #include "SVGPreserveAspectRatio.h"
     34 #include <wtf/text/StringImpl.h>
     35 
     36 namespace WebCore {
     37 
     38 bool SVGFitToViewBox::parseViewBox(Document* doc, const String& s, FloatRect& viewBox)
     39 {
     40     const UChar* c = s.characters();
     41     const UChar* end = c + s.length();
     42     return parseViewBox(doc, c, end, viewBox, true);
     43 }
     44 
     45 bool SVGFitToViewBox::parseViewBox(Document* doc, const UChar*& c, const UChar* end, FloatRect& viewBox, bool validate)
     46 {
     47     String str(c, end - c);
     48 
     49     skipOptionalSpaces(c, end);
     50 
     51     float x = 0.0f;
     52     float y = 0.0f;
     53     float width = 0.0f;
     54     float height = 0.0f;
     55     bool valid = parseNumber(c, end, x) && parseNumber(c, end, y) && parseNumber(c, end, width) && parseNumber(c, end, height, false);
     56     if (!validate) {
     57         viewBox = FloatRect(x, y, width, height);
     58         return true;
     59     }
     60     if (!valid) {
     61         doc->accessSVGExtensions()->reportWarning("Problem parsing viewBox=\"" + str + "\"");
     62         return false;
     63     }
     64 
     65     if (width < 0.0) { // check that width is positive
     66         doc->accessSVGExtensions()->reportError("A negative value for ViewBox width is not allowed");
     67         return false;
     68     }
     69     if (height < 0.0) { // check that height is positive
     70         doc->accessSVGExtensions()->reportError("A negative value for ViewBox height is not allowed");
     71         return false;
     72     }
     73     skipOptionalSpaces(c, end);
     74     if (c < end) { // nothing should come after the last, fourth number
     75         doc->accessSVGExtensions()->reportWarning("Problem parsing viewBox=\"" + str + "\"");
     76         return false;
     77     }
     78 
     79     viewBox = FloatRect(x, y, width, height);
     80     return true;
     81 }
     82 
     83 AffineTransform SVGFitToViewBox::viewBoxToViewTransform(const FloatRect& viewBoxRect, const SVGPreserveAspectRatio& preserveAspectRatio, float viewWidth, float viewHeight)
     84 {
     85     if (!viewBoxRect.width() || !viewBoxRect.height())
     86         return AffineTransform();
     87 
     88     return preserveAspectRatio.getCTM(viewBoxRect.x(), viewBoxRect.y(), viewBoxRect.width(), viewBoxRect.height(), viewWidth, viewHeight);
     89 }
     90 
     91 bool SVGFitToViewBox::parseMappedAttribute(Document* document, Attribute* attr)
     92 {
     93     if (attr->name() == SVGNames::viewBoxAttr) {
     94         FloatRect viewBox;
     95         if (!attr->value().isNull()) {
     96             if (!parseViewBox(document, attr->value(), viewBox))
     97                 return true;
     98         }
     99         setViewBoxBaseValue(viewBox);
    100     } else if (attr->name() == SVGNames::preserveAspectRatioAttr) {
    101         SVGPreserveAspectRatio::parsePreserveAspectRatio(this, attr->value());
    102         return true;
    103     }
    104 
    105     return false;
    106 }
    107 
    108 bool SVGFitToViewBox::isKnownAttribute(const QualifiedName& attrName)
    109 {
    110     return attrName == SVGNames::viewBoxAttr || attrName == SVGNames::preserveAspectRatioAttr;
    111 }
    112 
    113 }
    114 
    115 #endif // ENABLE(SVG)
    116