private void RenderPath(WpfDrawingRenderer renderer) { WpfDrawingContext context = renderer.Context; SvgRenderingHint hint = _svgElement.RenderingHint; if (hint != SvgRenderingHint.Shape || hint == SvgRenderingHint.Clipping) { return; } var parentNode = _svgElement.ParentNode; // We do not directly render the contents of the clip-path, unless specifically requested... if (string.Equals(parentNode.LocalName, "clipPath") && !context.RenderingClipRegion) { return; } SvgStyleableElement styleElm = (SvgStyleableElement)_svgElement; //string sVisibility = styleElm.GetPropertyValue("visibility"); //string sDisplay = styleElm.GetPropertyValue("display"); //if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none")) //{ // return; //} DrawingGroup drawGroup = context.Peek(); Debug.Assert(drawGroup != null); Geometry geometry = CreateGeometry(_svgElement, context.OptimizePath); string elementId = this.GetElementName(); string elementClass = this.GetElementClass(); GeometryDrawing drawing = null; if (geometry == null || geometry.IsEmpty()) { return; } var bounds = geometry.Bounds; if (string.Equals(_svgElement.LocalName, "line", StringComparison.Ordinal)) { _isLineSegment = true; } else if (string.Equals(_svgElement.LocalName, "rect", StringComparison.Ordinal)) { _isLineSegment = bounds.Width.Equals(0) || bounds.Height.Equals(0); } else if (string.Equals(_svgElement.LocalName, "path", StringComparison.Ordinal)) { _isLineSegment = bounds.Width.Equals(0) || bounds.Height.Equals(0); } context.UpdateBounds(geometry.Bounds); // SetClip(context); WpfSvgPaint fillPaint = new WpfSvgPaint(context, styleElm, "fill"); string fileValue = styleElm.GetAttribute("fill"); Brush brush = fillPaint.GetBrush(geometry, _setBrushOpacity); if (brush == null) { WpfSvgPaint fallbackPaint = fillPaint.WpfFallback; if (fallbackPaint != null) { brush = fallbackPaint.GetBrush(geometry, _setBrushOpacity); } } bool isFillTransmable = fillPaint.IsFillTransformable; WpfSvgPaint strokePaint = new WpfSvgPaint(context, styleElm, "stroke"); Pen pen = strokePaint.GetPen(geometry, _setBrushOpacity); // By the SVG Specifications: // Keyword 'objectBoundingBox' should not be used when the geometry of the applicable // element has no width or no height, such as the case of a horizontal or vertical line, // even when the line has actual thickness when viewed due to having a non-zero stroke // width since stroke width is ignored for bounding box calculations. When the geometry // of the applicable element has no width or height and 'objectBoundingBox' is specified, // then the given effect (e.g., a gradient) will be ignored. if (pen != null && _isLineSegment && strokePaint.FillType == WpfFillType.Gradient) { WpfGradientFill gradientFill = (WpfGradientFill)strokePaint.PaintServer; if (gradientFill.IsUserSpace == false) { bool invalidGrad = false; if (string.Equals(_svgElement.LocalName, "line", StringComparison.Ordinal)) { LineGeometry lineGeometry = geometry as LineGeometry; if (lineGeometry != null) { invalidGrad = SvgObject.IsEqual(lineGeometry.EndPoint.X, lineGeometry.StartPoint.X) || SvgObject.IsEqual(lineGeometry.EndPoint.Y, lineGeometry.StartPoint.Y); } } else { invalidGrad = true; } if (invalidGrad) { // Brush is not likely inherited, we need to support fallback too WpfSvgPaint fallbackPaint = strokePaint.WpfFallback; if (fallbackPaint != null) { pen.Brush = fallbackPaint.GetBrush(geometry, _setBrushOpacity); } else { var scopePaint = strokePaint.GetScopeStroke(); if (scopePaint != null) { if (scopePaint != strokePaint) { pen.Brush = scopePaint.GetBrush(geometry, _setBrushOpacity); } else { pen.Brush = null; } } else { pen.Brush = null; } } } } } if (_paintContext != null) { _paintContext.Fill = fillPaint; _paintContext.Stroke = strokePaint; _paintContext.Tag = geometry; } if (brush != null || pen != null) { Transform transform = this.Transform; if (transform != null && !transform.Value.IsIdentity) { geometry.Transform = transform; if (brush != null && isFillTransmable) { Transform brushTransform = brush.Transform; if (brushTransform == null || brushTransform == Transform.Identity) { brush.Transform = transform; } else { TransformGroup groupTransform = new TransformGroup(); groupTransform.Children.Add(brushTransform); groupTransform.Children.Add(transform); brush.Transform = groupTransform; } } if (pen != null && pen.Brush != null) { Transform brushTransform = pen.Brush.Transform; if (brushTransform == null || brushTransform == Transform.Identity) { pen.Brush.Transform = transform; } else { TransformGroup groupTransform = new TransformGroup(); groupTransform.Children.Add(brushTransform); groupTransform.Children.Add(transform); pen.Brush.Transform = groupTransform; } } } else { transform = null; // render any identity transform useless... } drawing = new GeometryDrawing(brush, pen, geometry); if (!string.IsNullOrWhiteSpace(elementId) && !context.IsRegisteredId(elementId)) { SvgObject.SetName(drawing, elementId); context.RegisterId(elementId); if (context.IncludeRuntime) { SvgObject.SetId(drawing, elementId); } } if (!string.IsNullOrWhiteSpace(elementClass) && context.IncludeRuntime) { SvgObject.SetClass(drawing, elementClass); } Brush maskBrush = this.Masking; Geometry clipGeom = this.ClipGeometry; if (clipGeom != null || maskBrush != null) { //Geometry clipped = Geometry.Combine(geometry, clipGeom, // GeometryCombineMode.Exclude, null); //if (clipped != null && !clipped.IsEmpty()) //{ // geometry = clipped; //} DrawingGroup clipMaskGroup = new DrawingGroup(); Rect geometryBounds = geometry.Bounds; if (clipGeom != null) { clipMaskGroup.ClipGeometry = clipGeom; SvgUnitType clipUnits = this.ClipUnits; if (clipUnits == SvgUnitType.ObjectBoundingBox) { Rect drawingBounds = geometryBounds; if (transform != null) { drawingBounds = transform.TransformBounds(drawingBounds); } 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; } else { if (transform != null) { clipGeom.Transform = transform; } } } if (maskBrush != null) { DrawingBrush drawingBrush = (DrawingBrush)maskBrush; SvgUnitType maskUnits = this.MaskUnits; SvgUnitType maskContentUnits = this.MaskContentUnits; if (maskUnits == SvgUnitType.ObjectBoundingBox) { Rect drawingBounds = geometryBounds; if (transform != null) { drawingBounds = transform.TransformBounds(drawingBounds); } DrawingGroup maskGroup = drawingBrush.Drawing as DrawingGroup; if (maskGroup != null) { DrawingCollection maskDrawings = maskGroup.Children; for (int i = 0; i < maskDrawings.Count; i++) { Drawing maskDrawing = maskDrawings[i]; GeometryDrawing maskGeomDraw = maskDrawing as GeometryDrawing; if (maskGeomDraw != null) { if (maskGeomDraw.Brush != null) { ConvertColors(maskGeomDraw.Brush); } if (maskGeomDraw.Pen != null) { ConvertColors(maskGeomDraw.Pen.Brush); } } } } if (maskContentUnits == SvgUnitType.ObjectBoundingBox) { TransformGroup transformGroup = new TransformGroup(); // Scale the clip region (at (0, 0)) and translate to the top-left corner of the target. var scaleTransform = new ScaleTransform(drawingBounds.Width, drawingBounds.Height); transformGroup.Children.Add(scaleTransform); var translateTransform = new TranslateTransform(drawingBounds.X, drawingBounds.Y); transformGroup.Children.Add(translateTransform); Matrix scaleMatrix = new Matrix(); Matrix translateMatrix = new Matrix(); scaleMatrix.Scale(drawingBounds.Width, drawingBounds.Height); translateMatrix.Translate(drawingBounds.X, drawingBounds.Y); Matrix matrix = Matrix.Multiply(scaleMatrix, translateMatrix); //maskBrush.Transform = transformGroup; maskBrush.Transform = new MatrixTransform(matrix); } else { drawingBrush.Viewbox = drawingBounds; drawingBrush.ViewboxUnits = BrushMappingMode.Absolute; drawingBrush.Stretch = Stretch.Uniform; drawingBrush.Viewport = drawingBounds; drawingBrush.ViewportUnits = BrushMappingMode.Absolute; } } else { if (transform != null) { maskBrush.Transform = transform; } } clipMaskGroup.OpacityMask = maskBrush; } clipMaskGroup.Children.Add(drawing); drawGroup.Children.Add(clipMaskGroup); } else { drawGroup.Children.Add(drawing); } } // If this is not the child of a "marker", then try rendering a marker... if (!string.Equals(parentNode.LocalName, "marker")) { RenderMarkers(renderer, styleElm, context); } // Register this drawing with the Drawing-Document... if (drawing != null) { this.Rendered(drawing); } }
public override void BeforeRender(WpfDrawingRenderer renderer) { base.BeforeRender(renderer); if (renderer == null) { return; } _isLineSegment = false; _setBrushOpacity = true; WpfDrawingContext context = renderer.Context; //SetQuality(context); //SetTransform(context); //SetMask(context); // _drawGroup = new DrawingGroup(); SvgStyleableElement styleElm = (SvgStyleableElement)_svgElement; float opacityValue = -1; bool isStyleOpacity = false; string opacity = styleElm.GetAttribute("opacity"); if (string.IsNullOrWhiteSpace(opacity)) { opacity = styleElm.GetPropertyValue("opacity"); if (!string.IsNullOrWhiteSpace(opacity)) { isStyleOpacity = true; } } if (!string.IsNullOrWhiteSpace(opacity)) { opacityValue = (float)SvgNumber.ParseNumber(opacity); opacityValue = Math.Min(opacityValue, 1); opacityValue = Math.Max(opacityValue, 0); if (isStyleOpacity && (opacityValue >= 0 && opacityValue < 1)) { _setBrushOpacity = false; if (styleElm.HasAttribute("fill-opacity") || (styleElm.HasAttribute("style") && styleElm.GetAttribute("style").Contains("fill-opacity"))) { _setBrushOpacity = true; } } } //string eVisibility = _svgElement.GetAttribute("visibility"); //string eDisplay = _svgElement.GetAttribute("display"); //if (string.Equals(eVisibility, "hidden") || string.Equals(eDisplay, "none")) //{ // opacityValue = 0; //} //else //{ // string sVisibility = styleElm.GetPropertyValue("visibility"); // string sDisplay = styleElm.GetPropertyValue("display"); // if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none")) // { // opacityValue = 0; // } //} string sVisibility = styleElm.GetPropertyValue("visibility"); if (string.IsNullOrWhiteSpace(sVisibility)) { sVisibility = _svgElement.GetAttribute("visibility"); } string sDisplay = styleElm.GetPropertyValue("display"); if (string.IsNullOrWhiteSpace(sDisplay)) { sDisplay = _svgElement.GetAttribute("display"); } if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none")) { opacityValue = 0; } Transform pathTransform = this.Transform; if (pathTransform != null && !pathTransform.Value.IsIdentity) { if (_drawGroup == null) { _drawGroup = new DrawingGroup(); } _drawGroup.Transform = pathTransform; } else { pathTransform = null; // render any identity transform useless... } Geometry pathClip = this.ClipGeometry; if (pathClip != null && !pathClip.IsEmpty()) { if (_drawGroup == null) { _drawGroup = new DrawingGroup(); } _drawGroup.ClipGeometry = pathClip; } else { pathClip = null; // render any empty geometry useless... } Brush pathMask = this.Masking; if (pathMask != null) { if (_drawGroup == null) { _drawGroup = new DrawingGroup(); } _drawGroup.OpacityMask = pathMask; } if (pathTransform != null || pathClip != null || pathMask != null || (opacityValue >= 0 && opacityValue < 1)) { if (_drawGroup == null) { _drawGroup = new DrawingGroup(); } if ((opacityValue >= 0 && opacityValue < 1)) { _drawGroup.Opacity = opacityValue; } DrawingGroup curGroup = _context.Peek(); Debug.Assert(curGroup != null); if (curGroup != null) { curGroup.Children.Add(_drawGroup); context.Push(_drawGroup); } } else { _drawGroup = null; } if (_drawGroup != null) { string elementClass = this.GetElementClass(); if (!string.IsNullOrWhiteSpace(elementClass) && context.IncludeRuntime) { SvgObject.SetClass(_drawGroup, elementClass); } string elementId = this.GetElementName(); if (!string.IsNullOrWhiteSpace(elementId) && !context.IsRegisteredId(elementId)) { SvgObject.SetName(_drawGroup, elementId); context.RegisterId(elementId); if (context.IncludeRuntime) { SvgObject.SetId(_drawGroup, elementId); } } } }
public override void BeforeRender(WpfDrawingRenderer renderer) { base.BeforeRender(renderer); if (_textElement == null || _textContext == null) { return; } _isTextPath = false; _isGroupAdded = false; _textWidth = 0; _isMeasuring = false; SvgRenderingHint hint = _svgElement.RenderingHint; if (hint == SvgRenderingHint.Clipping) { return; } var context = renderer.Context; var comparer = StringComparison.OrdinalIgnoreCase; // We do not directly render the contents of the clip-path, unless specifically requested... if (string.Equals(_svgElement.ParentNode.LocalName, SvgConstants.TagClipPath, comparer) && !context.RenderingClipRegion) { return; } _context = renderer.Context; SetQuality(context); SetTransform(context); SetClip(_context); SetMask(_context); _drawGroup = new DrawingGroup(); string sVisibility = _textElement.GetPropertyValue(CssConstants.PropVisibility); string sDisplay = _textElement.GetPropertyValue(CssConstants.PropDisplay); if (string.Equals(sVisibility, CssConstants.ValHidden, comparer) || string.Equals(sDisplay, CssConstants.ValNone, comparer)) { _drawGroup.Opacity = 0; } string elementId = this.GetElementName(); if (!string.IsNullOrWhiteSpace(elementId) && !context.IsRegisteredId(elementId)) { SvgObject.SetName(_drawGroup, elementId); context.RegisterId(elementId); if (context.IncludeRuntime) { SvgObject.SetId(_drawGroup, elementId); } } string elementClass = this.GetElementClass(); if (!string.IsNullOrWhiteSpace(elementClass) && context.IncludeRuntime) { SvgObject.SetClass(_drawGroup, elementClass); } Transform textTransform = this.Transform; if (textTransform != null && !textTransform.Value.IsIdentity) { _drawGroup.Transform = textTransform; } else { textTransform = null; // render any identity transform useless... } Geometry textClip = this.ClipGeometry; if (textClip != null && !textClip.IsEmpty()) { _drawGroup.ClipGeometry = textClip; } else { textClip = null; // render any empty geometry useless... } Brush textMask = this.Masking; if (textMask != null) { _drawGroup.OpacityMask = textMask; } if (textTransform != null || textClip != null || textMask != null) { DrawingGroup curGroup = _context.Peek(); Debug.Assert(curGroup != null); if (curGroup != null) { curGroup.Children.Add(_drawGroup); context.Push(_drawGroup); _isGroupAdded = true; } } _drawContext = _drawGroup.Open(); _horzRenderer.Initialize(_drawContext, _context); _vertRenderer.Initialize(_drawContext, _context); _pathRenderer.Initialize(_drawContext, _context); }
public override void Render(WpfDrawingRenderer renderer) { _isAggregated = false; if (_isLayer) { base.Render(renderer); return; } WpfDrawingContext context = renderer.Context; Geometry clipGeom = this.ClipGeometry; Transform transform = this.Transform; float opacityValue = -1; SvgAElement element = (SvgAElement)_svgElement; string opacity = element.GetPropertyValue("opacity"); if (string.IsNullOrWhiteSpace(opacity)) { opacity = element.GetPropertyValue("opacity"); } if (opacity != null && opacity.Length > 0) { opacityValue = (float)SvgNumber.ParseNumber(opacity); opacityValue = Math.Min(opacityValue, 1); opacityValue = Math.Max(opacityValue, 0); } WpfLinkVisitor linkVisitor = context.LinkVisitor; if (linkVisitor != null || clipGeom != null || transform != null || opacityValue >= 0) { _drawGroup = new DrawingGroup(); string elementId = this.GetElementName(); if (!string.IsNullOrWhiteSpace(elementId) && !context.IsRegisteredId(elementId)) { SvgObject.SetName(_drawGroup, elementId); context.RegisterId(elementId); if (context.IncludeRuntime) { SvgObject.SetId(_drawGroup, elementId); } } string elementClass = this.GetElementClass(); if (!string.IsNullOrWhiteSpace(elementClass) && context.IncludeRuntime) { SvgObject.SetClass(_drawGroup, elementClass); } DrawingGroup currentGroup = context.Peek(); if (currentGroup == null) { throw new InvalidOperationException("An existing group is expected."); } if (linkVisitor != null && linkVisitor.Aggregates && context.Links != null) { if (!linkVisitor.Exists(elementId)) { context.Links.Children.Add(_drawGroup); } } else { currentGroup.Children.Add(_drawGroup); } context.Push(_drawGroup); if (clipGeom != null) { _drawGroup.ClipGeometry = clipGeom; } if (transform != null) { _drawGroup.Transform = transform; } if (opacityValue >= 0) { _drawGroup.Opacity = opacityValue; } string sVisibility = element.GetPropertyValue("visibility"); string sDisplay = element.GetPropertyValue("display"); if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none")) { opacityValue = 0; _drawGroup.Opacity = 0; } if (linkVisitor != null) { linkVisitor.Visit(_drawGroup, element, context, opacityValue); _isAggregated = linkVisitor.IsAggregate; } } base.Render(renderer); }
// disable default rendering public override void BeforeRender(WpfDrawingRenderer renderer) { base.BeforeRender(renderer); if (_hostElement != null && _paintContext != null) { _paintContext.TargetId = _hostElement.UniqueId; } _matrix = Matrix.Identity; WpfDrawingContext context = renderer.Context; //SetQuality(context); //SetTransform(context); //SetClip(_context); //SetMask(_context); _drawGroup = new DrawingGroup(); string sVisibility = _markerElement.GetPropertyValue("visibility"); string sDisplay = _markerElement.GetPropertyValue("display"); if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none")) { // A 'marker' element with 'display' set to 'none' on that element or any // ancestor is rendered when referenced by another element. // _drawGroup.Opacity = 0; } string elementId = this.GetElementName(); if (!string.IsNullOrWhiteSpace(elementId) && !context.IsRegisteredId(elementId)) { SvgObject.SetName(_drawGroup, elementId); context.RegisterId(elementId); if (context.IncludeRuntime) { SvgObject.SetId(_drawGroup, elementId); } } string elementClass = this.GetElementClass(); if (!string.IsNullOrWhiteSpace(elementClass) && context.IncludeRuntime) { SvgObject.SetClass(_drawGroup, elementClass); } //Transform markerTransform = this.Transform; //if (markerTransform != null && !markerTransform.Value.IsIdentity) //{ // _drawGroup.Transform = markerTransform; //} //else //{ // markerTransform = null; // render any identity transform useless... //} Geometry markerClip = this.ClipGeometry; if (markerClip != null && !markerClip.IsEmpty()) { _drawGroup.ClipGeometry = markerClip; } else { markerClip = null; // render any empty geometry useless... } Brush markerMask = this.Masking; if (markerMask != null) { _drawGroup.OpacityMask = markerMask; } DrawingGroup currentGroup = context.Peek(); if (currentGroup == null) { throw new InvalidOperationException("An existing group is expected."); } currentGroup.Children.Add(_drawGroup); context.Push(_drawGroup); }