15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright (C) Research In Motion Limited 2010. All rights reserved.
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * This library is free software; you can redistribute it and/or
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modify it under the terms of the GNU Library General Public
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * License as published by the Free Software Foundation; either
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * version 2 of the License, or (at your option) any later version.
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * This library is distributed in the hope that it will be useful,
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * but WITHOUT ANY WARRANTY; without even the implied warranty of
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Library General Public License for more details.
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * You should have received a copy of the GNU Library General Public License
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * along with this library; see the file COPYING.LIB.  If not, write to
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Boston, MA 02110-1301, USA.
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "config.h"
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
2253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/svg/SVGPathSegListSource.h"
2353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
2453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/svg/SVGPathSegArc.h"
2553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/svg/SVGPathSegCurvetoCubic.h"
2653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/svg/SVGPathSegCurvetoCubicSmooth.h"
2753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/svg/SVGPathSegCurvetoQuadratic.h"
2853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/svg/SVGPathSegLinetoHorizontal.h"
2953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/svg/SVGPathSegLinetoVertical.h"
305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)namespace WebCore {
325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
33d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)SVGPathSegListSource::SVGPathSegListSource(SVGPathSegList::ConstIterator itBegin, SVGPathSegList::ConstIterator itEnd)
34d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    : m_itCurrent(itBegin)
35d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    , m_itEnd(itEnd)
365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)bool SVGPathSegListSource::hasMoreData() const
405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
41d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    return m_itCurrent != m_itEnd;
425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
445c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)bool SVGPathSegListSource::parseSVGSegmentType(SVGPathSegType& pathSegType)
455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
46d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    m_segment = *m_itCurrent;
475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    pathSegType = static_cast<SVGPathSegType>(m_segment->pathSegType());
48d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    ++m_itCurrent;
495c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    return true;
505c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
515c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
525c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)SVGPathSegType SVGPathSegListSource::nextCommand(SVGPathSegType)
535c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
54d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    m_segment = *m_itCurrent;
555c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    SVGPathSegType pathSegType = static_cast<SVGPathSegType>(m_segment->pathSegType());
56d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    ++m_itCurrent;
575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    return pathSegType;
585c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
605c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)bool SVGPathSegListSource::parseMoveToSegment(FloatPoint& targetPoint)
615c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ASSERT(m_segment);
635c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ASSERT(m_segment->pathSegType() == PathSegMoveToAbs || m_segment->pathSegType() == PathSegMoveToRel);
645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    SVGPathSegSingleCoordinate* moveTo = static_cast<SVGPathSegSingleCoordinate*>(m_segment.get());
655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    targetPoint = FloatPoint(moveTo->x(), moveTo->y());
665c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    return true;
675c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
695c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)bool SVGPathSegListSource::parseLineToSegment(FloatPoint& targetPoint)
705c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
715c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ASSERT(m_segment);
725c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ASSERT(m_segment->pathSegType() == PathSegLineToAbs || m_segment->pathSegType() == PathSegLineToRel);
735c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    SVGPathSegSingleCoordinate* lineTo = static_cast<SVGPathSegSingleCoordinate*>(m_segment.get());
745c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    targetPoint = FloatPoint(lineTo->x(), lineTo->y());
755c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    return true;
765c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
775c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
785c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)bool SVGPathSegListSource::parseLineToHorizontalSegment(float& x)
795c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
805c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ASSERT(m_segment);
815c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ASSERT(m_segment->pathSegType() == PathSegLineToHorizontalAbs || m_segment->pathSegType() == PathSegLineToHorizontalRel);
825c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    SVGPathSegLinetoHorizontal* horizontal = static_cast<SVGPathSegLinetoHorizontal*>(m_segment.get());
835c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    x = horizontal->x();
845c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    return true;
855c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
865c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
875c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)bool SVGPathSegListSource::parseLineToVerticalSegment(float& y)
885c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
895c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ASSERT(m_segment);
905c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ASSERT(m_segment->pathSegType() == PathSegLineToVerticalAbs || m_segment->pathSegType() == PathSegLineToVerticalRel);
915c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    SVGPathSegLinetoVertical* vertical = static_cast<SVGPathSegLinetoVertical*>(m_segment.get());
925c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    y = vertical->y();
935c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    return true;
945c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
955c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
965c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)bool SVGPathSegListSource::parseCurveToCubicSegment(FloatPoint& point1, FloatPoint& point2, FloatPoint& targetPoint)
975c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
985c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ASSERT(m_segment);
995c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ASSERT(m_segment->pathSegType() == PathSegCurveToCubicAbs || m_segment->pathSegType() == PathSegCurveToCubicRel);
1005c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    SVGPathSegCurvetoCubic* cubic = static_cast<SVGPathSegCurvetoCubic*>(m_segment.get());
1015c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    point1 = FloatPoint(cubic->x1(), cubic->y1());
1025c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    point2 = FloatPoint(cubic->x2(), cubic->y2());
1035c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    targetPoint = FloatPoint(cubic->x(), cubic->y());
1045c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    return true;
1055c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
1065c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1075c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)bool SVGPathSegListSource::parseCurveToCubicSmoothSegment(FloatPoint& point2, FloatPoint& targetPoint)
1085c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
1095c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ASSERT(m_segment);
1105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ASSERT(m_segment->pathSegType() == PathSegCurveToCubicSmoothAbs || m_segment->pathSegType() == PathSegCurveToCubicSmoothRel);
1115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    SVGPathSegCurvetoCubicSmooth* cubicSmooth = static_cast<SVGPathSegCurvetoCubicSmooth*>(m_segment.get());
1125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    point2 = FloatPoint(cubicSmooth->x2(), cubicSmooth->y2());
1135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    targetPoint = FloatPoint(cubicSmooth->x(), cubicSmooth->y());
1145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    return true;
1155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
1165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)bool SVGPathSegListSource::parseCurveToQuadraticSegment(FloatPoint& point1, FloatPoint& targetPoint)
1185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
1195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ASSERT(m_segment);
1205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ASSERT(m_segment->pathSegType() == PathSegCurveToQuadraticAbs || m_segment->pathSegType() == PathSegCurveToQuadraticRel);
1215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    SVGPathSegCurvetoQuadratic* quadratic = static_cast<SVGPathSegCurvetoQuadratic*>(m_segment.get());
1225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    point1 = FloatPoint(quadratic->x1(), quadratic->y1());
1235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    targetPoint = FloatPoint(quadratic->x(), quadratic->y());
1245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    return true;
1255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
1265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)bool SVGPathSegListSource::parseCurveToQuadraticSmoothSegment(FloatPoint& targetPoint)
1285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
1295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ASSERT(m_segment);
1305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ASSERT(m_segment->pathSegType() == PathSegCurveToQuadraticSmoothAbs || m_segment->pathSegType() == PathSegCurveToQuadraticSmoothRel);
1315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    SVGPathSegSingleCoordinate* quadraticSmooth = static_cast<SVGPathSegSingleCoordinate*>(m_segment.get());
1325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    targetPoint = FloatPoint(quadraticSmooth->x(), quadraticSmooth->y());
1335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    return true;
1345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
1355c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)bool SVGPathSegListSource::parseArcToSegment(float& rx, float& ry, float& angle, bool& largeArc, bool& sweep, FloatPoint& targetPoint)
1375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
1385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ASSERT(m_segment);
1395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ASSERT(m_segment->pathSegType() == PathSegArcAbs || m_segment->pathSegType() == PathSegArcRel);
1405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    SVGPathSegArc* arcTo = static_cast<SVGPathSegArc*>(m_segment.get());
1415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    rx = arcTo->r1();
1425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ry = arcTo->r2();
1435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    angle = arcTo->angle();
1445c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    largeArc = arcTo->largeArcFlag();
1455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    sweep = arcTo->sweepFlag();
1465c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    targetPoint = FloatPoint(arcTo->x(), arcTo->y());
1475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    return true;
1485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
1495c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1505c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
151