private WpfFill GetPaintFill(string uri) { string absoluteUri = _element.ResolveUri(uri); if (_element.Imported && _element.ImportDocument != null && _element.ImportNode != null) { // We need to determine whether the provided URI refers to element in the // original document or in the current document... SvgStyleableElement styleElm = _element.ImportNode as SvgStyleableElement; if (styleElm != null) { var cssDeclaration = styleElm.GetComputedStyle(""); string propertyValue = cssDeclaration.GetPropertyValue(_propertyName); if (!string.IsNullOrWhiteSpace(propertyValue)) { WpfSvgPaint importFill = new WpfSvgPaint(_context, styleElm, _propertyName); if (string.Equals(uri, importFill.Uri, StringComparison.OrdinalIgnoreCase)) { WpfFill fill = WpfFill.CreateFill(_element.ImportDocument, absoluteUri); if (fill != null) { return(fill); } } } } } return(WpfFill.CreateFill(_element.OwnerDocument, absoluteUri)); }
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); }
private Brush GetBrush(Geometry geometry, string propPrefix, bool setOpacity) { WpfSvgPaintContext paintContext = null; SvgPaintType paintType = this.PaintType; WpfSvgPaint fill; if (paintType == SvgPaintType.None) { return(null); } if (paintType == SvgPaintType.CurrentColor) { //TODO: Find a better way to support currentColor specified on parent element. var deferredFill = this.GetDeferredFill(); if (deferredFill == null) { fill = new WpfSvgPaint(_context, _element, "color"); } else { fill = deferredFill; } } else if (paintType == SvgPaintType.ContextFill) { paintContext = GetFillContext(); if (paintContext != null) { fill = paintContext.Fill; } else { fill = this; } } else if (paintType == SvgPaintType.ContextStroke) { paintContext = GetStrokeContext(); if (paintContext != null) { fill = paintContext.Stroke; } else { fill = this; } } else { fill = this; } SvgPaintType fillType = fill.PaintType; if (fillType == SvgPaintType.Uri || fillType == SvgPaintType.UriCurrentColor || fillType == SvgPaintType.UriNone || fillType == SvgPaintType.UriRgbColor || fillType == SvgPaintType.UriRgbColorIccColor) { _paintFill = GetPaintFill(fill.Uri); if (_paintFill != null) { Brush brush = null; if (geometry != null) { brush = _paintFill.GetBrush(geometry.Bounds, _context, geometry.Transform); } else { brush = _paintFill.GetBrush(Rect.Empty, _context, null); } if (brush != null) { brush.Opacity = GetOpacity(propPrefix); } return(brush); } if (paintType == SvgPaintType.UriNone || paintType == SvgPaintType.Uri) { return(null); } if (paintType == SvgPaintType.UriCurrentColor) { fill = new WpfSvgPaint(_context, _element, "color"); } else { fill = this; } } if (fill == null || fill.RgbColor == null) { return(null); } Color?solidColor = WpfConvert.ToColor(fill.RgbColor); if (solidColor == null) { return(null); } SolidColorBrush solidBrush = new SolidColorBrush(solidColor.Value); if (setOpacity) { solidBrush.Opacity = GetOpacity(propPrefix); } return(solidBrush); }
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); } } }
private Brush GetBrush(Geometry geometry, string propPrefix, bool setOpacity) { SvgPaint fill; if (PaintType == SvgPaintType.None) { return null; } else if (PaintType == SvgPaintType.CurrentColor) { fill = new WpfSvgPaint(_context, _element, "color"); } else { fill = this; } SvgPaintType paintType = fill.PaintType; if (paintType == SvgPaintType.Uri || paintType == SvgPaintType.UriCurrentColor || paintType == SvgPaintType.UriNone || paintType == SvgPaintType.UriRgbColor || paintType == SvgPaintType.UriRgbColorIccColor) { _paintFill = GetPaintFill(fill.Uri); if (_paintFill != null) { Brush brush = null; if (geometry != null) { brush = _paintFill.GetBrush(geometry.Bounds, _context); } else { brush = _paintFill.GetBrush(Rect.Empty, _context); } if (brush != null) { brush.Opacity = GetOpacity(propPrefix); } return brush; } else { if (PaintType == SvgPaintType.UriNone || PaintType == SvgPaintType.Uri) { return null; } else if (PaintType == SvgPaintType.UriCurrentColor) { fill = new WpfSvgPaint(_context, _element, "color"); } else { fill = this; } } } if (fill == null || fill.RgbColor == null) { return null; } Color? solidColor = WpfConvert.ToColor(fill.RgbColor); if (solidColor == null) { return null; } SolidColorBrush solidBrush = new SolidColorBrush(solidColor.Value); //int opacity = GetOpacity(propPrefix); //solidBrush.Color = Color.FromArgb(opacity, brush.Color); if (setOpacity) { solidBrush.Opacity = GetOpacity(propPrefix); } return solidBrush; }
private Brush GetBrush(Geometry geometry, string propPrefix, bool setOpacity) { WpfSvgPaintContext paintContext = null; SvgPaintType paintType = this.PaintType; WpfSvgPaint fill; if (paintType == SvgPaintType.None) { return(null); } if (paintType == SvgPaintType.CurrentColor) { //TODO: Find a better way to support currentColor specified on parent element. var deferredFill = this.GetDeferredFill(); if (deferredFill == null) { fill = new WpfSvgPaint(_context, _element, CssConstants.PropColor); } else { fill = deferredFill; } } else if (paintType == SvgPaintType.ContextFill) { paintContext = GetFillContext(); if (paintContext != null) { fill = paintContext.Fill; } else { fill = this; } } else if (paintType == SvgPaintType.ContextStroke) { paintContext = GetStrokeContext(); if (paintContext != null) { fill = paintContext.Stroke; } else { fill = this; } } else { fill = this; } SvgPaintType fillType = fill.PaintType; if (fillType == SvgPaintType.Uri || fillType == SvgPaintType.UriCurrentColor || fillType == SvgPaintType.UriNone || fillType == SvgPaintType.UriRgbColor || fillType == SvgPaintType.UriRgbColorIccColor) { _paintFill = GetPaintFill(fill.Uri); if (_paintFill != null) { Brush brush = null; if (geometry != null) { brush = _paintFill.GetBrush(geometry.Bounds, _context, geometry.Transform); } else { brush = _paintFill.GetBrush(Rect.Empty, _context, null); } if (brush != null) { brush.Opacity = GetOpacity(propPrefix); } return(brush); } if (paintType == SvgPaintType.UriNone || paintType == SvgPaintType.Uri) { return(null); } if (paintType == SvgPaintType.UriCurrentColor) { fill = new WpfSvgPaint(_context, _element, CssConstants.PropColor); } else { fill = this; } } if (fill == null || fill.RgbColor == null) { return(null); } if (fill.RgbColor.IsVarColor) { var cssVar = this.GetVarsValue(fill); if (cssVar != null) { var cssVariables = _context.Settings.CssVariables; if (cssVariables != null && cssVariables.ContainsKey(cssVar.VarName)) { var cssColor = new CssColor(cssVariables[cssVar.VarName]); Color?varColor = WpfConvert.ToColor(cssColor); if (varColor != null) { var varBrush = new SolidColorBrush(varColor.Value); if (setOpacity) { varBrush.Opacity = GetOpacity(propPrefix); } return(varBrush); } } var cssValue = _element.GetComputedCssValue(cssVar.VarName, string.Empty) as CssAbsPrimitiveValue; if (cssValue != null) { Color?varColor = WpfConvert.ToColor(cssValue.GetRgbColorValue()); if (varColor != null) { var varBrush = new SolidColorBrush(varColor.Value); if (setOpacity) { varBrush.Opacity = GetOpacity(propPrefix); } return(varBrush); } } var fallbackValue = cssVar.VarValue; if (!string.IsNullOrWhiteSpace(fallbackValue)) { var cssColor = new CssColor(fallbackValue); Color?varColor = WpfConvert.ToColor(cssColor); if (varColor != null) { var varBrush = new SolidColorBrush(varColor.Value); if (setOpacity) { varBrush.Opacity = GetOpacity(propPrefix); } return(varBrush); } } } } Color?solidColor = WpfConvert.ToColor(fill.RgbColor); if (solidColor == null) { return(null); } SolidColorBrush solidBrush = new SolidColorBrush(solidColor.Value); if (setOpacity) { solidBrush.Opacity = GetOpacity(propPrefix); } return(solidBrush); }
private Brush GetBrush(Geometry geometry, string propPrefix, bool setOpacity) { SvgPaint fill; if (PaintType == SvgPaintType.None) { return(null); } else if (PaintType == SvgPaintType.CurrentColor) { fill = new WpfSvgPaint(_context, _element, "color"); } else { fill = this; } SvgPaintType paintType = fill.PaintType; if (paintType == SvgPaintType.Uri || paintType == SvgPaintType.UriCurrentColor || paintType == SvgPaintType.UriNone || paintType == SvgPaintType.UriRgbColor || paintType == SvgPaintType.UriRgbColorIccColor) { _paintFill = GetPaintFill(fill.Uri); if (_paintFill != null) { Brush brush = null; if (geometry != null) { brush = _paintFill.GetBrush(geometry.Bounds, _context); } else { brush = _paintFill.GetBrush(Rect.Empty, _context); } if (brush != null) { brush.Opacity = GetOpacity(propPrefix); } return(brush); } else { if (PaintType == SvgPaintType.UriNone || PaintType == SvgPaintType.Uri) { return(null); } else if (PaintType == SvgPaintType.UriCurrentColor) { fill = new WpfSvgPaint(_context, _element, "color"); } else { fill = this; } } } if (fill == null || fill.RgbColor == null) { return(null); } Color?solidColor = WpfConvert.ToColor(fill.RgbColor); if (solidColor == null) { return(null); } SolidColorBrush solidBrush = new SolidColorBrush(solidColor.Value); //int opacity = GetOpacity(propPrefix); //solidBrush.Color = Color.FromArgb(opacity, brush.Color); if (setOpacity) { solidBrush.Opacity = GetOpacity(propPrefix); } return(solidBrush); }