| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) Research In Motion Limited 2010. All rights reserved. | 2 * Copyright (C) Research In Motion Limited 2010. All rights reserved. |
| 3 * Copyright (C) 2013 Apple Inc. All rights reserved. | 3 * Copyright (C) 2013 Apple Inc. All rights reserved. |
| 4 * | 4 * |
| 5 * This library is free software; you can redistribute it and/or | 5 * This library is free software; you can redistribute it and/or |
| 6 * modify it under the terms of the GNU Library General Public | 6 * modify it under the terms of the GNU Library General Public |
| 7 * License as published by the Free Software Foundation; either | 7 * License as published by the Free Software Foundation; either |
| 8 * version 2 of the License, or (at your option) any later version. | 8 * version 2 of the License, or (at your option) any later version. |
| 9 * | 9 * |
| 10 * This library is distributed in the hope that it will be useful, | 10 * This library is distributed in the hope that it will be useful, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 if (m_is8BitSource) { | 35 if (m_is8BitSource) { |
| 36 m_current.m_character8 = string.characters8(); | 36 m_current.m_character8 = string.characters8(); |
| 37 m_end.m_character8 = m_current.m_character8 + string.length(); | 37 m_end.m_character8 = m_current.m_character8 + string.length(); |
| 38 } else { | 38 } else { |
| 39 m_current.m_character16 = string.characters16(); | 39 m_current.m_character16 = string.characters16(); |
| 40 m_end.m_character16 = m_current.m_character16 + string.length(); | 40 m_end.m_character16 = m_current.m_character16 + string.length(); |
| 41 } | 41 } |
| 42 eatWhitespace(); | 42 eatWhitespace(); |
| 43 } | 43 } |
| 44 | 44 |
| 45 bool SVGPathStringSource::hasMoreData() const | |
| 46 { | |
| 47 if (m_is8BitSource) | |
| 48 return m_current.m_character8 < m_end.m_character8; | |
| 49 return m_current.m_character16 < m_end.m_character16; | |
| 50 } | |
| 51 | |
| 52 void SVGPathStringSource::eatWhitespace() | 45 void SVGPathStringSource::eatWhitespace() |
| 53 { | 46 { |
| 54 if (m_is8BitSource) | 47 if (m_is8BitSource) |
| 55 skipOptionalSVGSpaces(m_current.m_character8, m_end.m_character8); | 48 skipOptionalSVGSpaces(m_current.m_character8, m_end.m_character8); |
| 56 else | 49 else |
| 57 skipOptionalSVGSpaces(m_current.m_character16, m_end.m_character16); | 50 skipOptionalSVGSpaces(m_current.m_character16, m_end.m_character16); |
| 58 } | 51 } |
| 59 | 52 |
| 60 static SVGPathSegType parseSVGSegmentTypeHelper(unsigned lookahead) | 53 static SVGPathSegType mapLetterToSegmentType(unsigned lookahead) |
| 61 { | 54 { |
| 62 switch (lookahead) { | 55 switch (lookahead) { |
| 63 case 'Z': | 56 case 'Z': |
| 64 case 'z': | 57 case 'z': |
| 65 return PathSegClosePath; | 58 return PathSegClosePath; |
| 66 case 'M': | 59 case 'M': |
| 67 return PathSegMoveToAbs; | 60 return PathSegMoveToAbs; |
| 68 case 'm': | 61 case 'm': |
| 69 return PathSegMoveToRel; | 62 return PathSegMoveToRel; |
| 70 case 'L': | 63 case 'L': |
| (...skipping 26 matching lines...) Expand all Loading... |
| 97 return PathSegCurveToCubicSmoothRel; | 90 return PathSegCurveToCubicSmoothRel; |
| 98 case 'T': | 91 case 'T': |
| 99 return PathSegCurveToQuadraticSmoothAbs; | 92 return PathSegCurveToQuadraticSmoothAbs; |
| 100 case 't': | 93 case 't': |
| 101 return PathSegCurveToQuadraticSmoothRel; | 94 return PathSegCurveToQuadraticSmoothRel; |
| 102 default: | 95 default: |
| 103 return PathSegUnknown; | 96 return PathSegUnknown; |
| 104 } | 97 } |
| 105 } | 98 } |
| 106 | 99 |
| 107 static bool nextCommandHelper(unsigned lookahead, SVGPathSegType previousCommand
, SVGPathSegType& nextCommand) | 100 static bool isNumberStart(unsigned lookahead) |
| 108 { | 101 { |
| 109 // Check for remaining coordinates in the current command. | 102 return (lookahead >= '0' && lookahead <= '9') |
| 110 if ((lookahead == '+' || lookahead == '-' || lookahead == '.' || (lookahead
>= '0' && lookahead <= '9')) | 103 || lookahead == '+' |
| 111 && previousCommand != PathSegClosePath) { | 104 || lookahead == '-' |
| 112 if (previousCommand == PathSegMoveToAbs) { | 105 || lookahead == '.'; |
| 113 nextCommand = PathSegLineToAbs; | 106 } |
| 114 return true; | 107 |
| 115 } | 108 static bool maybeImplicitCommand(unsigned lookahead, SVGPathSegType previousComm
and, SVGPathSegType& nextCommand) |
| 116 if (previousCommand == PathSegMoveToRel) { | 109 { |
| 117 nextCommand = PathSegLineToRel; | 110 // Check if the current lookahead may start a number - in which case it |
| 118 return true; | 111 // could be the start of an implicit command. The 'close' command does not |
| 119 } | 112 // have any parameters though and hence can't have an implicit |
| 120 nextCommand = previousCommand; | 113 // 'continuation'. |
| 114 if (!isNumberStart(lookahead) || previousCommand == PathSegClosePath) |
| 115 return false; |
| 116 // Implicit continuations of moveto command translate to linetos. |
| 117 if (previousCommand == PathSegMoveToAbs) { |
| 118 nextCommand = PathSegLineToAbs; |
| 121 return true; | 119 return true; |
| 122 } | 120 } |
| 123 return false; | 121 if (previousCommand == PathSegMoveToRel) { |
| 122 nextCommand = PathSegLineToRel; |
| 123 return true; |
| 124 } |
| 125 nextCommand = previousCommand; |
| 126 return true; |
| 124 } | 127 } |
| 125 | 128 |
| 126 void SVGPathStringSource::setErrorMark(SVGParseStatus status) | 129 void SVGPathStringSource::setErrorMark(SVGParseStatus status) |
| 127 { | 130 { |
| 128 if (m_error.status() != SVGParseStatus::NoError) | 131 if (m_error.status() != SVGParseStatus::NoError) |
| 129 return; | 132 return; |
| 130 size_t locus = m_is8BitSource | 133 size_t locus = m_is8BitSource |
| 131 ? m_current.m_character8 - m_string.characters8() | 134 ? m_current.m_character8 - m_string.characters8() |
| 132 : m_current.m_character16 - m_string.characters16(); | 135 : m_current.m_character16 - m_string.characters16(); |
| 133 m_error = SVGParsingError(status, locus); | 136 m_error = SVGParsingError(status, locus); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 152 bool error; | 155 bool error; |
| 153 if (m_is8BitSource) | 156 if (m_is8BitSource) |
| 154 error = !parseArcFlag(m_current.m_character8, m_end.m_character8, flagVa
lue); | 157 error = !parseArcFlag(m_current.m_character8, m_end.m_character8, flagVa
lue); |
| 155 else | 158 else |
| 156 error = !parseArcFlag(m_current.m_character16, m_end.m_character16, flag
Value); | 159 error = !parseArcFlag(m_current.m_character16, m_end.m_character16, flag
Value); |
| 157 if (UNLIKELY(error)) | 160 if (UNLIKELY(error)) |
| 158 setErrorMark(SVGParseStatus::ExpectedArcFlag); | 161 setErrorMark(SVGParseStatus::ExpectedArcFlag); |
| 159 return flagValue; | 162 return flagValue; |
| 160 } | 163 } |
| 161 | 164 |
| 162 SVGPathSegType SVGPathStringSource::peekSegmentType() | |
| 163 { | |
| 164 ASSERT(hasMoreData()); | |
| 165 // This won't work in all cases because of the state required to "detect" im
plicit commands. | |
| 166 unsigned lookahead = m_is8BitSource ? *m_current.m_character8 : *m_current.m
_character16; | |
| 167 SVGPathSegType segmentType = parseSVGSegmentTypeHelper(lookahead); | |
| 168 // Here we assume that this is only called via SVGPathParser::initialCommand
IsMoveTo. | |
| 169 // TODO(fs): It ought to be possible to refactor away this entirely, and | |
| 170 // just handle this in parseSegment (which we sort of do already...) The | |
| 171 // only user is the method mentioned above. | |
| 172 if (segmentType != PathSegMoveToAbs && segmentType != PathSegMoveToRel) | |
| 173 setErrorMark(SVGParseStatus::ExpectedMoveToCommand); | |
| 174 return segmentType; | |
| 175 } | |
| 176 | |
| 177 PathSegmentData SVGPathStringSource::parseSegment() | 165 PathSegmentData SVGPathStringSource::parseSegment() |
| 178 { | 166 { |
| 179 ASSERT(hasMoreData()); | 167 ASSERT(hasMoreData()); |
| 180 PathSegmentData segment; | 168 PathSegmentData segment; |
| 181 unsigned lookahead = m_is8BitSource ? *m_current.m_character8 : *m_current.m
_character16; | 169 unsigned lookahead = m_is8BitSource ? *m_current.m_character8 : *m_current.m
_character16; |
| 182 SVGPathSegType command = parseSVGSegmentTypeHelper(lookahead); | 170 SVGPathSegType command = mapLetterToSegmentType(lookahead); |
| 183 if (command == PathSegUnknown) { | 171 if (UNLIKELY(m_previousCommand == PathSegUnknown)) { |
| 184 // Possibly an implicit command. Not allowed if this is the first comman
d. | 172 // First command has to be a moveto. |
| 185 if (m_previousCommand == PathSegUnknown) { | 173 if (command != PathSegMoveToRel && command != PathSegMoveToAbs) { |
| 186 setErrorMark(SVGParseStatus::ExpectedMoveToCommand); | 174 setErrorMark(SVGParseStatus::ExpectedMoveToCommand); |
| 187 return segment; | 175 return segment; |
| 188 } | 176 } |
| 189 if (!nextCommandHelper(lookahead, m_previousCommand, command)) { | 177 // Consume command letter. |
| 178 if (m_is8BitSource) |
| 179 m_current.m_character8++; |
| 180 else |
| 181 m_current.m_character16++; |
| 182 } else if (command == PathSegUnknown) { |
| 183 // Possibly an implicit command. |
| 184 ASSERT(m_previousCommand != PathSegUnknown); |
| 185 if (!maybeImplicitCommand(lookahead, m_previousCommand, command)) { |
| 190 setErrorMark(SVGParseStatus::ExpectedPathCommand); | 186 setErrorMark(SVGParseStatus::ExpectedPathCommand); |
| 191 return segment; | 187 return segment; |
| 192 } | 188 } |
| 193 } else { | 189 } else { |
| 194 // Valid explicit command. | 190 // Valid explicit command. |
| 195 if (m_is8BitSource) | 191 if (m_is8BitSource) |
| 196 m_current.m_character8++; | 192 m_current.m_character8++; |
| 197 else | 193 else |
| 198 m_current.m_character16++; | 194 m_current.m_character16++; |
| 199 } | 195 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 case PathSegUnknown: | 249 case PathSegUnknown: |
| 254 ASSERT_NOT_REACHED(); | 250 ASSERT_NOT_REACHED(); |
| 255 } | 251 } |
| 256 | 252 |
| 257 if (UNLIKELY(m_error.status() != SVGParseStatus::NoError)) | 253 if (UNLIKELY(m_error.status() != SVGParseStatus::NoError)) |
| 258 segment.command = PathSegUnknown; | 254 segment.command = PathSegUnknown; |
| 259 return segment; | 255 return segment; |
| 260 } | 256 } |
| 261 | 257 |
| 262 } // namespace blink | 258 } // namespace blink |
| OLD | NEW |