/// <summary> /// Parses the data in string specified and returns the list of commands found therein /// </summary> public static SvgPathSegList Parse(string markup) { if (markup == null) { throw new ArgumentNullException(nameof(markup)); } var result = new SvgPathSegList(); var buffer = new StringBuilder(); var lastCommand = '\0'; var args = new float[7]; var stream = new MarkupStringStream(markup); while (!stream.IsAtEnd) { if (!stream.SkipWhitespaceUntilNextToken()) { break; } var isNewCommand = char.IsLetter(stream.Peek); var command = lastCommand = isNewCommand ? stream.Read() : lastCommand; var argCount = GetPathCommandArgs(command); for (var arg = 0; arg < argCount; arg++) { buffer.Clear(); stream.SkipWhitespaceUntilNextToken(); var gotDot = false; while (!stream.IsAtEnd && IsNumeric(stream.Peek)) { var c = stream.Peek; if (((c == '-' || c == '+') && buffer.Length > 0) || (c == '.' && gotDot)) { var last = buffer[buffer.Length - 1]; if (last != 'e' && last != 'E') { break; } } gotDot |= c == '.'; buffer.Append(stream.Read()); } args[arg] = float.Parse(buffer.ToString()); } result.Add(CreatePathSegment(command, isNewCommand, args)); } return(result); }
/// <summary> /// Converts all relative path segments in the specified list to absolute path /// segments and returns the transformed list /// </summary> /// <param name="list">list of path segments to transform</param> /// <returns>transformed list of path segments where all relative path segments have been converted into their absolute counterparts</returns> public static SvgPathSegList ConvertToAbsolute(SvgPathSegList list) { if (list == null) { throw new ArgumentNullException(nameof(list)); } var transformer = new SvgPathSegRelativeToAbsoluteConverter(); return(new SvgPathSegList(list.Select(item => item.Accept(transformer)))); }
/// <summary> /// Converts all the curves in the path segment list to <see cref="SvgPathSegCurvetoCubicAbs">cubic bezier curves</see> /// </summary> /// <param name="list">list of path segments to transform</param> /// <returns>transformed list of path segments where all curves have been replaced with cubic bezier curves</returns> public static SvgPathSegList ConvertAllLinesAndCurvesToCubicCurves(SvgPathSegList list) { if (list == null) { throw new ArgumentNullException(nameof(list)); } var transformer = new SvgPathSegLinesAndCurvestoCubicCurvesConverter(); foreach (var item in list) { item.Accept(transformer); } transformer.Flush(); return(transformer.Result); }
/// <summary> /// Multiplies all path segments in the specified list by the specified matrix. If there are any relative path segments in the /// list then they will be converted to their absolute counterparts. /// </summary> /// <param name="list">list of path segments to transform</param> /// <param name="matrix">matrix to multiply the path segment coordinates by</param> /// <returns>transformed list of path segments where all coordinates have been multiplied by the specified matrix</returns> public static SvgPathSegList MultiplyByMatrix(SvgPathSegList list, SvgMatrix matrix) { if (list == null) { throw new ArgumentNullException(nameof(list)); } if (matrix == null) { throw new ArgumentNullException(nameof(matrix)); } if (list.Any(i => RelativePathSegTypes.Contains(i.PathSegType))) { list = ConvertToAbsolute(list); } var transformer = new SvgPathSegMatrixTransformer(matrix); return(new SvgPathSegList(list.Select(item => item.Accept(transformer)))); }
/// <summary> /// Multiplies all path segments in the specified list by the specified matrix. If there are any relative path segments in the /// list then they will be converted to their absolute counterparts. /// </summary> /// <param name="list">list of path segments to transform</param> /// <param name="matrix">matrix to multiply the path segment coordinates by</param> /// <returns>transformed list of path segments where all coordinates have been multiplied by the specified matrix</returns> public static SvgPathSegList MultiplyByMatrix(this SvgPathSegList list, SvgMatrix matrix) => SvgPathSegListTransformer.MultiplyByMatrix(list, matrix);
/// <summary> /// Converts all the curves in the path segment list to <see cref="SvgPathSegCurvetoCubicAbs">cubic bezier curves</see> /// </summary> /// <param name="list">list of path segments to transform</param> /// <returns>transformed list of path segments where all curves have been replaced with cubic bezier curves</returns> public static SvgPathSegList ConvertAllLinesAndCurvesToCubicCurves(this SvgPathSegList list) => SvgPathSegListTransformer.ConvertAllLinesAndCurvesToCubicCurves(list);
/// <summary> /// Converts all relative path segments in the specified list to their absolute counterparts and returns the new list. /// </summary> /// <param name="list">list of path segments to transform</param> /// <returns>transformed list of path segments where all relative path segments have been converted into their absolute counterparts</returns> public static SvgPathSegList ConvertToAbsolute(this SvgPathSegList list) => SvgPathSegListTransformer.ConvertToAbsolute(list);