/// <summary> /// Initializes a new instance of the <see cref="SvgElement"/> class. /// </summary> public SvgElement() { this._children = new SvgElementCollection(this); //this._eventHandlers = new EventHandlerList(); this._elementName = string.Empty; this._customAttributes = new SvgCustomAttributeCollection(this); Transforms = new SvgTransformCollection(); //subscribe to attribute events Attributes.AttributeChanged += Attributes_AttributeChanged; CustomAttributes.AttributeChanged += Attributes_AttributeChanged; var typeInfo = this.GetType().GetTypeInfo(); ////find svg attribute descriptions _svgPropertyAttributes = from PropertyInfo a in typeInfo.DeclaredProperties let attribute = a.GetCustomAttribute <SvgAttributeAttribute>() where attribute != null select new PropertyAttributeTuple(a, attribute); _svgEventAttributes = from EventInfo a in typeInfo.DeclaredEvents let attribute = a.GetCustomAttribute <SvgAttributeAttribute>() where attribute != null select new EventAttributeTuple { Event = a, Attribute = attribute }; }
/// <summary> /// Initializes a new instance of the <see cref="SvgElement"/> class. /// </summary> public SvgElement() { this._children = new SvgElementCollection(this); this._eventHandlers = new EventHandlerList(); this._elementName = string.Empty; this._customAttributes = new SvgCustomAttributeCollection(this); Transforms = new SvgTransformCollection(); //subscribe to attribute events Attributes.AttributeChanged += Attributes_AttributeChanged; CustomAttributes.AttributeChanged += Attributes_AttributeChanged; //find svg attribute descriptions _svgPropertyAttributes = from PropertyDescriptor a in TypeDescriptor.GetProperties(this) let attribute = a.Attributes[typeof(SvgAttributeAttribute)] as SvgAttributeAttribute where attribute != null select new PropertyAttributeTuple { Property = a, Attribute = attribute }; _svgEventAttributes = from EventDescriptor a in TypeDescriptor.GetEvents(this) let attribute = a.Attributes[typeof(SvgAttributeAttribute)] as SvgAttributeAttribute where attribute != null select new EventAttributeTuple { Event = a.ComponentType.GetField(a.Name, BindingFlags.Instance | BindingFlags.NonPublic), Attribute = attribute }; }
/// <summary> /// Recursive method to add up the paths of all children /// </summary> /// <param name="elem"></param> /// <param name="path"></param> protected void AddPaths(SvgElement elem, GraphicsPath path, SvgTransformCollection transforms) { if (transforms == null) { transforms = new SvgTransformCollection(); } foreach (var child in elem.Children) { var adjust = transforms.Clone(); if (child.Transforms != null) { adjust.AddRange(child.Transforms); } if (child is SvgVisualElement) { if (!(child is SvgGroup)) { var childPath = ((SvgVisualElement)child).Path; if (childPath != null && childPath.PointCount > 0) { childPath = (GraphicsPath)childPath.Clone(); adjust.Transform(childPath); path.AddPath(childPath, false); } } } AddPaths(child, path, adjust); } }
public SvgMenuWidget(float width) : base("") { FWidth = width; Transforms = new SvgTransformCollection(); Transforms.Add(new SvgTranslate(0, 0)); Visible = false; }
/// <summary> /// Initializes a new instance of the <see cref="SvgElement"/> class. /// </summary> public SvgElement() { this._children = new SvgElementCollection(this); this._eventHandlers = new EventHandlerList(); this._elementName = string.Empty; this._customAttributes = new SvgCustomAttributeCollection(this); Transforms = new SvgTransformCollection(); //subscribe to attribute events Attributes.AttributeChanged += Attributes_AttributeChanged; CustomAttributes.AttributeChanged += Attributes_AttributeChanged; }
internal SvgTransformableHelper(SvgTransformCollection transform) { this.Transform = transform; }
public static SKMatrix ToSKMatrix(this SvgTransformCollection svgTransformCollection) { var skMatrixTotal = SKMatrix.MakeIdentity(); if (svgTransformCollection == null) { return(skMatrixTotal); } foreach (var svgTransform in svgTransformCollection) { switch (svgTransform) { case SvgMatrix svgMatrix: { var skMatrix = svgMatrix.ToSKMatrix(); SKMatrix.PreConcat(ref skMatrixTotal, ref skMatrix); } break; case SvgRotate svgRotate: { var skMatrixRotate = SKMatrix.MakeRotationDegrees(svgRotate.Angle, svgRotate.CenterX, svgRotate.CenterY); SKMatrix.PreConcat(ref skMatrixTotal, ref skMatrixRotate); } break; case SvgScale svgScale: { var skMatrixScale = SKMatrix.MakeScale(svgScale.X, svgScale.Y); SKMatrix.PreConcat(ref skMatrixTotal, ref skMatrixScale); } break; case SvgShear svgShear: { // Not in the svg specification. } break; case SvgSkew svgSkew: { float sx = (float)Math.Tan(Math.PI * svgSkew.AngleX / 180); float sy = (float)Math.Tan(Math.PI * svgSkew.AngleY / 180); var skMatrixSkew = SKMatrix.MakeSkew(sx, sy); SKMatrix.PreConcat(ref skMatrixTotal, ref skMatrixSkew); } break; case SvgTranslate svgTranslate: { var skMatrixTranslate = SKMatrix.MakeTranslation(svgTranslate.X, svgTranslate.Y); SKMatrix.PreConcat(ref skMatrixTotal, ref skMatrixTranslate); } break; default: break; } } return(skMatrixTotal); }
public static StrokeCollection exec(string absolutePath, int width, int height, int thickness, System.Windows.Media.Color color, bool dotted) { string filename = Path.GetFileName(absolutePath); if (!isPotraceDirectory) { Directory.SetCurrentDirectory(Directory.GetCurrentDirectory() + "/../../Potrace"); isPotraceDirectory = true; } string extension = absolutePath.Substring(absolutePath.Length - 4, 4); if (extension.ToLower() == PNG || extension.ToLower() == JPG) { Image img = Image.FromFile(absolutePath); filename = filename.Substring(0, filename.Length - 4) + BMP; img.Save("Images/" + filename, ImageFormat.Bmp); } else if (extension.ToLower() != BMP) { throw new FileFormatException(); } Process process = new Process(); process.StartInfo.FileName = "cmd.exe"; process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardInput = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.UseShellExecute = false; process.Start(); process.StandardInput.WriteLine("mkbitmap.exe -o Images/mkbitmap-o/" + filename + " Images/" + filename); process.StandardInput.Flush(); process.StandardInput.WriteLine("potrace.exe --svg -o Images/potrace-o/" + filename.Substring(0, filename.Length - 4) + SVG + " -a 0 --flat -W " + width + "pt -H " + height + "pt Images/mkbitmap-o/" + filename); process.StandardInput.Flush(); process.StandardInput.Close(); process.WaitForExit(); string svg = File.ReadAllText("Images/potrace-o/" + filename.Substring(0, filename.Length - 4) + SVG); SvgTransformCollection transforms = (SvgTransformCollection) new SvgTransformConverter().ConvertFrom(svg.Substring(svg.IndexOf(TRANSFORM_KEY) + TRANSFORM_KEY.Length)); SvgPathSegmentList pathSegments = (SvgPathSegmentList) new SvgPathBuilder().ConvertFrom(svg.Substring(svg.IndexOf(PATH_KEY) + PATH_KEY.Length)); PointF translate = new PointF(0, 0); PointF scale = new PointF(1, 1); for (int i = 0; i < transforms.Count; i++) { if (transforms[i].GetType() == typeof(SvgTranslate)) { translate = new PointF(((SvgTranslate)transforms[i]).X, ((SvgTranslate)transforms[i]).Y); } else if (transforms[i].GetType() == typeof(SvgScale)) { scale = new PointF(((SvgScale)transforms[i]).X, ((SvgScale)transforms[i]).Y); } } StrokeCollection strokes = new StrokeCollection(); if (dotted) { for (int i = 0; i < pathSegments.Count; i++) { StylusPointCollection stylusPoints = new StylusPointCollection(); stylusPoints.Add(new StylusPoint(scale.X * pathSegments[i].Start.X + translate.X, scale.Y * pathSegments[i].Start.Y + translate.Y)); Stroke stroke = new Stroke(stylusPoints); stroke.DrawingAttributes.Width = stroke.DrawingAttributes.Height = thickness; stroke.DrawingAttributes.Color = color; strokes.Add(stroke); } } else { for (int i = 0; i < pathSegments.Count; i++) { if (pathSegments[i].GetType() == typeof(SvgMoveToSegment)) { StylusPointCollection stylusPoints = new StylusPointCollection(); stylusPoints.Add(new StylusPoint(scale.X * pathSegments[i].Start.X + translate.X, scale.Y * pathSegments[i].Start.Y + translate.Y)); Stroke stroke = new Stroke(stylusPoints); stroke.DrawingAttributes.Width = stroke.DrawingAttributes.Height = thickness; stroke.DrawingAttributes.Color = color; strokes.Add(stroke); } else if (pathSegments[i].GetType() == typeof(SvgLineSegment)) { strokes[strokes.Count - 1].StylusPoints.Add(new StylusPoint(scale.X * pathSegments[i].End.X + translate.X, scale.Y * pathSegments[i].End.Y + translate.Y)); } } } if (extension.ToLower() == PNG || extension.ToLower() == JPG) { File.Delete("Images/" + filename); } File.Delete("Images/mkbitmap-o/" + filename); File.Delete("Images/potrace-o/" + filename.Substring(0, filename.Length - 4) + SVG); return(strokes); }
/// <summary> /// Converts the given object to the type of this converter, using the specified context and culture information. /// </summary> /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context.</param> /// <param name="culture">The <see cref="T:System.Globalization.CultureInfo"/> to use as the current culture.</param> /// <param name="value">The <see cref="T:System.Object"/> to convert.</param> /// <returns> /// An <see cref="T:System.Object"/> that represents the converted value. /// </returns> /// <exception cref="T:System.NotSupportedException">The conversion cannot be performed. </exception> public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { if (value is string) { SvgTransformCollection transformList = new SvgTransformCollection(); string[] parts; string contents; string transformName; foreach (string transform in SvgTransformConverter.SplitTransforms((string)value)) { if (string.IsNullOrEmpty(transform)) continue; parts = transform.Split('(', ')'); transformName = parts[0].Trim(); contents = parts[1].Trim(); switch (transformName) { case "translate": string[] coords = contents.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); if (coords.Length == 0 || coords.Length > 2) { throw new FormatException("Translate transforms must be in the format 'translate(x [,y])'"); } float x = float.Parse(coords[0].Trim(), NumberStyles.Float, CultureInfo.InvariantCulture); if (coords.Length > 1) { float y = float.Parse(coords[1].Trim(), NumberStyles.Float, CultureInfo.InvariantCulture); transformList.Add(new SvgTranslate(x, y)); } else { transformList.Add(new SvgTranslate(x)); } break; case "rotate": string[] args = contents.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); if (args.Length != 1 && args.Length != 3) { throw new FormatException("Rotate transforms must be in the format 'rotate(angle [cx cy ])'"); } float angle = float.Parse(args[0], NumberStyles.Float, CultureInfo.InvariantCulture); if (args.Length == 1) { transformList.Add(new SvgRotate(angle)); } else { float cx = float.Parse(args[1], NumberStyles.Float, CultureInfo.InvariantCulture); float cy = float.Parse(args[2], NumberStyles.Float, CultureInfo.InvariantCulture); transformList.Add(new SvgRotate(angle, cx, cy)); } break; case "scale": string[] scales = contents.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); if (scales.Length == 0 || scales.Length > 2) { throw new FormatException("Scale transforms must be in the format 'scale(x [,y])'"); } float sx = float.Parse(scales[0].Trim(), NumberStyles.Float, CultureInfo.InvariantCulture); if (scales.Length > 1) { float sy = float.Parse(scales[1].Trim(), NumberStyles.Float, CultureInfo.InvariantCulture); transformList.Add(new SvgScale(sx, sy)); } else { transformList.Add(new SvgScale(sx)); } break; case "matrix": string[] points = contents.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); if (points.Length != 6) { throw new FormatException("Matrix transforms must be in the format 'matrix(m11, m12, m21, m22, dx, dy)'"); } List<float> mPoints = new List<float>(); foreach (string point in points) { mPoints.Add(float.Parse(point.Trim(), NumberStyles.Float, CultureInfo.InvariantCulture)); } transformList.Add(new SvgMatrix(mPoints)); break; case "shear": string[] shears = contents.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); if (shears.Length == 0 || shears.Length > 2) { throw new FormatException("Shear transforms must be in the format 'shear(x [,y])'"); } float hx = float.Parse(shears[0].Trim(), NumberStyles.Float, CultureInfo.InvariantCulture); if (shears.Length > 1) { float hy = float.Parse(shears[1].Trim(), NumberStyles.Float, CultureInfo.InvariantCulture); transformList.Add(new SvgShear(hx, hy)); } else { transformList.Add(new SvgShear(hx)); } break; case "skewX": float ax = float.Parse(contents, NumberStyles.Float, CultureInfo.InvariantCulture); transformList.Add(new SvgSkew(ax, 0)); break; case "skewY": float ay = float.Parse(contents, NumberStyles.Float, CultureInfo.InvariantCulture); transformList.Add(new SvgSkew(0, ay)); break; } } return transformList; } return base.ConvertFrom(context, culture, value); }