//========================================================================== public override Drawing GetBaseDrawing() { if (Data == null) { return(null); } ImageSource imageSource = null; switch (DataType) { case "jpeg": case "png": var bmp = new BitmapImage(); bmp.BeginInit(); bmp.StreamSource = new MemoryStream(Data); bmp.EndInit(); imageSource = bmp; break; case "svg+xml": imageSource = SvgReader.Load(new MemoryStream(Data)); break; } if (imageSource == null) { return(null); } return(new ImageDrawing(imageSource, new Rect( new Point(X.ToDouble(), Y.ToDouble()), new Size(Width.ToDouble(), Height.ToDouble()) ))); }
//========================================================================== public Pen GetPen() { if (Stroke == null) { return(null); } if (StrokeWidth.ToDouble() <= 0.0) { return(null); } Brush brush = Stroke.ToBrush(this); brush.Opacity = Opacity.ToDouble() * StrokeOpacity.ToDouble(); Pen pen = new Pen(brush, StrokeWidth.ToDouble()); pen.MiterLimit = this.StrokeMiterlimit; if (this.StrokeDasharray != null && this.StrokeDasharray.Length > 0) { var sda = new double[this.StrokeDasharray.Length]; var i = 0; foreach (var svgLength in this.StrokeDasharray) { sda[i++] = svgLength.Value; } var sdo = this.StrokeDashoffset != null ? this.StrokeDashoffset.Value : 0.0; pen.DashStyle = new DashStyle(sda, sdo); } return(pen); }
//========================================================================== public override Geometry GetBaseGeometry() { var eg = new EllipseGeometry( new Point(CenterX.ToDouble(), CenterY.ToDouble()), Radius.ToDouble(), Radius.ToDouble()); return(eg); }
//========================================================================== public Pen GetPen() { if (Stroke == null) { return(null); } if (StrokeWidth.ToDouble() <= 0.0) { return(null); } Brush brush = Stroke.ToBrush(this); brush.Opacity = Opacity.ToDouble() * StrokeOpacity.ToDouble(); Pen pen = new Pen(brush, StrokeWidth.ToDouble()); pen.MiterLimit = this.StrokeMiterlimit; if (this.StrokeDasharray != null && this.StrokeDasharray.Length > 0) { var sda = new double[this.StrokeDasharray.Length]; var i = 0; foreach (var svgLength in this.StrokeDasharray) { sda[i++] = svgLength.Value; } var sdo = this.StrokeDashoffset != null ? this.StrokeDashoffset.Value : 0.0; pen.DashStyle = new DashStyle(sda, sdo); switch (StrokeLinecap) { case SvgStrokeLinecap.Round: pen.DashCap = PenLineCap.Round; break; case SvgStrokeLinecap.Square: pen.DashCap = PenLineCap.Square; break; case SvgStrokeLinecap.Butt: case SvgStrokeLinecap.Inherit: default: pen.DashCap = PenLineCap.Flat; break; } } return(pen); }
//========================================================================== public override Geometry GetBaseGeometry() { return(new RectangleGeometry(new Rect(new Point(X.ToDouble(), Y.ToDouble()), new Size(Width.ToDouble(), Height.ToDouble())), CornerRadiusX.ToDouble(), CornerRadiusY.ToDouble())); }
//========================================================================== public override BitmapEffect ToBitmapEffect() { BlurBitmapEffect blur_bitmap_effect = new BlurBitmapEffect(); blur_bitmap_effect.Radius = StdDeviation.ToDouble(); blur_bitmap_effect.KernelType = KernelType.Box; return(blur_bitmap_effect); }
//========================================================================== public override Drawing GetBaseDrawing() { if (Data == null) { return(null); } string temp_file = Path.GetTempFileName(); using (FileStream file_stream = new FileStream(temp_file, FileMode.Create, FileAccess.Write)) using (BinaryWriter writer = new BinaryWriter(file_stream)) writer.Write(Data); return(new ImageDrawing(new BitmapImage(new Uri(temp_file)), new Rect( new Point(X.ToDouble(), Y.ToDouble()), new Size(Width.ToDouble(), Height.ToDouble()) ))); }
//========================================================================== protected override GradientBrush SetBrush(GradientBrush brush) { RadialGradientBrush radial_gradient_brush = base.SetBrush(brush) as RadialGradientBrush; if (radial_gradient_brush != null) { double cx = CX.ToDouble(); double cy = CY.ToDouble(); double fx = (FX != null) ? FX.ToDouble() : cx; double fy = (FY != null) ? FY.ToDouble() : cy; radial_gradient_brush.GradientOrigin = new Point(fx, fy); radial_gradient_brush.RadiusX = R.ToDouble(); radial_gradient_brush.RadiusY = R.ToDouble(); radial_gradient_brush.Center = new Point(cx, cy); } return(brush); }
//========================================================================== public Pen GetPen() { if (Stroke == null) { return(null); } if (StrokeWidth.ToDouble() <= 0.0) { return(null); } Brush brush = Stroke.ToBrush(this); brush.Opacity = Opacity.ToDouble() * StrokeOpacity.ToDouble(); Pen pen = new Pen(brush, StrokeWidth.ToDouble()); return(pen); }
//========================================================================== public GradientStop ToGradientStop() { Color color = Color.ToColor(); color.A = (byte)Math.Round(Opacity.ToDouble() * 255); GradientStop stop = new GradientStop(); stop.Color = color; stop.Offset = Offset.ToDouble(); return(stop); }
//========================================================================== public Brush GetBrush() { if (Fill == null) { return(null); } Brush brush = Fill.ToBrush(this); if (brush == null) { return(null); } brush.Opacity = Opacity.ToDouble() * FillOpacity.ToDouble(); return(brush); }
//========================================================================== public override Geometry GetBaseGeometry() { return(new EllipseGeometry( new Point(CenterX.ToDouble(), CenterY.ToDouble()), RadiusX.ToDouble(), RadiusY.ToDouble())); }
//========================================================================== public SvgDrawableContainerBaseElement(SvgDocument document, SvgBaseElement parent, XElement drawableContainerElement) : base(document, parent, drawableContainerElement) { XAttribute viewBox_attribute = drawableContainerElement.Attribute("viewBox"); if (viewBox_attribute != null) { this.ViewBox = SvgViewbox.Parse(viewBox_attribute.Value); } XAttribute opacity_attribute = drawableContainerElement.Attribute("opacity"); SvgLength.TryUpdate(ref Opacity, opacity_attribute?.Value); XAttribute transform_attribute = drawableContainerElement.Attribute("transform"); if (transform_attribute != null) { Transform = SvgTransform.Parse(transform_attribute.Value); } XAttribute clip_attribute = drawableContainerElement.Attribute("clip-path"); if (clip_attribute != null) { ClipPath = SvgURL.Parse(clip_attribute.Value); } XAttribute filter_attribute = drawableContainerElement.Attribute("filter"); if (filter_attribute != null) { Filter = SvgURL.Parse(filter_attribute.Value); } XAttribute mask_attribute = drawableContainerElement.Attribute("mask"); if (mask_attribute != null) { Mask = SvgURL.Parse(mask_attribute.Value); } XAttribute display_attribute = drawableContainerElement.Attribute("display"); if (display_attribute != null) { switch (display_attribute.Value) { case "inline": Display = SvgDisplay.Inline; break; case "block": Display = SvgDisplay.Block; break; case "list-item": Display = SvgDisplay.ListItem; break; case "run-in": Display = SvgDisplay.RunIn; break; case "compact": Display = SvgDisplay.Compact; break; case "marker": Display = SvgDisplay.Marker; break; case "table": Display = SvgDisplay.Table; break; case "inline-table": Display = SvgDisplay.InlineTable; break; case "table-row-group": Display = SvgDisplay.TableRowGroup; break; case "table-header-group": Display = SvgDisplay.TableHeaderGroup; break; case "table-footer-group": Display = SvgDisplay.TableFooterGroup; break; case "table-row": Display = SvgDisplay.TableRow; break; case "table-column-group": Display = SvgDisplay.TableColumnGroup; break; case "table-column": Display = SvgDisplay.TableColumn; break; case "table-cell": Display = SvgDisplay.TableCell; break; case "table-caption": Display = SvgDisplay.TableCaption; break; case "none": Display = SvgDisplay.None; break; default: throw new NotImplementedException(); } } XAttribute fill_opacity_attribute = drawableContainerElement.Attribute("fill-opacity"); SvgLength.TryUpdate(ref FillOpacity, fill_opacity_attribute?.Value); XAttribute stroke_opacity_attribute = drawableContainerElement.Attribute("stroke-opacity"); SvgLength.TryUpdate(ref StrokeOpacity, stroke_opacity_attribute?.Value); XAttribute fill_attribute = drawableContainerElement.Attribute("fill"); if (fill_attribute != null) { Fill = SvgPaint.Parse(fill_attribute.Value); } XAttribute stroke_attribute = drawableContainerElement.Attribute("stroke"); if (stroke_attribute != null) { Stroke = SvgPaint.Parse(stroke_attribute.Value); } XAttribute stroke_width_attribute = drawableContainerElement.Attribute("stroke-width"); SvgLength.TryUpdate(ref StrokeWidth, stroke_width_attribute?.Value); XAttribute stroke_linecap_attribute = drawableContainerElement.Attribute("stroke-linecap"); if (stroke_linecap_attribute != null) { switch (stroke_linecap_attribute.Value) { case "butt": StrokeLinecap = SvgStrokeLinecap.Butt; break; case "round": StrokeLinecap = SvgStrokeLinecap.Round; break; case "square": StrokeLinecap = SvgStrokeLinecap.Square; break; case "inherit": StrokeLinecap = SvgStrokeLinecap.Inherit; break; default: throw new NotImplementedException(); } } XAttribute stroke_linejoin_attribute = drawableContainerElement.Attribute("stroke-linejoin"); if (stroke_linejoin_attribute != null) { switch (stroke_linejoin_attribute.Value) { case "miter": StrokeLinejoin = SvgStrokeLinejoin.Miter; break; case "round": StrokeLinejoin = SvgStrokeLinejoin.Round; break; case "bevel": StrokeLinejoin = SvgStrokeLinejoin.Bevel; break; case "inherit": StrokeLinejoin = SvgStrokeLinejoin.Inherit; break; default: throw new NotSupportedException(); } } XAttribute stroke_miterlimit_attribute = drawableContainerElement.Attribute("stroke-miterlimit"); if (stroke_miterlimit_attribute != null) { if (stroke_miterlimit_attribute.Value == "inherit") { StrokeMiterlimit = Double.NaN; } else { double miterlimit = Double.Parse(stroke_miterlimit_attribute.Value, CultureInfo.InvariantCulture.NumberFormat); //if(miterlimit < 1) //throw new NotSupportedException("A miterlimit less than 1 is not supported."); StrokeMiterlimit = miterlimit; } } XAttribute stroke_dasharray_attribute = drawableContainerElement.Attribute("stroke-dasharray"); if (stroke_dasharray_attribute != null) { if (stroke_dasharray_attribute.Value == "none") { StrokeDasharray = null; } else if (stroke_dasharray_attribute.Value == "inherit") { StrokeDasharray = new SvgLength[0]; } else { List <SvgLength> lengths = new List <SvgLength>(); var lengthTokens = stroke_dasharray_attribute.Value.Replace(";", "") .Trim() .Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (string length in lengthTokens) { lengths.Add(SvgLength.Parse(length)); } if (lengths.Count % 2 == 1) { StrokeDasharray = new SvgLength[lengths.Count * 2]; for (int i = 0; i < lengths.Count - 1; ++i) { StrokeDasharray[i] = lengths[i]; StrokeDasharray[i + lengths.Count] = lengths[i]; } } else { StrokeDasharray = lengths.ToArray(); } } } XAttribute stroke_dashoffset_attribute = drawableContainerElement.Attribute("stroke-dashoffset"); SvgLength.TryUpdate(ref StrokeDashoffset, stroke_dashoffset_attribute?.Value); XAttribute fill_rule_attribute = drawableContainerElement.Attribute("fill-rule"); if (fill_rule_attribute != null) { switch (fill_rule_attribute.Value) { case "nonzero": FillRule = SvgFillRule.Nonzero; break; case "evenodd": FillRule = SvgFillRule.Evenodd; break; case "inherit": FillRule = SvgFillRule.Inherit; break; default: throw new NotImplementedException(); } } // color, color-interpolation, color-rendering XAttribute width_attribute = drawableContainerElement.Attribute("width"); SvgLength.TryUpdate(ref Width, width_attribute?.Value); XAttribute height_attribute = drawableContainerElement.Attribute("height"); SvgLength.TryUpdate(ref Height, height_attribute?.Value); XAttribute preserveAspectRatio_attribute = drawableContainerElement.Attribute("preserveAspectRatio"); if (preserveAspectRatio_attribute != null) { switch (preserveAspectRatio_attribute.Value) { case "none": if (Width != null && Height != null) { var scaleTransform = new SvgScaleTransform( Width.ToDouble() / ViewBox.Value.Width, Height.ToDouble() / ViewBox.Value.Height); Width.ToDouble(); if (Transform == null) { Transform = scaleTransform; } else { Transform = new SvgTransformGroup(new[] { Transform, scaleTransform }); } } break; } } // overflow }
//========================================================================== public virtual Drawing Draw() { DrawingGroup drawing_group = new DrawingGroup(); drawing_group.Opacity = Opacity.ToDouble(); if (Transform != null) { drawing_group.Transform = Transform.ToTransform(); } if (ViewBox != null) { drawing_group.Children.Add(ViewBox.Process()); } foreach (SvgBaseElement child_element in Children) { SvgBaseElement element = child_element; if (element is SvgUseElement) { element = (element as SvgUseElement).GetElement(); } Drawing drawing = null; if (element is SvgDrawableBaseElement) { if ((element as SvgDrawableBaseElement).Display != SvgDisplay.None) { drawing = (element as SvgDrawableBaseElement).Draw(); } } else if (element is SvgDrawableContainerBaseElement) { if ((element as SvgDrawableContainerBaseElement).Display != SvgDisplay.None) { drawing = (element as SvgDrawableContainerBaseElement).Draw(); } } if (drawing != null) { drawing_group.Children.Add(drawing); } } if (Filter != null) { SvgFilterElement filter_element = Document.Elements[Filter.Id] as SvgFilterElement; if (filter_element != null) { drawing_group.BitmapEffect = filter_element.ToBitmapEffect(); } } if (ClipPath != null) { SvgClipPathElement clip_path_element = Document.Elements[ClipPath.Id] as SvgClipPathElement; if (clip_path_element != null) { drawing_group.ClipGeometry = clip_path_element.GetClipGeometry(); } } if (Mask != null) { SvgMaskElement mask_element = Document.Elements[Mask.Id] as SvgMaskElement; if (mask_element != null) { DrawingBrush opacity_mask = mask_element.GetOpacityMask(); /* * if(Transform != null) * opacity_mask.Transform = Transform.ToTransform(); */ drawing_group.OpacityMask = opacity_mask; } } return(drawing_group); }
//========================================================================== public virtual Drawing Draw() { DrawingGroup drawing_group = new DrawingGroup(); drawing_group.Opacity = Opacity.ToDouble(); if (Transform != null) { drawing_group.Transform = Transform.ToTransform(); } //if (ViewBox != null) // drawing_group.Children.Add(ViewBox.Process()); foreach (SvgBaseElement child_element in Children) { SvgBaseElement element = child_element; if (element is SvgUseElement) { element = (element as SvgUseElement).GetElement(); } Drawing drawing = null; if (element is SvgDrawableBaseElement) { if ((element as SvgDrawableBaseElement).Display != SvgDisplay.None) { drawing = (element as SvgDrawableBaseElement).Draw(); } } else if (element is SvgDrawableContainerBaseElement) { if ((element as SvgDrawableContainerBaseElement).Display != SvgDisplay.None) { drawing = (element as SvgDrawableContainerBaseElement).Draw(); } } if (drawing != null) { drawing_group.Children.Add(drawing); } } if (Filter != null) { SvgFilterElement filter_element = Document.Elements[Filter.Id] as SvgFilterElement; if (filter_element != null) { drawing_group.BitmapEffect = filter_element.ToBitmapEffect(); } } if (ClipPath != null) { SvgClipPathElement clip_path_element = Document.Elements[ClipPath.Id] as SvgClipPathElement; if (clip_path_element != null) { drawing_group.ClipGeometry = clip_path_element.GetClipGeometry(); } } if (Mask != null) { SvgMaskElement mask_element = Document.Elements[Mask.Id] as SvgMaskElement; if (mask_element != null) { DrawingBrush opacity_mask = mask_element.GetOpacityMask(); /* * if(Transform != null) * opacity_mask.Transform = Transform.ToTransform(); */ drawing_group.OpacityMask = opacity_mask; } } // add base element size if (this is SvgSVGElement) { var svg = this as SvgSVGElement; DrawingVisual visual = new DrawingVisual(); DrawingContext dc = visual.RenderOpen(); Rect rect = new Rect(new System.Windows.Point(0, 0), new System.Windows.Size(svg.Width.Value, svg.Height.Value)); dc.DrawRectangle(System.Windows.Media.Brushes.Transparent, (System.Windows.Media.Pen)null, rect); dc.Close(); drawing_group.Children.Add(visual.Drawing); } return(drawing_group); }