private void OnAfterRender(WpfDrawingRenderer renderer) { Debug.Assert(_drawGroup != null); // Support for Tiny 1.2 viewport-fill property... if (_svgElement.HasAttribute("viewport-fill")) { var viewportFill = _svgElement.GetAttribute("viewport-fill"); if (!string.IsNullOrWhiteSpace(viewportFill)) { SvgSvgElement svgElm = (SvgSvgElement)_svgElement; var brush = WpfFill.CreateViewportBrush(svgElm); if (brush != null) { var bounds = new RectangleGeometry(_drawGroup.Bounds); var drawing = new GeometryDrawing(brush, null, bounds); _drawGroup.Children.Insert(0, drawing); } } } WpfDrawingContext context = renderer.Context; DrawingGroup currentGroup = context.Peek(); if (currentGroup == null || currentGroup != _drawGroup) { throw new InvalidOperationException("An existing group is expected."); } context.Pop(); if (_isRoot && !context.IsFragment) { this.AdjustViewbox(); } if (_isRoot || context.IsFragment) { return; } DrawingGroup drawGroup = CreateOuterGroup(); if (drawGroup == null) { return; } currentGroup = context.Peek(); if (currentGroup == null || currentGroup.Children.Remove(_drawGroup) == false) { return; } drawGroup.Children.Add(_drawGroup); currentGroup.Children.Add(drawGroup); }
public override void Render(WpfDrawingRenderer renderer) { WpfDrawingContext context = renderer.Context; SvgImageElement imageElement = (SvgImageElement)_svgElement; double x = imageElement.X.AnimVal.Value; double y = imageElement.Y.AnimVal.Value; double width = imageElement.Width.AnimVal.Value; double height = imageElement.Height.AnimVal.Value; Rect destRect = new Rect(x, y, width, height); Rect clipRect = new Rect(x, y, width, height); ImageSource imageSource = null; if (imageElement.IsSvgImage) { if (imageElement.IsRootReferenced(imageElement.OwnerDocument.BaseURI)) { return; } SvgWindow wnd = GetSvgWindow(); if (wnd == null) { return; } //_embeddedRenderer.BackColor = Color.Empty; _embeddedRenderer.Render(wnd.Document); DrawingGroup imageGroup = _embeddedRenderer.Drawing as DrawingGroup; if (imageGroup != null && (imageGroup.Children != null && imageGroup.Children.Count == 1)) { DrawingGroup imageDrawing = imageGroup.Children[0] as DrawingGroup; if (imageDrawing != null) { imageDrawing.ClipGeometry = null; imageSource = new DrawingImage(imageDrawing); } else { imageGroup.ClipGeometry = null; imageSource = new DrawingImage(imageGroup); } } else { imageSource = new DrawingImage(_embeddedRenderer.Drawing); } if (_embeddedRenderer != null) { _embeddedRenderer.Dispose(); _embeddedRenderer = null; } } else { imageSource = GetBitmapSource(imageElement, context); } if (imageSource == null) { return; } //TODO--PAUL: Set the DecodePixelWidth/DecodePixelHeight? // Freeze the DrawingImage for performance benefits. //imageSource.Freeze(); DrawingGroup drawGroup = null; ISvgAnimatedPreserveAspectRatio animatedAspectRatio = imageElement.PreserveAspectRatio; if (animatedAspectRatio != null && animatedAspectRatio.AnimVal != null) { SvgPreserveAspectRatio aspectRatio = animatedAspectRatio.AnimVal as SvgPreserveAspectRatio; SvgPreserveAspectRatioType aspectRatioType = (aspectRatio != null) ? aspectRatio.Align : SvgPreserveAspectRatioType.Unknown; if (aspectRatio != null && aspectRatioType != SvgPreserveAspectRatioType.None && aspectRatioType != SvgPreserveAspectRatioType.Unknown) { double imageWidth = imageSource.Width; double imageHeight = imageSource.Height; double viewWidth = destRect.Width; double viewHeight = destRect.Height; SvgMeetOrSlice meetOrSlice = aspectRatio.MeetOrSlice; if (meetOrSlice == SvgMeetOrSlice.Meet) { if (imageWidth <= viewWidth && imageHeight <= viewHeight) { if (this.Transform == null) { if (!aspectRatio.IsDefaultAlign) // Cacxa { destRect = this.GetBounds(destRect, new Size(imageWidth, imageHeight), aspectRatioType); } else { Transform viewTransform = this.GetAspectRatioTransform(aspectRatio, new SvgRect(0, 0, imageWidth, imageHeight), new SvgRect(destRect.X, destRect.Y, destRect.Width, destRect.Height)); if (viewTransform != null) { drawGroup = new DrawingGroup(); drawGroup.Transform = viewTransform; DrawingGroup lastGroup = context.Peek(); Debug.Assert(lastGroup != null); if (lastGroup != null) { lastGroup.Children.Add(drawGroup); } destRect = this.GetBounds(destRect, new Size(imageWidth, imageHeight), aspectRatioType); // The origin is already handled by the view transform... destRect.X = 0; destRect.Y = 0; } } } else { destRect = new Rect(0, 0, viewWidth, viewHeight); } } else { if (this.Transform == null) { Transform viewTransform = this.GetAspectRatioTransform(aspectRatio, new SvgRect(0, 0, imageWidth, imageHeight), new SvgRect(destRect.X, destRect.Y, destRect.Width, destRect.Height)); if (viewTransform != null) { drawGroup = new DrawingGroup(); drawGroup.Transform = viewTransform; DrawingGroup lastGroup = context.Peek(); Debug.Assert(lastGroup != null); if (lastGroup != null) { lastGroup.Children.Add(drawGroup); } destRect = this.GetBounds(destRect, new Size(imageWidth, imageHeight), aspectRatioType); // The origin is already handled by the view transform... destRect.X = 0; destRect.Y = 0; } } } } else if (meetOrSlice == SvgMeetOrSlice.Slice) { var fScaleX = viewWidth / imageWidth; var fScaleY = viewHeight / imageHeight; Transform viewTransform = this.GetAspectRatioTransform(aspectRatio, new SvgRect(0, 0, imageWidth, imageHeight), new SvgRect(destRect.X, destRect.Y, destRect.Width, destRect.Height)); DrawingGroup sliceGroup = new DrawingGroup(); sliceGroup.ClipGeometry = new RectangleGeometry(clipRect); DrawingGroup lastGroup = context.Peek(); Debug.Assert(lastGroup != null); if (lastGroup != null) { lastGroup.Children.Add(sliceGroup); } if (viewTransform != null) { drawGroup = new DrawingGroup(); drawGroup.Transform = viewTransform; sliceGroup.Children.Add(drawGroup); destRect = this.GetBounds(destRect, new Size(imageWidth, imageHeight), aspectRatioType); // The origin is already handled by the view transform... destRect.X = 0; destRect.Y = 0; } else { drawGroup = sliceGroup; } } } } ImageDrawing drawing = new ImageDrawing(imageSource, destRect); float opacityValue = -1; string opacity = imageElement.GetAttribute("opacity"); if (string.IsNullOrWhiteSpace(opacity)) { opacity = imageElement.GetPropertyValue("opacity"); } if (!string.IsNullOrWhiteSpace(opacity)) { opacityValue = (float)SvgNumber.ParseNumber(opacity); opacityValue = Math.Min(opacityValue, 1); opacityValue = Math.Max(opacityValue, 0); } Geometry clipGeom = this.ClipGeometry; Transform transform = this.Transform; GeometryDrawing viewportDrawing = null; // Support for Tiny 1.2 viewport-fill property... if (_svgElement.HasAttribute("viewport-fill")) { var viewportFill = _svgElement.GetAttribute("viewport-fill"); if (!string.IsNullOrWhiteSpace(viewportFill)) { var brush = WpfFill.CreateViewportBrush(imageElement); if (brush != null) { var viewportBounds = new RectangleGeometry(destRect); viewportDrawing = new GeometryDrawing(brush, null, viewportBounds); } } } bool ownedGroup = true; if (drawGroup == null) { drawGroup = context.Peek(); ownedGroup = false; } else { if (viewportDrawing != null) { drawGroup.Children.Insert(0, viewportDrawing); } } Debug.Assert(drawGroup != null); if (drawGroup != null) { if ((opacityValue >= 0 && opacityValue < 1) || (clipGeom != null && !clipGeom.IsEmpty()) || (transform != null && !transform.Value.IsIdentity)) { DrawingGroup clipGroup = ownedGroup ? drawGroup : new DrawingGroup(); if (opacityValue >= 0 && opacityValue < 1) { clipGroup.Opacity = opacityValue; } if (clipGeom != null) { SvgUnitType clipUnits = this.ClipUnits; if (clipUnits == SvgUnitType.ObjectBoundingBox) { Rect drawingBounds = drawing.Bounds; TransformGroup transformGroup = new TransformGroup(); // Scale the clip region (at (0, 0)) and translate to the top-left corner of the target. transformGroup.Children.Add( new ScaleTransform(drawingBounds.Width, drawingBounds.Height)); transformGroup.Children.Add( new TranslateTransform(drawingBounds.X, drawingBounds.Y)); clipGeom.Transform = transformGroup; } clipGroup.ClipGeometry = clipGeom; } if (transform != null) { Transform curTransform = clipGroup.Transform; if (curTransform != null && curTransform.Value.IsIdentity == false) { TransformGroup transformGroup = new TransformGroup(); transformGroup.Children.Add(curTransform); transformGroup.Children.Add(transform); clipGroup.Transform = transformGroup; } else { clipGroup.Transform = transform; } } clipGroup.Children.Add(drawing); if (!ownedGroup) { if (viewportDrawing != null) { clipGroup.Children.Insert(0, viewportDrawing); } drawGroup.Children.Add(clipGroup); } } else { drawGroup.Children.Add(drawing); } string elementId = this.GetElementName(); if (ownedGroup) { string sVisibility = imageElement.GetPropertyValue("visibility"); string sDisplay = imageElement.GetPropertyValue("display"); if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none")) { drawGroup.Opacity = 0; } if (!_idAssigned && !string.IsNullOrWhiteSpace(elementId) && !context.IsRegisteredId(elementId)) { context.RegisterId(elementId); if (context.IncludeRuntime) { SvgObject.SetName(drawGroup, elementId); SvgObject.SetId(drawGroup, elementId); } } // Register this drawing with the Drawing-Document... this.Rendered(drawGroup); } else if (!_idAssigned) { if (!_idAssigned && !string.IsNullOrWhiteSpace(elementId) && !context.IsRegisteredId(elementId)) { context.RegisterId(elementId); if (context.IncludeRuntime) { SvgObject.SetName(imageSource, elementId); SvgObject.SetId(imageSource, elementId); } } // Register this drawing with the Drawing-Document... this.Rendered(drawing); } } }