public static List <List <Vector2> > GetPaths(SVGMatrix matrix, SVGPathElement svgElement) { lastCommand = SVGPathSegTypes.Unknown; List <Vector2> positionBuffer = new List <Vector2>(); List <List <Vector2> > output = new List <List <Vector2> >(); SVGPathSegList segList = svgElement.segList; for (int i = 0; i < segList.Count; i++) { GetSegment(svgElement, segList.GetItem(i), output, positionBuffer, matrix); } if (lastCommand != SVGPathSegTypes.Close && positionBuffer.Count > 0) { output.Add(new List <Vector2>(positionBuffer.ToArray())); } //Vector2 startPoint, endPoint; for (int i = 0; i < output.Count; i++) { // Ramer Douglas Peucker Reduction if (output[i] == null || output[i].Count < 3) { continue; } output[i] = SVGBezier.Optimise(output[i], SVGGraphics.vpm); } return(output); }
private void Initial() { string _d = _attrList.GetValue("d"); SVGPathSeg lastSegment = null; SVGPathSeg firstPathSegment = null; List <char> _charList = new List <char>(); List <string> _valueList = new List <string>(); SVGStringExtractor.ExtractPathSegList(_d, ref _charList, ref _valueList); _segList = new SVGPathSegList(_charList.Count); int i, j, paramsLength; for (i = 0; i < _charList.Count; i++) { //lastSegment = _segList.GetLastItem(); char _char = _charList [i]; string _value = _valueList [i]; float[] parms = SVGStringExtractor.ExtractTransformValueAsPX(_value); paramsLength = parms.Length; switch (_char) { case 'Z': case 'z': if (_segList.Count > 0 && firstPathSegment != null) { lastSegment = _segList.AppendItem(new SVGPathSegLinetoAbs(firstPathSegment.currentPoint.x, firstPathSegment.currentPoint.y, lastSegment)); } _segList.AppendItem(CreateSVGPathSegClosePath()); firstPathSegment = null; break; case 'M': if (lastSegment != null && lastSegment.type != SVGPathSegTypes.Close && lastSegment.type != SVGPathSegTypes.MoveTo_Abs && lastSegment.type != SVGPathSegTypes.MoveTo_Rel) { firstPathSegment = null; } if (paramsLength < 2) { continue; } for (j = 0; j < paramsLength; j += 2) { if (paramsLength - j < 2) { continue; } lastSegment = _segList.AppendItem(new SVGPathSegMovetoAbs(parms [j], parms [j + 1], lastSegment)); if (firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'm': if (lastSegment != null && lastSegment.type != SVGPathSegTypes.Close && lastSegment.type != SVGPathSegTypes.MoveTo_Abs && lastSegment.type != SVGPathSegTypes.MoveTo_Rel) { firstPathSegment = null; } if (paramsLength < 2) { continue; } for (j = 0; j < paramsLength; j += 2) { if (paramsLength - j < 2) { continue; } lastSegment = _segList.AppendItem(new SVGPathSegMovetoRel(parms [j], parms [j + 1], lastSegment)); if (firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'L': if (paramsLength < 2) { continue; } for (j = 0; j < paramsLength; j += 2) { if (paramsLength - j < 2) { continue; } lastSegment = _segList.AppendItem(new SVGPathSegLinetoAbs(parms [j], parms [j + 1], lastSegment)); if (firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'l': if (paramsLength < 2) { continue; } for (j = 0; j < paramsLength; j += 2) { if (paramsLength - j < 2) { continue; } lastSegment = _segList.AppendItem(new SVGPathSegLinetoRel(parms [j], parms [j + 1], lastSegment)); if (firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'C': if (paramsLength < 6) { continue; } for (j = 0; j < paramsLength; j += 6) { if (paramsLength - j < 6) { continue; } lastSegment = _segList.AppendItem(new SVGPathSegCurvetoCubicAbs(parms [j], parms [j + 1], parms [j + 2], parms [j + 3], parms [j + 4], parms [j + 5], lastSegment)); if (firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'c': if (paramsLength < 6) { continue; } for (j = 0; j < paramsLength; j += 6) { if (paramsLength - j < 6) { continue; } // Debug.Log(string.Format("CubicCurveRel: {0}, {1}, {2}, {3}, {4}, {5}", parms [j], parms [j + 1], parms [j + 2], parms [j + 3], parms [j + 4], parms [j + 5])); lastSegment = _segList.AppendItem(new SVGPathSegCurvetoCubicRel(parms [j], parms [j + 1], parms [j + 2], parms [j + 3], parms [j + 4], parms [j + 5], lastSegment)); if (firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'S': if (paramsLength < 4) { continue; } for (j = 0; j < paramsLength; j += 4) { if (paramsLength - j < 4) { continue; } lastSegment = _segList.AppendItem(new SVGPathSegCurvetoCubicSmoothAbs(parms [j], parms [j + 1], parms [j + 2], parms [j + 3], lastSegment)); if (firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 's': if (paramsLength < 4) { continue; } for (j = 0; j < paramsLength; j += 4) { if (paramsLength - j < 4) { continue; } lastSegment = _segList.AppendItem(new SVGPathSegCurvetoCubicSmoothRel(parms [j], parms [j + 1], parms [j + 2], parms [j + 3], lastSegment)); if (firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'Q': if (paramsLength < 4) { continue; } for (j = 0; j < paramsLength; j += 4) { if (paramsLength - j < 4) { continue; } lastSegment = _segList.AppendItem(new SVGPathSegCurvetoQuadraticAbs(parms [j], parms [j + 1], parms [j + 2], parms [j + 3], lastSegment)); if (firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'q': if (paramsLength < 4) { continue; } for (j = 0; j < paramsLength; j += 4) { if (paramsLength - j < 4) { continue; } lastSegment = _segList.AppendItem(new SVGPathSegCurvetoQuadraticRel(parms [j], parms [j + 1], parms [j + 2], parms [j + 3], lastSegment)); if (firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'T': if (paramsLength < 2) { continue; } for (j = 0; j < paramsLength; j += 2) { if (paramsLength - j < 2) { continue; } lastSegment = _segList.AppendItem(new SVGPathSegCurvetoQuadraticSmoothAbs(parms [j], parms [j + 1], lastSegment)); if (firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 't': if (paramsLength < 2) { continue; } for (j = 0; j < paramsLength; j += 2) { if (paramsLength - j < 2) { continue; } lastSegment = _segList.AppendItem(new SVGPathSegCurvetoQuadraticSmoothRel(parms [j], parms [j + 1], lastSegment)); if (firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'A': if (paramsLength < 7) { continue; } for (j = 0; j < paramsLength; j += 7) { if (paramsLength - j < 7) { continue; } lastSegment = _segList.AppendItem(new SVGPathSegArcAbs(parms [j], parms [j + 1], parms [j + 2], parms [j + 3] == 1f, parms [j + 4] == 1f, parms [j + 5], parms [j + 6], lastSegment)); if (firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'a': if (paramsLength < 7) { continue; } for (j = 0; j < paramsLength; j += 7) { if (paramsLength - j < 7) { continue; } lastSegment = _segList.AppendItem(new SVGPathSegArcRel(parms [j], parms [j + 1], parms [j + 2], parms [j + 3] == 1f, parms [j + 4] == 1f, parms [j + 5], parms [j + 6], lastSegment)); if (firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'H': for (j = 0; j < paramsLength; j++) { lastSegment = _segList.AppendItem(new SVGPathSegLinetoHorizontalAbs(parms [j], lastSegment)); if (firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'h': for (j = 0; j < paramsLength; j++) { lastSegment = _segList.AppendItem(new SVGPathSegLinetoHorizontalRel(parms [j], lastSegment)); if (firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'V': for (j = 0; j < paramsLength; j++) { lastSegment = _segList.AppendItem(new SVGPathSegLinetoVerticalAbs(parms [j], lastSegment)); if (firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'v': for (j = 0; j < paramsLength; j++) { lastSegment = _segList.AppendItem(new SVGPathSegLinetoVerticalRel(parms [j], lastSegment)); if (firstPathSegment == null) { firstPathSegment = lastSegment; } } break; } } }
private void Initial() { string _d = _attrList.GetValue("d"); SVGPathSeg lastSegment = null; SVGPathSeg firstPathSegment = null; List<char> _charList = new List<char>(); List<string> _valueList = new List<string>(); SVGStringExtractor.ExtractPathSegList(_d, ref _charList, ref _valueList); _segList = new SVGPathSegList(_charList.Count); int i, j, paramsLength; for (i = 0; i < _charList.Count; i++) { //lastSegment = _segList.GetLastItem(); char _char = _charList [i]; string _value = _valueList [i]; float[] parms = SVGStringExtractor.ExtractTransformValueAsPX(_value); paramsLength = parms.Length; //Debug.Log(_char+" "+_value); switch (_char) { case 'Z': case 'z': if(_segList.Count > 0 && firstPathSegment != null) { lastSegment = _segList.AppendItem(new SVGPathSegLinetoAbs(firstPathSegment.currentPoint.x, firstPathSegment.currentPoint.y, lastSegment)); } _segList.AppendItem(CreateSVGPathSegClosePath()); firstPathSegment = null; break; case 'M': if(lastSegment != null && lastSegment.type != SVGPathSegTypes.Close && lastSegment.type != SVGPathSegTypes.MoveTo_Abs && lastSegment.type != SVGPathSegTypes.MoveTo_Rel) { firstPathSegment = null; } if(paramsLength < 2) continue; for(j = 0; j < paramsLength; j+=2) { if(paramsLength - j < 2) continue; lastSegment = _segList.AppendItem(new SVGPathSegMovetoAbs(parms [j], parms [j + 1], lastSegment)); if(firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'm': if(lastSegment != null && lastSegment.type != SVGPathSegTypes.Close && lastSegment.type != SVGPathSegTypes.MoveTo_Abs && lastSegment.type != SVGPathSegTypes.MoveTo_Rel) { firstPathSegment = null; } if(paramsLength < 2) continue; for(j = 0; j < paramsLength; j+=2) { if(paramsLength - j < 2) continue; lastSegment = _segList.AppendItem(new SVGPathSegMovetoRel(parms [j], parms [j + 1], lastSegment)); if(firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'L': if(paramsLength < 2) continue; for(j = 0; j < paramsLength; j+=2) { if(paramsLength - j < 2) continue; lastSegment = _segList.AppendItem(new SVGPathSegLinetoAbs(parms [j], parms [j + 1], lastSegment)); if(firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'l': if(paramsLength < 2) continue; for(j = 0; j < paramsLength; j+=2) { if(paramsLength - j < 2) continue; lastSegment = _segList.AppendItem(new SVGPathSegLinetoRel(parms [j], parms [j + 1], lastSegment)); if(firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'C': if(paramsLength < 6) continue; for(j = 0; j < paramsLength; j+=6) { if(paramsLength - j < 6) continue; lastSegment = _segList.AppendItem(new SVGPathSegCurvetoCubicAbs(parms [j], parms [j + 1], parms [j + 2], parms [j + 3], parms [j + 4], parms [j + 5], lastSegment)); if(firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'c': if(paramsLength < 6) continue; for(j = 0; j < paramsLength; j+=6) { if(paramsLength - j < 6) continue; // Debug.Log(string.Format("CubicCurveRel: {0}, {1}, {2}, {3}, {4}, {5}", parms [j], parms [j + 1], parms [j + 2], parms [j + 3], parms [j + 4], parms [j + 5])); lastSegment = _segList.AppendItem(new SVGPathSegCurvetoCubicRel(parms [j], parms [j + 1], parms [j + 2], parms [j + 3], parms [j + 4], parms [j + 5], lastSegment)); if(firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'S': if(paramsLength < 4) continue; for(j = 0; j < paramsLength; j+=4) { if(paramsLength - j < 4) continue; lastSegment = _segList.AppendItem(new SVGPathSegCurvetoCubicSmoothAbs(parms [j], parms [j + 1], parms [j + 2], parms [j + 3], lastSegment)); if(firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 's': if(paramsLength < 4) continue; for(j = 0; j < paramsLength; j+=4) { if(paramsLength - j < 4) continue; lastSegment = _segList.AppendItem(new SVGPathSegCurvetoCubicSmoothRel(parms [j], parms [j + 1], parms [j + 2], parms [j + 3], lastSegment)); if(firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'Q': if(paramsLength < 4) continue; for(j = 0; j < paramsLength; j+=4) { if(paramsLength - j < 4) continue; lastSegment = _segList.AppendItem(new SVGPathSegCurvetoQuadraticAbs(parms [j], parms [j + 1], parms [j + 2], parms [j + 3], lastSegment)); if(firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'q': if(paramsLength < 4) continue; for(j = 0; j < paramsLength; j+=4) { if(paramsLength - j < 4) continue; lastSegment = _segList.AppendItem(new SVGPathSegCurvetoQuadraticRel(parms [j], parms [j + 1], parms [j + 2], parms [j + 3], lastSegment)); if(firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'T': if(paramsLength < 2) continue; for(j = 0; j < paramsLength; j+=2) { if(paramsLength - j < 2) continue; lastSegment = _segList.AppendItem(new SVGPathSegCurvetoQuadraticSmoothAbs(parms [j], parms [j + 1], lastSegment)); if(firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 't': if(paramsLength < 2) continue; for(j = 0; j < paramsLength; j+=2) { if(paramsLength - j < 2) continue; lastSegment = _segList.AppendItem(new SVGPathSegCurvetoQuadraticSmoothRel(parms [j], parms [j + 1], lastSegment)); if(firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'A': if(paramsLength < 7) continue; for(j = 0; j < paramsLength; j+=7) { if(paramsLength - j < 7) continue; lastSegment = _segList.AppendItem(new SVGPathSegArcAbs(parms [j], parms [j + 1], parms [j + 2], parms [j + 3] == 1f, parms [j + 4] == 1f, parms [j + 5], parms [j + 6], lastSegment)); if(firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'a': if(paramsLength < 7) continue; for(j = 0; j < paramsLength; j+=7) { if(paramsLength - j < 7) continue; lastSegment = _segList.AppendItem(new SVGPathSegArcRel(parms [j], parms [j + 1], parms [j + 2], parms [j + 3] == 1f, parms [j + 4] == 1f, parms [j + 5], parms [j + 6], lastSegment)); if(firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'H': for(j = 0; j < paramsLength; j++) { lastSegment = _segList.AppendItem(new SVGPathSegLinetoHorizontalAbs(parms [j], lastSegment)); if(firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'h': for(j = 0; j < paramsLength; j++) { lastSegment = _segList.AppendItem(new SVGPathSegLinetoHorizontalRel(parms [j], lastSegment)); if(firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'V': for(j = 0; j < paramsLength; j++) { lastSegment = _segList.AppendItem(new SVGPathSegLinetoVerticalAbs(parms [j], lastSegment)); if(firstPathSegment == null) { firstPathSegment = lastSegment; } } break; case 'v': for(j = 0; j < paramsLength; j++) { lastSegment = _segList.AppendItem(new SVGPathSegLinetoVerticalRel(parms [j], lastSegment)); if(firstPathSegment == null) { firstPathSegment = lastSegment; } } break; } } }
/***********************************************************************************/ internal void SetList(SVGPathSegList segList) { this._segList = segList; }