/// <summary> /// Tries to parse a vector attribute on an XML node. /// </summary> /// <param name="lexer">The lexer.</param> /// <param name="diagnostics">The diagnostics handler.</param> /// <param name="result">The result.</param> /// <returns>Returns <c>true</c> if the coordinate was parsed; otherwise, <c>false</c>.</returns> public static bool TryParseVector(this SvgPathDataLexer lexer, IDiagnosticHandler diagnostics, Vector2 defaultValue, out Vector2 result) { bool success = true; success &= TryParseCoordinate(lexer, diagnostics, defaultValue.X, out double x); success &= TryParseCoordinate(lexer, diagnostics, defaultValue.Y, out double y); result = new(x, y); return(success); }
/// <summary> /// Tries to parse an optional coordinate. /// </summary> /// <param name="lexer">The lexer.</param> /// <param name="diagnostics">The diagnostics.</param> /// <param name="defaultValue">The default value.</param> /// <param name="result">The result.</param> /// <returns>Returns <c>true</c> if the coordinate was parsed; otherwise, <c>false</c>.</returns> public static bool TryParseCoordinate(this SvgPathDataLexer lexer, IDiagnosticHandler diagnostics, double defaultValue, out double result) { if (lexer.Branch(TokenType.Number, out var value)) { if (!double.TryParse(value.Content.ToString(), NumberStyles.Float, Culture, out result)) { diagnostics?.Post(new DiagnosticMessage(SeverityLevel.Warning, "DRAW001", $"Expected coordinate")); return(false); } return(true); } else { result = defaultValue; return(false); } }