1
package latexDraw.parsers.svg.parsers;
3
import java.text.ParseException;
5
import latexDraw.parsers.svg.SVGLength;
6
import latexDraw.parsers.svg.SVGLength.LengthType;
9
* Defines a parser that parses SVG length code.<br>
11
* This file is part of LaTeXDraw.<br>
12
* Copyright (c) 2005-2008 Arnaud BLOUIN<br>
14
* LaTeXDraw is free software; you can redistribute it and/or modify
15
* it under the terms of the GNU General Public License as published by
16
* the Free Software Foundation; either version 2 of the License, or
17
* (at your option) any later version.<br>
19
* LaTeXDraw is distributed without any warranty; without even the
20
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
21
* PURPOSE. See the GNU General Public License for more details.<br>
24
* @author Arnaud BLOUIN<br>
28
public class SVGLengthParser extends SVGNumberParser
33
* @param code The code to parse.
36
public SVGLengthParser(String code)
44
* Parses an SVG length.
45
* @return An SVGLength.
46
* @throws ParseException If a problem occurs or if not managed unit are parsed (%, em and ex).
49
public SVGLength parseLength() throws ParseException
51
double value = parseNumber(false);
53
String errorUnit = "Invalid unit", valueAsStr;//$NON-NLS-1$
56
valueAsStr = parseNumberAsString(false);
62
lgthType = LengthType.NUMBER;
70
throw new ParseException(errorUnit, getPosition());
73
lgthType = LengthType.PT;
77
lgthType = LengthType.PX;
81
lgthType = LengthType.PC;
88
throw new ParseException("Not yet managed: %", getPosition());//$NON-NLS-1$
92
lgthType = LengthType.CM;
94
throw new ParseException(errorUnit, getPosition());
100
lgthType = LengthType.MM;
102
throw new ParseException(errorUnit, getPosition());
107
lgthType = LengthType.IN;
109
throw new ParseException(errorUnit, getPosition());
117
throw new ParseException(errorUnit, getPosition());
120
throw new ParseException("Not yet managed: em", getPosition());//$NON-NLS-1$
123
throw new ParseException("Not yet managed: ex", getPosition());//$NON-NLS-1$
127
throw new ParseException(errorUnit, getPosition());
130
return new SVGLength(UnitProcessor.toUserUnit(value, lgthType), lgthType, valueAsStr);
136
* Parses an SVG coordinate.
137
* @return An SVGLength.
138
* @throws ParseException If a problem occurs or if not managed unit are parsed (%, em and ex).
141
public SVGLength parseCoordinate() throws ParseException
143
return parseLength();
149
* Parses a number or a percentage (not yet managed).
150
* @return An SVGLength.
151
* @throws ParseException If a problem occurs or if a percentage is parsed.
154
public SVGLength parseNumberOrPercent() throws ParseException
156
double value = parseNumber(false);
161
valueAsStr = parseNumberAsString(false);
168
type = LengthType.NUMBER;
172
throw new ParseException("Not yet managed: %", getPosition());//$NON-NLS-1$
175
return new SVGLength(UnitProcessor.toUserUnit(value, type), type, valueAsStr);