public override Brush GetBrush(SvgVisualElement renderingElement, SvgRenderer renderer, float opacity) { LoadStops(renderingElement); try { if (this.GradientUnits == SvgCoordinateUnits.ObjectBoundingBox) { renderer.Boundable(renderingElement); } var origin = renderer.Boundable().Location; var centerPoint = CalculateCenterPoint(renderer, origin); var focalPoint = CalculateFocalPoint(renderer, origin); var specifiedRadius = CalculateRadius(renderer); var effectiveRadius = CalculateEffectiveRadius(renderingElement, centerPoint, specifiedRadius); var brush = new PathGradientBrush(CreateGraphicsPath(origin, centerPoint, effectiveRadius)) { InterpolationColors = CalculateColorBlend(renderer, opacity, specifiedRadius, effectiveRadius), CenterPoint = focalPoint }; Debug.Assert(brush.Rectangle.Contains(renderingElement.Bounds), "Brush rectangle does not contain rendering element bounds!"); return(brush); } finally { if (this.GradientUnits == SvgCoordinateUnits.ObjectBoundingBox) { renderer.PopBoundable(); } } }
public override Brush GetBrush(SvgVisualElement renderingElement, SvgRenderer renderer, float opacity) { LoadStops(renderingElement); if (IsInvalid) { return(null); } try { if (this.GradientUnits == SvgCoordinateUnits.ObjectBoundingBox) { renderer.Boundable(renderingElement); } var specifiedStart = CalculateStart(renderer); var specifiedEnd = CalculateEnd(renderer); var effectiveStart = specifiedStart; var effectiveEnd = specifiedEnd; if (NeedToExpandGradient(renderingElement, specifiedStart, specifiedEnd)) { var expansion = ExpandGradient(renderingElement, specifiedStart, specifiedEnd); effectiveStart = expansion.StartPoint; effectiveEnd = expansion.EndPoint; } return(new LinearGradientBrush(effectiveStart, effectiveEnd, System.Drawing.Color.Transparent, System.Drawing.Color.Transparent) { InterpolationColors = CalculateColorBlend(renderer, opacity, specifiedStart, effectiveStart, specifiedEnd, effectiveEnd), WrapMode = WrapMode.TileFlipX }); } finally { if (this.GradientUnits == SvgCoordinateUnits.ObjectBoundingBox) { renderer.PopBoundable(); } } }
/// <summary> /// Gets the <see cref="GraphicsPath"/> for this element. /// </summary> /// <value></value> public override System.Drawing.Drawing2D.GraphicsPath Path(SvgRenderer renderer) { // Make sure the path is always null if there is no text //if there is a TSpan inside of this text element then path should not be null (even if this text is empty!) if ((string.IsNullOrEmpty(this.Text) || this.Text.Trim().Length < 1) && this.Children.Where(x => x is SvgTextSpan).Select(x => x as SvgTextSpan).Count() == 0) { return(_path = null); } //NOT SURE WHAT THIS IS ABOUT - Path gets created again anyway - WTF? // When an empty string is passed to GraphicsPath, it rises an InvalidArgumentException... if (_path == null || this.IsPathDirty) { renderer = (renderer ?? SvgRenderer.FromNull()); // Measure the overall bounds of all the text var boundsData = GetTextBounds(renderer); var font = GetFont(renderer); SvgTextBase innerText; float x = (_x.Count < 1 ? _calcX : _x[0].ToDeviceValue(renderer, UnitRenderingType.HorizontalOffset, this)) + (_dx.Count < 1 ? 0 : _dx[0].ToDeviceValue(renderer, UnitRenderingType.Horizontal, this)); float y = (_y.Count < 1 ? _calcY : _y[0].ToDeviceValue(renderer, UnitRenderingType.VerticalOffset, this)) + (_dy.Count < 1 ? 0 : _dy[0].ToDeviceValue(renderer, UnitRenderingType.Vertical, this)); _path = new GraphicsPath(); _path.StartFigure(); // Determine the location of the start point switch (this.TextAnchor) { case SvgTextAnchor.Middle: x -= (boundsData.Bounds.Width / 2); break; case SvgTextAnchor.End: x -= boundsData.Bounds.Width; break; } try { renderer.Boundable(new FontBoundable(font)); switch (this.BaselineShift) { case null: case "": case "baseline": case "inherit": // do nothing break; case "sub": y += new SvgUnit(SvgUnitType.Ex, 1).ToDeviceValue(renderer, UnitRenderingType.Vertical, this); break; case "super": y -= new SvgUnit(SvgUnitType.Ex, 1).ToDeviceValue(renderer, UnitRenderingType.Vertical, this); break; default: var convert = new SvgUnitConverter(); var shift = (SvgUnit)convert.ConvertFromInvariantString(this.BaselineShift); y -= shift.ToDeviceValue(renderer, UnitRenderingType.Vertical, this); break; } } finally { renderer.PopBoundable(); } NodeBounds data; var yCummOffset = 0.0f; for (var i = 0; i < boundsData.Nodes.Count; i++) { data = boundsData.Nodes[i]; innerText = data.Node as SvgTextBase; if (innerText == null) { // Minus FontSize because the x/y coords mark the bottom left, not bottom top. DrawString(renderer, _path, x + data.xOffset, y - boundsData.Bounds.Height, font, PrepareText(data.Node.Content, i > 0 && boundsData.Nodes[i - 1].Node is SvgTextBase, i < boundsData.Nodes.Count - 1 && boundsData.Nodes[i + 1].Node is SvgTextBase)); } else { innerText._calcX = x + data.xOffset; innerText._calcY = y + yCummOffset; if (innerText.Dy.Count == 1) { yCummOffset += innerText.Dy[0].ToDeviceValue(renderer, UnitRenderingType.Vertical, this); } } } _path.CloseFigure(); this.IsPathDirty = false; } return(_path); }
/// <summary> /// Gets a <see cref="Brush"/> representing the current paint server. /// </summary> /// <param name="renderingElement">The owner <see cref="SvgVisualElement"/>.</param> /// <param name="opacity">The opacity of the brush.</param> public override Brush GetBrush(SvgVisualElement renderingElement, SvgRenderer renderer, float opacity) { // If there aren't any children, return null if (this.Children.Count == 0) { return(null); } // Can't render if there are no dimensions if (this._width.Value == 0.0f || this._height.Value == 0.0f) { return(null); } try { if (this.PatternUnits == SvgCoordinateUnits.ObjectBoundingBox) { renderer.Boundable(renderingElement); } float width = this._width.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this); float height = this._height.ToDeviceValue(renderer, UnitRenderingType.Vertical, this); Matrix patternMatrix = new Matrix(); // Apply a translate if needed if (this._x.Value > 0.0f || this._y.Value > 0.0f) { float x = this._x.ToDeviceValue(renderer, UnitRenderingType.HorizontalOffset, this); float y = this._y.ToDeviceValue(renderer, UnitRenderingType.VerticalOffset, this); patternMatrix.Translate(x + -1.0f, y + -1.0f); } else { patternMatrix.Translate(-1, -1); } if (this.ViewBox.Height > 0 || this.ViewBox.Width > 0) { patternMatrix.Scale(this.Width.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this) / this.ViewBox.Width, this.Height.ToDeviceValue(renderer, UnitRenderingType.Vertical, this) / this.ViewBox.Height); } Bitmap image = new Bitmap((int)width, (int)height); using (SvgRenderer iRenderer = SvgRenderer.FromImage(image)) { iRenderer.Boundable((_patternContentUnits == SvgCoordinateUnits.ObjectBoundingBox) ? new GenericBoundable(0, 0, width, height) : renderer.Boundable()); iRenderer.Transform = patternMatrix; iRenderer.CompositingQuality = CompositingQuality.HighQuality; iRenderer.SmoothingMode = SmoothingMode.AntiAlias; iRenderer.PixelOffsetMode = PixelOffsetMode.Half; foreach (SvgElement child in this.Children) { child.RenderElement(iRenderer); } iRenderer.Save(); } image.Save(string.Format(@"C:\test{0:D3}.png", imgNumber++)); TextureBrush textureBrush = new TextureBrush(image); return(textureBrush); } finally { if (this.PatternUnits == SvgCoordinateUnits.ObjectBoundingBox) { renderer.PopBoundable(); } } }