private void RenderInternal(ISvgRenderer renderer, bool renderFilter) { if (!(renderFilter && RenderFilter(renderer))) { var opacity = FixOpacityValue(Opacity); if (opacity == 1f) { RenderInternal(renderer); } else { IsPathDirty = true; var bounds = Renderable ? Bounds : Path(null).GetBounds(); IsPathDirty = true; if (bounds.Width > 0f && bounds.Height > 0f) { using (var canvas = new Bitmap((int)Math.Ceiling(bounds.Width), (int)Math.Ceiling(bounds.Height))) { using (var canvasRenderer = SvgRenderer.FromImage(canvas)) { canvasRenderer.SetBoundable(renderer.GetBoundable()); canvasRenderer.TranslateTransform(-bounds.X, -bounds.Y); RenderInternal(canvasRenderer); } var srcRect = new RectangleF(0f, 0f, bounds.Width, bounds.Height); renderer.DrawImage(canvas, bounds, srcRect, GraphicsUnit.Pixel, opacity); } } } } }
protected override void Render(ISvgRenderer renderer) { switch (this.Overflow) { case SvgOverflow.Auto: case SvgOverflow.Visible: case SvgOverflow.Inherit: base.Render(renderer); break; default: var prevClip = renderer.GetClip(); try { var size = (this.Parent == null ? renderer.GetBoundable().Bounds.Size : GetDimensions()); var clip = new RectangleF(this.X.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this), this.Y.ToDeviceValue(renderer, UnitRenderingType.Vertical, this), size.Width, size.Height); renderer.SetClip(new Region(clip), CombineMode.Intersect); base.Render(renderer); } finally { renderer.SetClip(prevClip, CombineMode.Replace); } break; } }
private void Render(ISvgRenderer renderer, bool renderFilter) { if (Visible && Displayable && (!Renderable || Path(renderer) != null)) { if (!(renderFilter && RenderFilter(renderer))) { try { if (PushTransforms(renderer)) { SetClip(renderer); if (Renderable) { var opacity = Math.Min(Math.Max(Opacity, 0f), 1f); if (opacity == 1f) { RenderFillAndStroke(renderer); } else { IsPathDirty = true; var bounds = Bounds; IsPathDirty = true; if (bounds.Width > 0f && bounds.Height > 0f) { using (var canvas = new Bitmap((int)Math.Ceiling(bounds.Width), (int)Math.Ceiling(bounds.Height))) { using (var canvasRenderer = SvgRenderer.FromImage(canvas)) { canvasRenderer.SetBoundable(renderer.GetBoundable()); canvasRenderer.TranslateTransform(-bounds.X, -bounds.Y); RenderFillAndStroke(canvasRenderer); } var srcRect = new RectangleF(0f, 0f, bounds.Width, bounds.Height); renderer.DrawImage(canvas, bounds, srcRect, GraphicsUnit.Pixel, opacity); } } } } else { RenderChildren(renderer); } ResetClip(renderer); } } finally { PopTransforms(renderer); } } } }
private Bitmap CreateSourceGraphic() { var graphic = new Bitmap((int)(_bounds.Width + 2 * _inflate * _bounds.Width + _bounds.X), (int)(_bounds.Height + 2 * _inflate * _bounds.Height + _bounds.Y)); using (var renderer = SvgRenderer.FromImage(graphic)) { renderer.SetBoundable(_renderer.GetBoundable()); var transform = new Matrix(); transform.Translate(_bounds.Width * _inflate, _bounds.Height * _inflate); renderer.Transform = transform; //renderer.Transform = _renderer.Transform; //renderer.Clip = _renderer.Clip; _renderMethod.Invoke(renderer); } return(graphic); }
private void Render(ISvgRenderer renderer, bool renderFilter) { if (this.Visible && this.Displayable && this.PushTransforms(renderer) && (!this.Renderable || this.Path(renderer) != null)) { if (!(renderFilter && this.RenderFilter(renderer))) { this.SetClip(renderer); if (this.Renderable) { var opacity = Math.Min(Math.Max(this.Opacity, 0), 1); if (opacity == 1f) { this.RenderFillAndStroke(renderer); } else { IsPathDirty = true; var bounds = this.Bounds; IsPathDirty = true; using (var canvas = new Bitmap((int)Math.Ceiling(bounds.Width), (int)Math.Ceiling(bounds.Height))) { using (var canvasRenderer = SvgRenderer.FromImage(canvas)) { canvasRenderer.SetBoundable(renderer.GetBoundable()); canvasRenderer.TranslateTransform(-bounds.X, -bounds.Y); this.RenderFillAndStroke(canvasRenderer); } var srcRect = new RectangleF(0f, 0f, bounds.Width, bounds.Height); renderer.DrawImage(canvas, bounds, srcRect, GraphicsUnit.Pixel, opacity); } } } else { base.RenderChildren(renderer); } this.ResetClip(renderer); this.PopTransforms(renderer); } } }
/// <summary> /// Gets a <see cref="ColorBlend"/> representing the <see cref="SvgGradientServer"/>'s gradient stops. /// </summary> /// <param name="renderer">The renderer <see cref="ISvgRenderer"/>.</param> /// <param name="opacity">The opacity of the colour blend.</param> /// <param name="radial">True if it's a radial gradiant.</param> protected ColorBlend GetColorBlend(ISvgRenderer renderer, float opacity, bool radial) { var colourBlends = Stops.Count; var insertStart = false; var insertEnd = false; //gradient.Transform = renderingElement.Transforms.Matrix; //stops should be processed in reverse order if it's a radial gradient // May need to increase the number of colour blends because the range *must* be from 0.0 to 1.0. // E.g. 0.5 - 0.8 isn't valid therefore the rest need to be calculated. // If the first stop doesn't start at zero if (Stops[0].Offset.Value > 0f) { colourBlends++; if (radial) { insertEnd = true; } else { insertStart = true; } } // If the last stop doesn't end at 1 a stop var lastValue = Stops[Stops.Count - 1].Offset.Value; if (lastValue < 100f || lastValue < 1f) { colourBlends++; if (radial) { insertStart = true; } else { insertEnd = true; } } var blend = new ColorBlend(colourBlends); // Set positions and colour values var actualStops = 0; for (var i = 0; i < colourBlends; i++) { var currentStop = Stops[radial ? Stops.Count - 1 - actualStops : actualStops]; var boundWidth = renderer.GetBoundable().Bounds.Width; var mergedOpacity = opacity * currentStop.Opacity; var position = radial ? 1 - (currentStop.Offset.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this) / boundWidth) : (currentStop.Offset.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this) / boundWidth); position = (float)Math.Round(position, 1, MidpointRounding.AwayFromZero); var colour = System.Drawing.Color.FromArgb((int)Math.Round(mergedOpacity * 255), currentStop.GetColor(this)); actualStops++; // Insert this colour before itself at position 0 if (insertStart && i == 0) { blend.Positions[i] = 0.0f; blend.Colors[i] = colour; i++; } blend.Positions[i] = position; blend.Colors[i] = colour; // Insert this colour after itself at position 0 if (insertEnd && i == colourBlends - 2) { i++; blend.Positions[i] = 1.0f; blend.Colors[i] = colour; } } return(blend); }
/// <summary> /// Converts the current unit to one that can be used at render time. /// </summary> /// <param name="boundable">The container element used as the basis for calculations</param> /// <returns>The representation of the current unit in a device value (usually pixels).</returns> public float ToDeviceValue(ISvgRenderer renderer, UnitRenderingType renderType, SvgElement owner) { // If it's already been calculated if (this._deviceValue.HasValue) { return(this._deviceValue.Value); } if (this._value == 0.0f) { this._deviceValue = 0.0f; return(this._deviceValue.Value); } // http://www.w3.org/TR/CSS21/syndata.html#values // http://www.w3.org/TR/SVG11/coords.html#Units const float cmInInch = 2.54f; int ppi = SvgDocument.PointsPerInch; var type = this.Type; var value = this.Value; float points; switch (type) { case SvgUnitType.Em: using (var currFont = GetFont(renderer, owner)) { if (currFont == null) { points = (float)(value * 9); _deviceValue = (points / 72.0f) * ppi; } else { _deviceValue = value * (currFont.SizeInPoints / 72.0f) * ppi; } } break; case SvgUnitType.Ex: using (var currFont = GetFont(renderer, owner)) { if (currFont == null) { points = (float)(value * 9); _deviceValue = (points * 0.5f / 72.0f) * ppi; } else { _deviceValue = value * 0.5f * (currFont.SizeInPoints / 72.0f) * ppi; } break; } case SvgUnitType.Centimeter: _deviceValue = (float)((value / cmInInch) * ppi); break; case SvgUnitType.Inch: _deviceValue = value * ppi; break; case SvgUnitType.Millimeter: _deviceValue = (float)((value / 10) / cmInInch) * ppi; break; case SvgUnitType.Pica: _deviceValue = ((value * 12) / 72) * ppi; break; case SvgUnitType.Point: _deviceValue = (value / 72) * ppi; break; case SvgUnitType.Pixel: _deviceValue = value; break; case SvgUnitType.User: _deviceValue = value; break; case SvgUnitType.Percentage: // Can't calculate if there is no style owner var boundable = (renderer == null ? (owner == null ? null : owner.OwnerDocument) : renderer.GetBoundable()); if (boundable == null) { _deviceValue = value; break; } System.Drawing.SizeF size = boundable.Bounds.Size; switch (renderType) { case UnitRenderingType.Horizontal: _deviceValue = (size.Width / 100) * value; break; case UnitRenderingType.HorizontalOffset: _deviceValue = (size.Width / 100) * value + boundable.Location.X; break; case UnitRenderingType.Vertical: _deviceValue = (size.Height / 100) * value; break; case UnitRenderingType.VerticalOffset: _deviceValue = (size.Height / 100) * value + boundable.Location.Y; break; default: _deviceValue = (float)(Math.Sqrt(Math.Pow(size.Width, 2) + Math.Pow(size.Height, 2)) / Math.Sqrt(2) * value / 100.0); break; } break; default: _deviceValue = value; break; } return(this._deviceValue.Value); }
public override Brush GetBrush(SvgVisualElement renderingElement, ISvgRenderer renderer, float opacity, bool forStroke = false) { LoadStops(renderingElement); if (this.Stops.Count < 1) { return(null); } if (this.Stops.Count == 1) { var stopColor = this.Stops[0].GetColor(renderingElement); var alpha = (int)Math.Round((opacity * (stopColor.A / 255.0f)) * 255); var colour = System.Drawing.Color.FromArgb(alpha, stopColor); return(new SolidBrush(colour)); } try { if (this.GradientUnits == SvgCoordinateUnits.ObjectBoundingBox) { renderer.SetBoundable(renderingElement); } var points = new PointF[] { SvgUnit.GetDevicePoint(NormalizeUnit(this.X1), NormalizeUnit(this.Y1), renderer, this), SvgUnit.GetDevicePoint(NormalizeUnit(this.X2), NormalizeUnit(this.Y2), renderer, this) }; var bounds = renderer.GetBoundable().Bounds; if (bounds.Width <= 0 || bounds.Height <= 0 || ((points[0].X == points[1].X) && (points[0].Y == points[1].Y))) { if (this.GetCallback != null) { return(GetCallback().GetBrush(renderingElement, renderer, opacity, forStroke)); } return(null); } using (var transform = EffectiveGradientTransform) { transform.Translate(bounds.X, bounds.Y, MatrixOrder.Prepend); if (this.GradientUnits == SvgCoordinateUnits.ObjectBoundingBox) { // Transform a normal (i.e. perpendicular line) according to the transform transform.Scale(bounds.Width, bounds.Height, MatrixOrder.Prepend); } transform.TransformPoints(points); } points[0].X = (float)Math.Round(points[0].X, 4); points[0].Y = (float)Math.Round(points[0].Y, 4); points[1].X = (float)Math.Round(points[1].X, 4); points[1].Y = (float)Math.Round(points[1].Y, 4); if (this.GradientUnits == SvgCoordinateUnits.ObjectBoundingBox) { // Transform the normal line back to a line such that the gradient still starts in the correct corners, but // has the proper normal vector based on the transforms. If you work out the geometry, these formulas should work. var midPoint = new PointF((points[0].X + points[1].X) / 2, (points[0].Y + points[1].Y) / 2); var dy = points[1].Y - points[0].Y; var dx = points[1].X - points[0].X; var x1 = points[0].X; var y2 = points[1].Y; if (dx != 0f && dy != 0f) { var startX = (float)((dy * dx * (midPoint.Y - y2) + Math.Pow(dx, 2) * midPoint.X + Math.Pow(dy, 2) * x1) / (Math.Pow(dx, 2) + Math.Pow(dy, 2))); var endY = dy * (startX - x1) / dx + y2; points[0] = new PointF(startX, midPoint.Y + (midPoint.Y - endY)); points[1] = new PointF(midPoint.X + (midPoint.X - startX), endY); } } var effectiveStart = points[0]; var effectiveEnd = points[1]; if (PointsToMove(renderingElement, points[0], points[1]) > LinePoints.None) { var expansion = ExpandGradient(renderingElement, points[0], points[1]); effectiveStart = expansion.StartPoint; effectiveEnd = expansion.EndPoint; } var result = new LinearGradientBrush(effectiveStart, effectiveEnd, System.Drawing.Color.Transparent, System.Drawing.Color.Transparent) { InterpolationColors = CalculateColorBlend(renderer, opacity, points[0], effectiveStart, points[1], effectiveEnd), WrapMode = WrapMode.TileFlipX }; return(result); } finally { if (this.GradientUnits == SvgCoordinateUnits.ObjectBoundingBox) { renderer.PopBoundable(); } } }
protected override Brush CreateBrush(SvgVisualElement renderingElement, ISvgRenderer renderer, float opacity, bool forStroke) { // TODO: figure out how to do the brush transform in the presence of FocalRadius try { if (this.GradientUnits == SvgCoordinateUnits.ObjectBoundingBox) { renderer.SetBoundable(renderingElement); } // Calculate the path and transform it appropriately var center = new PointF(NormalizeUnit(CenterX).ToDeviceValue(renderer, UnitRenderingType.Horizontal, this), NormalizeUnit(CenterY).ToDeviceValue(renderer, UnitRenderingType.Vertical, this)); var focals = new PointF[] { new PointF(NormalizeUnit(FocalX).ToDeviceValue(renderer, UnitRenderingType.Horizontal, this), NormalizeUnit(FocalY).ToDeviceValue(renderer, UnitRenderingType.Vertical, this)) }; var specifiedRadius = NormalizeUnit(Radius).ToDeviceValue(renderer, UnitRenderingType.Other, this); var path = new GraphicsPath(); path.AddEllipse( center.X - specifiedRadius, center.Y - specifiedRadius, specifiedRadius * 2, specifiedRadius * 2 ); using (var transform = EffectiveGradientTransform) { var bounds = renderer.GetBoundable().Bounds; transform.Translate(bounds.X, bounds.Y, MatrixOrder.Prepend); if (this.GradientUnits == SvgCoordinateUnits.ObjectBoundingBox) { transform.Scale(bounds.Width, bounds.Height, MatrixOrder.Prepend); } path.Transform(transform); transform.TransformPoints(focals); } // Calculate any required scaling var scaleBounds = RectangleF.Inflate(renderingElement.Bounds, renderingElement.StrokeWidth, renderingElement.StrokeWidth); var scale = CalcScale(scaleBounds, path); // Not ideal, but this makes sure that the rest of the shape gets properly filled or drawn if (scale > 1.0f && SpreadMethod == SvgGradientSpreadMethod.Pad) { var stop = Stops.Last(); var origColor = stop.GetColor(renderingElement); var renderColor = System.Drawing.Color.FromArgb((int)Math.Round(opacity * stop.StopOpacity * 255), origColor); var origClip = renderer.GetClip(); try { using (var solidBrush = new SolidBrush(renderColor)) { var newClip = origClip.Clone(); newClip.Exclude(path); renderer.SetClip(newClip); var renderPath = (GraphicsPath)renderingElement.Path(renderer); if (forStroke) { using (var pen = new Pen(solidBrush, renderingElement.StrokeWidth.ToDeviceValue(renderer, UnitRenderingType.Other, renderingElement))) { renderer.DrawPath(pen, renderPath); } } else { renderer.FillPath(solidBrush, renderPath); } } } finally { renderer.SetClip(origClip); } } // Get the color blend and any tweak to the scaling var blend = CalculateColorBlend(renderer, opacity, scale, out scale); // Transform the path based on the scaling var gradBounds = path.GetBounds(); var transCenter = new PointF(gradBounds.Left + gradBounds.Width / 2, gradBounds.Top + gradBounds.Height / 2); using (var scaleMat = new Matrix()) { scaleMat.Translate(-1 * transCenter.X, -1 * transCenter.Y, MatrixOrder.Append); scaleMat.Scale(scale, scale, MatrixOrder.Append); scaleMat.Translate(transCenter.X, transCenter.Y, MatrixOrder.Append); path.Transform(scaleMat); } // calculate the brush var brush = new PathGradientBrush(path); brush.CenterPoint = focals[0]; brush.InterpolationColors = blend; return(brush); } finally { if (this.GradientUnits == SvgCoordinateUnits.ObjectBoundingBox) { renderer.PopBoundable(); } } }
/// <summary> /// Gets a <see cref="Brush"/> representing the current paint server. /// </summary> /// <param name="renderingElement">The owner <see cref="SvgVisualElement"/>.</param> /// <param name="renderer">The renderer object.</param> /// <param name="opacity">The opacity of the brush.</param> /// <param name="forStroke">Not used.</param> public override Brush GetBrush(SvgVisualElement renderingElement, ISvgRenderer renderer, float opacity, bool forStroke = false) { var chain = new List <SvgPatternServer>(); var curr = this; do { chain.Add(curr); curr = SvgDeferredPaintServer.TryGet <SvgPatternServer>(curr.InheritGradient, renderingElement); } while (curr != null); var firstChildren = chain.Where(p => p.Children.Count > 0).FirstOrDefault(); if (firstChildren == null) { return(null); } var firstX = chain.Where(p => p.X != null && p.X != SvgUnit.None).FirstOrDefault(); var firstY = chain.Where(p => p.Y != null && p.Y != SvgUnit.None).FirstOrDefault(); var firstWidth = chain.Where(p => p.Width != null && p.Width != SvgUnit.None).FirstOrDefault(); var firstHeight = chain.Where(p => p.Height != null && p.Height != SvgUnit.None).FirstOrDefault(); if (firstWidth == null || firstHeight == null) { return(null); } var firstPatternUnit = chain.Where(p => p.PatternUnits != SvgCoordinateUnits.Inherit).FirstOrDefault(); var firstPatternContentUnit = chain.Where(p => p.PatternContentUnits != SvgCoordinateUnits.Inherit).FirstOrDefault(); var firstViewBox = chain.Where(p => p.ViewBox != null && p.ViewBox != SvgViewBox.Empty).FirstOrDefault(); var xUnit = firstX == null ? new SvgUnit(0f) : firstX.X; var yUnit = firstY == null ? new SvgUnit(0f) : firstY.Y; var widthUnit = firstWidth.Width; var heightUnit = firstHeight.Height; var patternUnits = firstPatternUnit == null ? SvgCoordinateUnits.ObjectBoundingBox : firstPatternUnit.PatternUnits; var patternContentUnits = firstPatternContentUnit == null ? SvgCoordinateUnits.UserSpaceOnUse : firstPatternContentUnit.PatternContentUnits; var viewBox = firstViewBox == null ? SvgViewBox.Empty : firstViewBox.ViewBox; var isPatternObjectBoundingBox = patternUnits == SvgCoordinateUnits.ObjectBoundingBox; try { if (isPatternObjectBoundingBox) { renderer.SetBoundable(renderingElement); } var x = xUnit.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this); var y = yUnit.ToDeviceValue(renderer, UnitRenderingType.Vertical, this); var width = widthUnit.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this); var height = heightUnit.ToDeviceValue(renderer, UnitRenderingType.Vertical, this); if (isPatternObjectBoundingBox) { var bounds = renderer.GetBoundable().Bounds; // Boundary without stroke is expect... if (xUnit.Type != SvgUnitType.Percentage) { x *= bounds.Width; } if (yUnit.Type != SvgUnitType.Percentage) { y *= bounds.Height; } if (widthUnit.Type != SvgUnitType.Percentage) { width *= bounds.Width; } if (heightUnit.Type != SvgUnitType.Percentage) { height *= bounds.Height; } x += bounds.X; y += bounds.Y; } var tile = new Bitmap((int)Math.Ceiling(width), (int)Math.Ceiling(height)); using (var tileRenderer = SvgRenderer.FromImage(tile)) { tileRenderer.SetBoundable(renderingElement); if (viewBox != SvgViewBox.Empty) { var bounds = tileRenderer.GetBoundable().Bounds; tileRenderer.ScaleTransform(width / viewBox.Width, height / viewBox.Height); } else if (patternContentUnits == SvgCoordinateUnits.ObjectBoundingBox) { var bounds = tileRenderer.GetBoundable().Bounds; tileRenderer.ScaleTransform(bounds.Width, bounds.Height); } foreach (var child in firstChildren.Children) { child.RenderElement(tileRenderer); } } using (var transform = EffectivePatternTransform) { var textureBrush = new TextureBrush(tile, new RectangleF(0f, 0f, width, height)) { Transform = transform }; textureBrush.TranslateTransform(x, y); return(textureBrush); } } finally { if (isPatternObjectBoundingBox) { renderer.PopBoundable(); } } }
private void RenderInternal(ISvgRenderer renderer, bool renderFilter) { if (!(renderFilter && RenderFilter(renderer))) { var opacity = FixOpacityValue(Opacity); if (opacity == 1f) { if (Renderable) { RenderInternal(renderer, RenderFillAndStroke); } else { RenderInternal(renderer, RenderChildren); } } else { IsPathDirty = true; var bounds = Renderable ? Bounds : Path(null).GetBounds(); IsPathDirty = true; if (bounds.Width > 0f && bounds.Height > 0f) { var scaleX = 1f; var scaleY = 1f; using (var transform = renderer.Transform) { scaleX = Math.Max(scaleX, Math.Abs(transform.Elements[0])); scaleY = Math.Max(scaleY, Math.Abs(transform.Elements[3])); } using (var canvas = new Bitmap((int)Math.Ceiling(bounds.Width * scaleX), (int)Math.Ceiling(bounds.Height * scaleY))) { using (var canvasRenderer = SvgRenderer.FromImage(canvas)) { canvasRenderer.SetBoundable(renderer.GetBoundable()); canvasRenderer.TranslateTransform(-bounds.X, -bounds.Y); canvasRenderer.ScaleTransform(scaleX, scaleY); if (Renderable) { RenderInternal(canvasRenderer, RenderFillAndStroke); } else { RenderChildren(canvasRenderer); } } var srcRect = new RectangleF(0f, 0f, bounds.Width * scaleX, bounds.Height * scaleY); if (Renderable) { renderer.DrawImage(canvas, bounds, srcRect, GraphicsUnit.Pixel, opacity); } else { RenderInternal(renderer, r => r.DrawImage(canvas, bounds, srcRect, GraphicsUnit.Pixel, opacity)); } } } } } }
/// <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, ISvgRenderer renderer, float opacity, bool forStroke = false) { var chain = new List <SvgPatternServer>(); var curr = this; while (curr != null) { chain.Add(curr); curr = SvgDeferredPaintServer.TryGet <SvgPatternServer>(curr._inheritGradient, renderingElement); } var childElem = chain.Where((p) => p.Children != null && p.Children.Count > 0).FirstOrDefault(); if (childElem == null) { return(null); } var widthElem = chain.Where((p) => p.Width != null && p.Width != SvgUnit.None).FirstOrDefault(); var heightElem = chain.Where((p) => p.Height != null && p.Height != SvgUnit.None).FirstOrDefault(); if (widthElem == null && heightElem == null) { return(null); } var viewBoxElem = chain.Where((p) => p.ViewBox != null && p.ViewBox != SvgViewBox.Empty).FirstOrDefault(); var viewBox = viewBoxElem == null ? SvgViewBox.Empty : viewBoxElem.ViewBox; var xElem = chain.Where((p) => p.X != null && p.X != SvgUnit.None).FirstOrDefault(); var yElem = chain.Where((p) => p.Y != null && p.Y != SvgUnit.None).FirstOrDefault(); var xUnit = xElem == null ? SvgUnit.Empty : xElem.X; var yUnit = yElem == null ? SvgUnit.Empty : yElem.Y; var patternUnitElem = chain.Where((p) => p.PatternUnits != SvgCoordinateUnits.Inherit).FirstOrDefault(); var patternUnits = (patternUnitElem == null ? SvgCoordinateUnits.ObjectBoundingBox : patternUnitElem.PatternUnits); var patternContentUnitElem = chain.Where((p) => p.PatternContentUnits != SvgCoordinateUnits.Inherit).FirstOrDefault(); var patternContentUnits = (patternContentUnitElem == null ? SvgCoordinateUnits.UserSpaceOnUse : patternContentUnitElem.PatternContentUnits); try { if (patternUnits == SvgCoordinateUnits.ObjectBoundingBox) { renderer.SetBoundable(renderingElement); } using (var patternMatrix = new Matrix()) { var bounds = renderer.GetBoundable().Bounds; var xScale = (patternUnits == SvgCoordinateUnits.ObjectBoundingBox ? bounds.Width : 1); var yScale = (patternUnits == SvgCoordinateUnits.ObjectBoundingBox ? bounds.Height : 1); float x = xScale * NormalizeUnit(xUnit).ToDeviceValue(renderer, UnitRenderingType.Horizontal, this); float y = yScale * NormalizeUnit(yUnit).ToDeviceValue(renderer, UnitRenderingType.Vertical, this); float width = xScale * NormalizeUnit(widthElem.Width).ToDeviceValue(renderer, UnitRenderingType.Horizontal, this); float height = yScale * NormalizeUnit(heightElem.Height).ToDeviceValue(renderer, UnitRenderingType.Vertical, this); // Apply a scale if needed patternMatrix.Scale((patternContentUnits == SvgCoordinateUnits.ObjectBoundingBox ? bounds.Width : 1) * (viewBox.Width > 0 ? width / viewBox.Width : 1), (patternContentUnits == SvgCoordinateUnits.ObjectBoundingBox ? bounds.Height : 1) * (viewBox.Height > 0 ? height / viewBox.Height : 1), MatrixOrder.Prepend); Bitmap image = new Bitmap((int)width, (int)height); using (var iRenderer = SvgRenderer.FromImage(image)) { iRenderer.SetBoundable((_patternContentUnits == SvgCoordinateUnits.ObjectBoundingBox) ? new GenericBoundable(0, 0, width, height) : renderer.GetBoundable()); iRenderer.Transform = patternMatrix; iRenderer.SmoothingMode = SmoothingMode.AntiAlias; iRenderer.SetClip(new Region(new RectangleF(0, 0, viewBox.Width > 0 ? viewBox.Width : width, viewBox.Height > 0 ? viewBox.Height : height))); foreach (SvgElement child in childElem.Children) { child.RenderElement(iRenderer); } } TextureBrush textureBrush = new TextureBrush(image); var brushTransform = EffectivePatternTransform.Clone(); brushTransform.Translate(x, y, MatrixOrder.Append); textureBrush.Transform = brushTransform; return(textureBrush); } } finally { if (this.PatternUnits == SvgCoordinateUnits.ObjectBoundingBox) { renderer.PopBoundable(); } } }
/// <summary> /// Converts the current unit to one that can be used at render time. /// </summary> /// <param name="boundable">The container element used as the basis for calculations</param> /// <returns>The representation of the current unit in a device value (usually pixels).</returns> public float ToDeviceValue(ISvgRenderer renderer, UnitRenderingType renderType, SvgElement owner) { // If it's already been calculated if (this._deviceValue.HasValue) { return this._deviceValue.Value; } if (this._value == 0.0f) { this._deviceValue = 0.0f; return this._deviceValue.Value; } // http://www.w3.org/TR/CSS21/syndata.html#values // http://www.w3.org/TR/SVG11/coords.html#Units const float cmInInch = 2.54f; int ppi = SvgDocument.PointsPerInch; var type = this.Type; var value = this.Value; float points; switch (type) { case SvgUnitType.Em: using (var currFont = GetFont(renderer, owner)) { if (currFont == null) { points = (float)(value * 9); _deviceValue = (points / 72.0f) * ppi; } else { _deviceValue = value * (currFont.SizeInPoints / 72.0f) * ppi; } } break; case SvgUnitType.Ex: using (var currFont = GetFont(renderer, owner)) { if (currFont == null) { points = (float)(value * 9); _deviceValue = (points * 0.5f / 72.0f) * ppi; } else { _deviceValue = value * 0.5f * (currFont.SizeInPoints / 72.0f) * ppi; } break; } case SvgUnitType.Centimeter: _deviceValue = (float)((value / cmInInch) * ppi); break; case SvgUnitType.Inch: _deviceValue = value * ppi; break; case SvgUnitType.Millimeter: _deviceValue = (float)((value / 10) / cmInInch) * ppi; break; case SvgUnitType.Pica: _deviceValue = ((value * 12) / 72) * ppi; break; case SvgUnitType.Point: _deviceValue = (value / 72) * ppi; break; case SvgUnitType.Pixel: _deviceValue = value; break; case SvgUnitType.User: _deviceValue = value; break; case SvgUnitType.Percentage: // Can't calculate if there is no style owner var boundable = (renderer == null ? (owner == null ? null : owner.OwnerDocument) : renderer.GetBoundable()); if (boundable == null) { _deviceValue = value; break; } System.Drawing.SizeF size = boundable.Bounds.Size; switch (renderType) { case UnitRenderingType.Horizontal: _deviceValue = (size.Width / 100) * value; break; case UnitRenderingType.HorizontalOffset: _deviceValue = (size.Width / 100) * value + boundable.Location.X; break; case UnitRenderingType.Vertical: _deviceValue = (size.Height / 100) * value; break; case UnitRenderingType.VerticalOffset: _deviceValue = (size.Height / 100) * value + boundable.Location.Y; break; default: _deviceValue = (float)(Math.Sqrt(Math.Pow(size.Width, 2) + Math.Pow(size.Height, 2)) / Math.Sqrt(2) * value / 100.0); break; } break; default: _deviceValue = value; break; } return this._deviceValue.Value; }
/// <summary> /// Gets a <see cref="ColorBlend"/> representing the <see cref="SvgGradientServer"/>'s gradient stops. /// </summary> /// <param name="owner">The parent <see cref="SvgVisualElement"/>.</param> /// <param name="opacity">The opacity of the color blend.</param> protected ColorBlend GetColorBlend(ISvgRenderer renderer, float opacity, bool radial) { int colourBlends = this.Stops.Count; bool insertStart = false; bool insertEnd = false; //gradient.Transform = renderingElement.Transforms.Matrix; //stops should be processed in reverse order if it's a radial gradient // May need to increase the number of color blends because the range *must* be from 0.0 to 1.0. // E.g. 0.5 - 0.8 isn't valid therefore the rest need to be calculated. // If the first stop doesn't start at zero if (this.Stops[0].Offset.Value > 0) { colourBlends++; if (radial) { insertEnd = true; } else { insertStart = true; } } // If the last stop doesn't end at 1 a stop float lastValue = this.Stops[this.Stops.Count - 1].Offset.Value; if (lastValue < 100 || lastValue < 1) { colourBlends++; if (radial) { insertStart = true; } else { insertEnd = true; } } ColorBlend blend = new ColorBlend(colourBlends); // Set positions and color values int actualStops = 0; float mergedOpacity = 0.0f; float position = 0.0f; Color color = Colors.Black; for (int i = 0; i < colourBlends; i++) { var currentStop = this.Stops[radial ? this.Stops.Count - 1 - actualStops : actualStops]; var boundWidth = renderer.GetBoundable().Bounds.Width; mergedOpacity = opacity * currentStop.Opacity; position = radial ? 1 - (currentStop.Offset.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this) / boundWidth) : (currentStop.Offset.ToDeviceValue(renderer, UnitRenderingType.Horizontal, this) / boundWidth); color = new Color((int)Math.Round(mergedOpacity * 255), currentStop.GetColor(this)); actualStops++; // Insert this color before itself at position 0 if (insertStart && i == 0) { blend.Positions[i] = 0.0f; blend.Colors[i] = color; i++; } blend.Positions[i] = position; blend.Colors[i] = color; // Insert this color after itself at position 0 if (insertEnd && i == colourBlends - 2) { i++; blend.Positions[i] = 1.0f; blend.Colors[i] = color; } } return(blend); }
public ISvgBoundable GetBoundable() { return(_svgRendererImplementation.GetBoundable()); }