public static bool TryGetSpreadMethod(SvgSpreadMethod sm, out GradientSpreadMethod spreadMethod) { if (sm == SvgSpreadMethod.None) { spreadMethod = GradientSpreadMethod.Pad; return(false); } spreadMethod = (GradientSpreadMethod)sm; return(true); }
public static GradientSpreadMethod ToSpreadMethod(SvgSpreadMethod sm) { switch (sm) { case SvgSpreadMethod.Pad: return GradientSpreadMethod.Pad; case SvgSpreadMethod.Reflect: return GradientSpreadMethod.Reflect; case SvgSpreadMethod.Repeat: return GradientSpreadMethod.Repeat; } return GradientSpreadMethod.Pad; }
public static GradientSpreadMethod ToSpreadMethod(SvgSpreadMethod sm) { switch (sm) { case SvgSpreadMethod.Pad: return(GradientSpreadMethod.Pad); case SvgSpreadMethod.Reflect: return(GradientSpreadMethod.Reflect); case SvgSpreadMethod.Repeat: return(GradientSpreadMethod.Repeat); } return(GradientSpreadMethod.Pad); }
private Brush GetLinearGradientBrush(SvgLinearGradientElement res, Transform viewBoxTransform = null) { double x1 = res.X1.AnimVal.Value; double x2 = res.X2.AnimVal.Value; double y1 = res.Y1.AnimVal.Value; double y2 = res.Y2.AnimVal.Value; GradientStopCollection gradientStops = GetGradientStops(res.Stops); if (gradientStops == null || gradientStops.Count == 0) { return(null); } LinearGradientBrush brush = new LinearGradientBrush(gradientStops, new Point(x1, y1), new Point(x2, y2)); SvgSpreadMethod spreadMethod = SvgSpreadMethod.Pad; if (res.SpreadMethod != null) { spreadMethod = (SvgSpreadMethod)res.SpreadMethod.AnimVal; if (spreadMethod != SvgSpreadMethod.None) { brush.SpreadMethod = WpfConvert.ToSpreadMethod(spreadMethod); } } SvgUnitType mappingMode = SvgUnitType.ObjectBoundingBox; if (res.GradientUnits != null) { mappingMode = (SvgUnitType)res.GradientUnits.AnimVal; if (mappingMode == SvgUnitType.ObjectBoundingBox) { brush.MappingMode = BrushMappingMode.RelativeToBoundingBox; } else if (mappingMode == SvgUnitType.UserSpaceOnUse) { brush.MappingMode = BrushMappingMode.Absolute; //if (viewBoxTransform == null || viewBoxTransform.Value.IsIdentity) //{ // if (!elementBounds.IsEmpty) // { // viewBoxTransform = FitToViewbox(new SvgRect(x1, y1, // Math.Abs(x2 - x1), Math.Abs(y2 - y1)), elementBounds); // } //} } } string colorInterpolation = res.GetPropertyValue("color-interpolation"); if (!string.IsNullOrWhiteSpace(colorInterpolation)) { if (colorInterpolation == "linearRGB") { brush.ColorInterpolationMode = ColorInterpolationMode.ScRgbLinearInterpolation; } else { brush.ColorInterpolationMode = ColorInterpolationMode.SRgbLinearInterpolation; } } MatrixTransform transform = GetTransformMatrix(res); if (transform != null && !transform.Matrix.IsIdentity) { if (viewBoxTransform != null && !viewBoxTransform.Value.IsIdentity) { TransformGroup group = new TransformGroup(); group.Children.Add(viewBoxTransform); group.Children.Add(transform); brush.Transform = group; } else { brush.Transform = transform; //brush.StartPoint = new Point(0, 0.5); //brush.EndPoint = new Point(1, 0.5); } //brush.StartPoint = new Point(0, 0); //brush.EndPoint = new Point(1, 1); } else { float fLeft = (float)res.X1.AnimVal.Value; float fRight = (float)res.X2.AnimVal.Value; float fTop = (float)res.Y1.AnimVal.Value; float fBottom = (float)res.Y2.AnimVal.Value; if (mappingMode == SvgUnitType.ObjectBoundingBox) { if (!fTop.Equals(fBottom) && !fLeft.Equals(fRight)) { var drawingBrush = new DrawingBrush(); drawingBrush.Stretch = Stretch.Fill; drawingBrush.Viewbox = new Rect(0, 0, 1, 1); var DrawingRect = new GeometryDrawing(brush, null, new RectangleGeometry(new Rect(0, 0, 1, 1))); drawingBrush.Drawing = DrawingRect; return(drawingBrush); } } if (fTop.Equals(fBottom)) { //mode = LinearGradientMode.Horizontal; //brush.StartPoint = new Point(0, 0.5); //brush.EndPoint = new Point(1, 0.5); } else { if (fLeft.Equals(fRight)) { //mode = LinearGradientMode.Vertical; //brush.StartPoint = new Point(0.5, 0); //brush.EndPoint = new Point(0.5, 1); } else { if (fLeft < fRight) { if (viewBoxTransform != null && !viewBoxTransform.Value.IsIdentity) { //TransformGroup group = new TransformGroup(); //group.Children.Add(viewBoxTransform); //group.Children.Add(new RotateTransform(45, 0.5, 0.5)); //brush.Transform = group; brush.Transform = viewBoxTransform; } //else //{ // brush.RelativeTransform = new RotateTransform(45, 0.5, 0.5); //} //mode = LinearGradientMode.ForwardDiagonal; //brush.EndPoint = new Point(x1, y1 + 1); //brush.StartPoint = new Point(0, 0); //brush.EndPoint = new Point(1, 1); } else { //mode = LinearGradientMode.BackwardDiagonal; if (viewBoxTransform != null && !viewBoxTransform.Value.IsIdentity) { //TransformGroup group = new TransformGroup(); //group.Children.Add(viewBoxTransform); //group.Children.Add(new RotateTransform(-45, 0.5, 0.5)); //brush.Transform = group; brush.Transform = viewBoxTransform; } //else //{ // brush.RelativeTransform = new RotateTransform(-45, 0.5, 0.5); //} //brush.StartPoint = new Point(0, 0); //brush.EndPoint = new Point(1, 1); } } } } return(brush); }
private Brush GetRadialGradientBrush(SvgRadialGradientElement res) { double centerX = res.Cx.AnimVal.Value; double centerY = res.Cy.AnimVal.Value; double focusX = res.Fx.AnimVal.Value; double focusY = res.Fy.AnimVal.Value; double radius = res.R.AnimVal.Value; GradientStopCollection gradientStops = GetGradientStops(res.Stops); if (gradientStops == null || gradientStops.Count == 0) { return(null); } RadialGradientBrush brush = new RadialGradientBrush(gradientStops); brush.RadiusX = radius; brush.RadiusY = radius; brush.Center = new Point(centerX, centerY); brush.GradientOrigin = new Point(focusX, focusY); if (res.SpreadMethod != null) { SvgSpreadMethod spreadMethod = (SvgSpreadMethod)res.SpreadMethod.AnimVal; if (spreadMethod != SvgSpreadMethod.None) { brush.SpreadMethod = WpfConvert.ToSpreadMethod(spreadMethod); } } if (res.GradientUnits != null) { SvgUnitType mappingMode = (SvgUnitType)res.GradientUnits.AnimVal; if (mappingMode == SvgUnitType.ObjectBoundingBox) { brush.MappingMode = BrushMappingMode.RelativeToBoundingBox; } else if (mappingMode == SvgUnitType.UserSpaceOnUse) { brush.MappingMode = BrushMappingMode.Absolute; if (res.Fx.AnimVal.UnitType == SvgLengthType.Percentage) { brush.GradientOrigin = brush.Center; } } } MatrixTransform transform = GetTransformMatrix(res); if (transform != null && !transform.Matrix.IsIdentity) { brush.Transform = transform; } string colorInterpolation = res.GetPropertyValue("color-interpolation"); if (!string.IsNullOrWhiteSpace(colorInterpolation)) { if (colorInterpolation == "linearRGB") { brush.ColorInterpolationMode = ColorInterpolationMode.SRgbLinearInterpolation; } else { brush.ColorInterpolationMode = ColorInterpolationMode.ScRgbLinearInterpolation; } } return(brush); }
public static RadialGradientBrush ConstructBrush(SvgRadialGradientElement gradient, Rect bounds, Matrix transform) { if (gradient.Stops.Count == 0) { return(null); } double centerX = gradient.Cx.AnimVal.Value; double centerY = gradient.Cy.AnimVal.Value; double focusX = gradient.Fx.AnimVal.Value; double focusY = gradient.Fy.AnimVal.Value; double radius = gradient.R.AnimVal.Value; GradientStopCollection gradientStops = ToGradientStops(gradient.Stops); RadialGradientBrush brush = new RadialGradientBrush(gradientStops); brush.RadiusX = radius; brush.RadiusY = radius; brush.Center = new Point(centerX, centerY); brush.GradientOrigin = new Point(focusX, focusY); if (gradient.SpreadMethod != null) { SvgSpreadMethod spreadMethod = (SvgSpreadMethod)gradient.SpreadMethod.AnimVal; GradientSpreadMethod sm; if (TryGetSpreadMethod(spreadMethod, out sm)) { brush.SpreadMethod = sm; } } SvgUnitType mappingMode = SvgUnitType.ObjectBoundingBox; if (gradient.GradientUnits != null) { mappingMode = (SvgUnitType)gradient.GradientUnits.AnimVal; if (mappingMode == SvgUnitType.ObjectBoundingBox) { brush.MappingMode = BrushMappingMode.RelativeToBoundingBox; } else if (mappingMode == SvgUnitType.UserSpaceOnUse) { brush.MappingMode = BrushMappingMode.Absolute; brush.Center.Offset(bounds.X, bounds.Y); brush.GradientOrigin.Offset(bounds.X, bounds.Y); } } Matrix brushTransform = ToWpfMatrix(((SvgTransformList)gradient.GradientTransform.AnimVal).TotalMatrix); if (mappingMode == SvgUnitType.UserSpaceOnUse) { brushTransform *= transform; } if (!brushTransform.IsIdentity) { brush.Transform = new MatrixTransform(brushTransform); } string colorInterpolation = gradient.GetPropertyValue("color-interpolation"); if (!string.IsNullOrWhiteSpace(colorInterpolation)) { if (colorInterpolation == CssConstants.ValLinearRgb) { brush.ColorInterpolationMode = ColorInterpolationMode.SRgbLinearInterpolation; } else { brush.ColorInterpolationMode = ColorInterpolationMode.ScRgbLinearInterpolation; } } return(brush); }
public static LinearGradientBrush ConstructBrush(SvgLinearGradientElement gradient, Rect bounds, Matrix transform) { if (gradient.Stops.Count == 0) { return(null); } double x1 = gradient.X1.AnimVal.Value; double x2 = gradient.X2.AnimVal.Value; double y1 = gradient.Y1.AnimVal.Value; double y2 = gradient.Y2.AnimVal.Value; GradientStopCollection gradientStops = ToGradientStops(gradient.Stops); LinearGradientBrush brush = new LinearGradientBrush(gradientStops, new Point(x1, y1), new Point(x2, y2)); SvgSpreadMethod spreadMethod = SvgSpreadMethod.Pad; if (gradient.SpreadMethod != null) { spreadMethod = (SvgSpreadMethod)gradient.SpreadMethod.AnimVal; GradientSpreadMethod sm; if (TryGetSpreadMethod(spreadMethod, out sm)) { brush.SpreadMethod = sm; } } SvgUnitType mappingMode = SvgUnitType.ObjectBoundingBox; if (gradient.GradientUnits != null) { mappingMode = (SvgUnitType)gradient.GradientUnits.AnimVal; if (mappingMode == SvgUnitType.ObjectBoundingBox) { brush.MappingMode = BrushMappingMode.RelativeToBoundingBox; } else if (mappingMode == SvgUnitType.UserSpaceOnUse) { brush.MappingMode = BrushMappingMode.Absolute; brush.StartPoint.Offset(bounds.X, bounds.Y); brush.EndPoint.Offset(bounds.X, bounds.Y); } } Matrix brushTransform = ToWpfMatrix(((SvgTransformList)gradient.GradientTransform.AnimVal).TotalMatrix); if (mappingMode == SvgUnitType.UserSpaceOnUse) { brushTransform *= transform; } if (!brushTransform.IsIdentity) { brush.Transform = new MatrixTransform(brushTransform); } string colorInterpolation = gradient.GetPropertyValue("color-interpolation"); if (!string.IsNullOrWhiteSpace(colorInterpolation)) { if (colorInterpolation == CssConstants.ValLinearRgb) { brush.ColorInterpolationMode = ColorInterpolationMode.ScRgbLinearInterpolation; } else { brush.ColorInterpolationMode = ColorInterpolationMode.SRgbLinearInterpolation; } } return(brush); }
private LinearGradientBrush GetLinearGradientBrush(Rect elementBounds, SvgLinearGradientElement res) { double x1 = res.X1.AnimVal.Value; double x2 = res.X2.AnimVal.Value; double y1 = res.Y1.AnimVal.Value; double y2 = res.Y2.AnimVal.Value; GradientStopCollection gradientStops = GetGradientStops(res.Stops); //LinearGradientBrush brush = new LinearGradientBrush(gradientStops); LinearGradientBrush brush = new LinearGradientBrush(gradientStops, new Point(x1, y1), new Point(x2, y2)); SvgSpreadMethod spreadMethod = SvgSpreadMethod.Pad; if (res.SpreadMethod != null) { spreadMethod = (SvgSpreadMethod)res.SpreadMethod.AnimVal; if (spreadMethod != SvgSpreadMethod.None) { brush.SpreadMethod = WpfConvert.ToSpreadMethod(spreadMethod); } } Transform viewBoxTransform = null; SvgUnitType mappingMode = SvgUnitType.ObjectBoundingBox; if (res.GradientUnits != null) { mappingMode = (SvgUnitType)res.GradientUnits.AnimVal; if (mappingMode == SvgUnitType.ObjectBoundingBox) { brush.MappingMode = BrushMappingMode.RelativeToBoundingBox; } else if (mappingMode == SvgUnitType.UserSpaceOnUse) { brush.MappingMode = BrushMappingMode.Absolute; viewBoxTransform = FitToViewbox(new SvgRect(x1, y1, Math.Abs(x2 - x1), Math.Abs(y2 - y1)), elementBounds); } } MatrixTransform transform = GetTransformMatrix(res); if (transform != null && !transform.Matrix.IsIdentity) { if (viewBoxTransform != null) { TransformGroup group = new TransformGroup(); group.Children.Add(viewBoxTransform); group.Children.Add(transform); brush.Transform = group; } else { brush.Transform = transform; //brush.StartPoint = new Point(0, 0.5); //brush.EndPoint = new Point(1, 0.5); } //brush.StartPoint = new Point(0, 0); //brush.EndPoint = new Point(1, 1); } else { float fLeft = (float)res.X1.AnimVal.Value; float fRight = (float)res.X2.AnimVal.Value; float fTop = (float)res.Y1.AnimVal.Value; float fBottom = (float)res.Y2.AnimVal.Value; if (fTop == fBottom) { //mode = LinearGradientMode.Horizontal; //brush.StartPoint = new Point(0, 0.5); //brush.EndPoint = new Point(1, 0.5); } else { if (fLeft == fRight) { //var mode = LinearGradientMode.Vertical; //brush.StartPoint = new Point(0.5, 0); // brush.EndPoint = new Point(0.5, 1); } else { if (fLeft < fRight) { if (viewBoxTransform != null) { TransformGroup group = new TransformGroup(); group.Children.Add(viewBoxTransform); group.Children.Add(new RotateTransform(45, 0.5, 0.5)); brush.Transform = group; } else { //brush.RelativeTransform = new RotateTransform(45, 0.5, 0.5); } //mode = LinearGradientMode.ForwardDiagonal; //brush.EndPoint = new Point(x1, y1 + 1); //brush.StartPoint = new Point(0, 0); //brush.EndPoint = new Point(1, 1); } else { //mode = LinearGradientMode.BackwardDiagonal; if (viewBoxTransform != null) { TransformGroup group = new TransformGroup(); group.Children.Add(viewBoxTransform); group.Children.Add(new RotateTransform(-45, 0.5, 0.5)); brush.Transform = group; } else { brush.RelativeTransform = new RotateTransform(-45, 0.5, 0.5); } //brush.StartPoint = new Point(0, 0); //brush.EndPoint = new Point(1, 1); } } } } string colorInterpolation = res.GetPropertyValue("color-interpolation"); if (!String.IsNullOrEmpty(colorInterpolation)) { if (colorInterpolation == "linearRGB") { brush.ColorInterpolationMode = ColorInterpolationMode.ScRgbLinearInterpolation; } else { brush.ColorInterpolationMode = ColorInterpolationMode.SRgbLinearInterpolation; } } return(brush); }
//========================================================================== public SvgGradientBaseElement(SvgDocument document, SvgBaseElement parent, XElement gradientElement) : base(document, parent, gradientElement) { XAttribute gradient_units_attribute = gradientElement.Attribute("gradientUnits"); if (gradient_units_attribute != null) { switch (gradient_units_attribute.Value) { case "objectBoundingBox": GradientUnits = SvgGradientUnits.ObjectBoundingBox; break; case "userSpaceOnUse": GradientUnits = SvgGradientUnits.UserSpaceOnUse; break; default: throw new NotImplementedException(String.Format("gradientUnits value '{0}' is no supported", gradient_units_attribute.Value)); } } XAttribute gradient_transform_attribute = gradientElement.Attribute("gradientTransform"); if (gradient_transform_attribute != null) { Transform = SvgTransform.Parse(gradient_transform_attribute.Value); } XAttribute spread_method_attribute = gradientElement.Attribute("spreadMethod"); if (spread_method_attribute != null) { switch (spread_method_attribute.Value) { case "pad": SpreadMethod = SvgSpreadMethod.Pad; break; case "reflect": SpreadMethod = SvgSpreadMethod.Reflect; break; case "repeat": SpreadMethod = SvgSpreadMethod.Repeat; break; } } foreach (XElement element in from element in gradientElement.Elements() where element.Name.NamespaceName == "http://www.w3.org/2000/svg" select element) { switch (element.Name.LocalName) { case "stop": Stops.Add(new SvgStopElement(Document, this, element)); break; default: throw new NotImplementedException(String.Format("Unhandled element: {0}", element)); } } }
private Brush GetLinearGradientBrush(SvgLinearGradientElement res, Transform viewBoxTransform = null) { GradientStopCollection gradientStops = GetGradientStops(res.Stops); if (gradientStops == null || gradientStops.Count == 0) { return(null); } double x1 = res.X1.AnimVal.Value; double x2 = res.X2.AnimVal.Value; double y1 = res.Y1.AnimVal.Value; double y2 = res.Y2.AnimVal.Value; LinearGradientBrush brush = new LinearGradientBrush(gradientStops, new Point(x1, y1), new Point(x2, y2)); SvgSpreadMethod spreadMethod = SvgSpreadMethod.Pad; if (res.SpreadMethod != null) { spreadMethod = (SvgSpreadMethod)res.SpreadMethod.AnimVal; if (spreadMethod != SvgSpreadMethod.None) { brush.SpreadMethod = WpfConvert.ToSpreadMethod(spreadMethod); } } SvgUnitType mappingMode = SvgUnitType.ObjectBoundingBox; if (res.GradientUnits != null) { mappingMode = (SvgUnitType)res.GradientUnits.AnimVal; if (mappingMode == SvgUnitType.ObjectBoundingBox) { brush.MappingMode = BrushMappingMode.RelativeToBoundingBox; } else if (mappingMode == SvgUnitType.UserSpaceOnUse) { brush.MappingMode = BrushMappingMode.Absolute; _isUserSpace = true; } } string colorInterpolation = res.GetPropertyValue("color-interpolation"); if (!string.IsNullOrWhiteSpace(colorInterpolation)) { if (string.Equals(colorInterpolation, "linearRGB", StringComparison.OrdinalIgnoreCase)) { brush.ColorInterpolationMode = ColorInterpolationMode.ScRgbLinearInterpolation; } else { brush.ColorInterpolationMode = ColorInterpolationMode.SRgbLinearInterpolation; } } MatrixTransform transform = GetTransformMatrix(res); if (transform != null && !transform.Matrix.IsIdentity) { if (viewBoxTransform != null && !viewBoxTransform.Value.IsIdentity) { TransformGroup group = new TransformGroup(); group.Children.Add(viewBoxTransform); group.Children.Add(transform); brush.Transform = group; } else { brush.Transform = transform; } } else { float fLeft = (float)res.X1.AnimVal.Value; float fRight = (float)res.X2.AnimVal.Value; float fTop = (float)res.Y1.AnimVal.Value; float fBottom = (float)res.Y2.AnimVal.Value; if (mappingMode == SvgUnitType.ObjectBoundingBox) { if (!fTop.Equals(fBottom) && !fLeft.Equals(fRight)) { var drawingBrush = new DrawingBrush(); drawingBrush.Stretch = Stretch.Fill; drawingBrush.Viewbox = new Rect(0, 0, 1, 1); var DrawingRect = new GeometryDrawing(brush, null, new RectangleGeometry(new Rect(0, 0, 1, 1))); drawingBrush.Drawing = DrawingRect; return(drawingBrush); } } if (fTop.Equals(fBottom)) { } else { if (fLeft.Equals(fRight)) { } else { if (fLeft < fRight) { if (viewBoxTransform != null && !viewBoxTransform.Value.IsIdentity) { brush.Transform = viewBoxTransform; } } else { if (viewBoxTransform != null && !viewBoxTransform.Value.IsIdentity) { brush.Transform = viewBoxTransform; } } } } } return(brush); }
private Brush GetRadialGradientBrush(SvgRadialGradientElement res) { var refElem = res.ReferencedElement; double centerX = res.Cx.AnimVal.Value; double centerY = res.Cy.AnimVal.Value; // 'fx', 'fy', and 'fr' define the start circle for the radial gradient. double focusX = res.Fx.AnimVal.Value; double focusY = res.Fy.AnimVal.Value; double radius = res.R.AnimVal.Value; var lengthUnit = res.Cx.AnimVal.UnitType; // If attribute 'fx' is not specified, 'fx' will coincide with the presentational // value of 'cx' for the element whether the value for 'cx' was inherited or not. if (lengthUnit == SvgLengthType.Percentage) { if (!res.HasAttribute("fx") && (refElem == null || !refElem.HasAttribute("fx"))) { focusX = centerX; } else if (focusX.Equals(0.0)) { focusX = centerX; if (focusX > 0 && focusX >= radius) { focusX = (centerX > radius) ? centerX - radius : focusX = radius; } } else { if (focusX > 0 && focusX >= radius) { focusX = (centerX > radius) ? centerX - radius : focusX = radius; } } } lengthUnit = res.Cy.AnimVal.UnitType; // If attribute 'fy' is not specified, 'fy' will coincide with the presentational // value of 'cy' for the element whether the value for 'cy' was inherited or not. if (lengthUnit == SvgLengthType.Percentage) { if (!res.HasAttribute("fy") && (refElem == null || !refElem.HasAttribute("fy"))) { focusY = centerY; } else if (focusY.Equals(0.0)) { focusY = centerY; if (focusY > 0 && focusY >= radius) { focusY = (centerY > radius) ? centerY - radius : focusY = radius; } } else { if (focusY > 0 && focusY >= radius) { focusY = (centerY > radius) ? centerY - radius : focusY = radius; } } } GradientStopCollection gradientStops = GetGradientStops(res.Stops); if (gradientStops == null || gradientStops.Count == 0) { return(null); } RadialGradientBrush brush = new RadialGradientBrush(gradientStops); brush.RadiusX = radius; brush.RadiusY = radius; brush.Center = new Point(centerX, centerY); brush.GradientOrigin = new Point(focusX, focusY); if (res.SpreadMethod != null) { SvgSpreadMethod spreadMethod = (SvgSpreadMethod)res.SpreadMethod.AnimVal; if (spreadMethod != SvgSpreadMethod.None) { brush.SpreadMethod = WpfConvert.ToSpreadMethod(spreadMethod); } } if (res.GradientUnits != null) { SvgUnitType mappingMode = (SvgUnitType)res.GradientUnits.AnimVal; if (mappingMode == SvgUnitType.ObjectBoundingBox) { brush.MappingMode = BrushMappingMode.RelativeToBoundingBox; } else if (mappingMode == SvgUnitType.UserSpaceOnUse) { brush.MappingMode = BrushMappingMode.Absolute; if (res.Fx.AnimVal.UnitType == SvgLengthType.Percentage) { brush.GradientOrigin = brush.Center; } _isUserSpace = true; } } MatrixTransform transform = GetTransformMatrix(res); if (transform != null && !transform.Matrix.IsIdentity) { brush.Transform = transform; } string colorInterpolation = res.GetPropertyValue("color-interpolation"); if (!string.IsNullOrWhiteSpace(colorInterpolation)) { if (string.Equals(colorInterpolation, "linearRGB", StringComparison.OrdinalIgnoreCase)) { brush.ColorInterpolationMode = ColorInterpolationMode.SRgbLinearInterpolation; } else { brush.ColorInterpolationMode = ColorInterpolationMode.ScRgbLinearInterpolation; } } return(brush); }
//========================================================================== public SvgGradientBaseElement(SvgDocument document, SvgBaseElement parent, XElement gradientElement) : base(document, parent, gradientElement) { XAttribute gradient_units_attribute = gradientElement.Attribute("gradientUnits"); if(gradient_units_attribute != null) switch(gradient_units_attribute.Value) { case "objectBoundingBox": GradientUnits = SvgGradientUnits.ObjectBoundingBox; break; case "userSpaceOnUse": GradientUnits = SvgGradientUnits.UserSpaceOnUse; break; default: throw new NotImplementedException(String.Format("gradientUnits value '{0}' is no supported", gradient_units_attribute.Value)); } XAttribute gradient_transform_attribute = gradientElement.Attribute("gradientTransform"); if(gradient_transform_attribute != null) Transform = SvgTransform.Parse(gradient_transform_attribute.Value); XAttribute spread_method_attribute = gradientElement.Attribute("spreadMethod"); if(spread_method_attribute != null) switch(spread_method_attribute.Value) { case "pad": SpreadMethod = SvgSpreadMethod.Pad; break; case "reflect": SpreadMethod = SvgSpreadMethod.Reflect; break; case "repeat": SpreadMethod = SvgSpreadMethod.Repeat; break; } foreach(XElement element in from element in gradientElement.Elements() where element.Name.NamespaceName == "http://www.w3.org/2000/svg" select element) switch(element.Name.LocalName) { case "stop": Stops.Add(new SvgStopElement(Document, this, element)); break; default: throw new NotImplementedException(String.Format("Unhandled element: {0}", element)); } }
private LinearGradientBrush GetLinearGradientBrush(SvgLinearGradientElement res) { double x1 = res.X1.AnimVal.Value; double x2 = res.X2.AnimVal.Value; double y1 = res.Y1.AnimVal.Value; double y2 = res.Y2.AnimVal.Value; GradientStopCollection gradientStops = GetGradientStops(res.Stops); LinearGradientBrush brush = new LinearGradientBrush(gradientStops, new Point(x1, y1), new Point(x2, y2)); SvgSpreadMethod spreadMethod = SvgSpreadMethod.None; if (res.SpreadMethod != null) { spreadMethod = (SvgSpreadMethod)res.SpreadMethod.AnimVal; if (spreadMethod != SvgSpreadMethod.None) { brush.SpreadMethod = WpfConvert.ToSpreadMethod(spreadMethod); } } if (res.GradientUnits != null) { SvgUnitType mappingMode = (SvgUnitType)res.GradientUnits.AnimVal; if (mappingMode == SvgUnitType.ObjectBoundingBox) { brush.MappingMode = BrushMappingMode.RelativeToBoundingBox; } else if (mappingMode == SvgUnitType.UserSpaceOnUse) { brush.MappingMode = BrushMappingMode.Absolute; } } MatrixTransform transform = GetTransformMatrix(res); if (transform != null && !transform.Matrix.IsIdentity) { brush.Transform = transform; } //else //{ // float fLeft = (float)res.X1.AnimVal.Value; // float fRight = (float)res.X2.AnimVal.Value; // float fTop = (float)res.Y1.AnimVal.Value; // float fBottom = (float)res.Y2.AnimVal.Value; // if (fTop == fBottom) // { // //mode = LinearGradientMode.Horizontal; // } // else // { // if (fLeft == fRight) // { // //mode = LinearGradientMode.Vertical; // } // else // { // if (fLeft < fRight) // { // //mode = LinearGradientMode.ForwardDiagonal; // brush.Transform = new RotateTransform(45, 0, 0); // //brush.EndPoint = new Point(x1, y1 + 1); // } // else // { // //mode = LinearGradientMode.BackwardDiagonal; // brush.Transform = new RotateTransform(-45); // } // } // } //} string colorInterpolation = res.GetPropertyValue("color-interpolation"); if (!String.IsNullOrEmpty(colorInterpolation)) { if (colorInterpolation == "linearRGB") { brush.ColorInterpolationMode = ColorInterpolationMode.ScRgbLinearInterpolation; } else { brush.ColorInterpolationMode = ColorInterpolationMode.SRgbLinearInterpolation; } } return(brush); }