public void TestSetPaint() { SvgPaint paint = new SvgPaint("none"); paint.SetPaint(SvgPaintType.Uri, "someuri", null, null); Assert.AreEqual("someuri", paint.Uri); Assert.AreEqual("url(someuri)", paint.CssText); Assert.AreEqual(SvgPaintType.Uri, paint.PaintType); paint.SetPaint(SvgPaintType.UriCurrentColor, "someuri", null, null); Assert.AreEqual("someuri", paint.Uri); Assert.AreEqual("url(someuri) currentColor", paint.CssText); Assert.AreEqual(SvgPaintType.UriCurrentColor, paint.PaintType); paint.SetPaint(SvgPaintType.UriNone, "someuri", null, null); Assert.AreEqual("someuri", paint.Uri); Assert.AreEqual("url(someuri) none", paint.CssText); Assert.AreEqual(SvgPaintType.UriNone, paint.PaintType); paint.SetPaint(SvgPaintType.None, null, null, null); Assert.AreEqual("none", paint.CssText); Assert.AreEqual(SvgPaintType.None, paint.PaintType); paint.SetPaint(SvgPaintType.CurrentColor, null, null, null); Assert.AreEqual("currentColor", paint.CssText); Assert.AreEqual(SvgPaintType.CurrentColor, paint.PaintType); paint.SetPaint(SvgPaintType.UriRgbColor, "someuri", "#ff00ff", null); Assert.AreEqual("url(someuri) rgb(255,0,255)", paint.CssText); Assert.AreEqual(SvgPaintType.UriRgbColor, paint.PaintType); }
private ICanvasBrush CreatePaint(CanvasDrawingSession session, Rect area, SvgPaint paint, SvgNumber?opacity) { if (paint == null || paint.PaintType == SvgPaintType.RgbColor) { var alpha = (byte)(255.0F * (opacity?.Value ?? 1.0F)); var brush = new CanvasSolidColorBrush( this.ResourceCreator, paint != null ? Color.FromArgb(alpha, paint.RgbColor.Red, paint.RgbColor.Green, paint.RgbColor.Blue) : Color.FromArgb(alpha, 0, 0, 0)); this.DisposableObjects.Add(brush); return(brush); } if (paint.PaintType == SvgPaintType.Uri && paint.Uri[0] == '#') { var key = paint.Uri.Substring(1); var grad = this.TargetDocument.GetElementById(key); if (grad.GetType() == typeof(SvgLinearGradientElement)) { return(this.CreateLinearGradient(session, area, (SvgLinearGradientElement)grad)); } if (grad.GetType() == typeof(SvgRadialGradientElement)) { return(this.CreateRadialGradient(session, area, (SvgRadialGradientElement)grad)); } } throw new NotImplementedException(); }
public void TestPaintTypes() { SvgPaint paint; paint = new SvgPaint("url(rr)"); Assert.AreEqual(SvgPaintType.Uri, paint.PaintType); paint = new SvgPaint("url(rr) #34ff23"); Assert.AreEqual(SvgPaintType.UriRgbColor, paint.PaintType); paint = new SvgPaint("url(rr) #34ff23 icc-color(somename)"); Assert.AreEqual(SvgPaintType.UriRgbColorIccColor, paint.PaintType); paint = new SvgPaint("url(rr) none"); Assert.AreEqual(SvgPaintType.UriNone, paint.PaintType); paint = new SvgPaint("url(rr) currentColor"); Assert.AreEqual(SvgPaintType.UriCurrentColor, paint.PaintType); paint = new SvgPaint("currentColor"); Assert.AreEqual(SvgPaintType.CurrentColor, paint.PaintType); paint = new SvgPaint("none"); Assert.AreEqual(SvgPaintType.None, paint.PaintType); paint = new SvgPaint("rgb(12%, 23%, 12)"); Assert.AreEqual(SvgPaintType.RgbColor, paint.PaintType); paint = new SvgPaint("rgb(12%, 23%, 45) icc-color(somename)"); Assert.AreEqual(SvgPaintType.RgbColorIccColor, paint.PaintType); }
public void TestSetUri() { SvgPaint paint = new SvgPaint("none"); paint.SetUri("uri"); Assert.AreEqual("uri", paint.Uri); Assert.AreEqual(SvgPaintType.Uri, paint.PaintType); }
private Tuple <string, ICssValue> ParseValue(string name, string value, string priority, bool presentation) { var important = priority == "important"; if (!presentation) { name = name.ToLower(); } ICssValue parsedValue = null; switch (name) { case "fill": case "stroke": parsedValue = new SvgPaint(value); break; case "stroke-width": parsedValue = SvgLength.Parse(value, presentation); break; case "stop-color": parsedValue = new SvgColor(value); break; case "fill-opacity": case "stroke-opacity": case "stop-opacity": case "opacity": parsedValue = SvgNumber.Parse(value, 0.0F, 1.0F); break; case "clip-path": parsedValue = new SvgIri(value); break; case "fill-rule": case "clip-rule": parsedValue = new SvgFillRule(presentation ? value : value.ToLower()); break; } if (!this._cache.ContainsKey(name)) { var result = Tuple.Create(value, parsedValue); this._cache.Add(name, result); return(result); } else if (important) { var result = Tuple.Create(value, parsedValue); this._cache[name] = result; return(result); } return(null); }
private Color?GetNodeColor(ISvgElement targetNode, string property) { //StyleColor fillColor = new StyleColor(); //fillColor.FillType = ColorFillType.Solid; string szRGB = null; string szOpacity = null; string szNodeopacity = null; if (targetNode.RenderingHint == SvgRenderingHint.Text || targetNode.RenderingHint == SvgRenderingHint.Shape) { szRGB = (targetNode as SvgElement).GetComputedStyle("").GetPropertyValue(property); szOpacity = (targetNode as SvgElement).GetComputedStyle("").GetPropertyValue(property + @"-opacity"); szNodeopacity = (targetNode as SvgElement).GetComputedStyle("").GetPropertyValue("opacity"); if (string.IsNullOrEmpty(szRGB)) { return(null); } } //Get RGB Color SvgPaint paint = new SvgPaint(szRGB); if (paint.RgbColor == null) { return(null); } Color?solidColor = WpfConvert.ToColor(paint.RgbColor); if (solidColor == null) { return(null); } //Get Aplha Color result = solidColor.Value; if (szNodeopacity != null || szOpacity != null) { double opacityValue = 1; if (szNodeopacity != null && szNodeopacity.Length > 0) { opacityValue *= SvgNumber.ParseNumber(szNodeopacity); } else if (szOpacity != null && szOpacity.Length > 0) { opacityValue *= SvgNumber.ParseNumber(szOpacity); } opacityValue = Math.Min(opacityValue, 1); opacityValue = Math.Max(opacityValue, 0); result.A = Convert.ToByte(opacityValue * 255); } return(result); }
private ICanvasBrush CreatePaint(CanvasDrawingSession session, Rect area, SvgPaint paint, SvgNumber?opacity, CssStyleDeclaration style) { if (paint == null || paint.PaintType == SvgPaintType.RgbColor) { var alpha = (byte)(255.0F * (opacity?.Value ?? 1.0F)); var code = paint == null ? alpha << 24 : alpha << 24 | paint.RgbColor.Red << 16 | paint.RgbColor.Green << 8 | paint.RgbColor.Blue; if (this.SolidResourceCache.ContainsKey(code)) { return(this.SolidResourceCache[code]); } var brush = new CanvasSolidColorBrush(this.ResourceCreator, paint?.ToPlatformColor(alpha) ?? Color.FromArgb(alpha, 0, 0, 0)); this.DisposableObjects.Add(brush); this.SolidResourceCache.Add(code, brush); return(brush); } if (paint.PaintType == SvgPaintType.CurrentColor) { var color = style.Color; var alpha = (byte)(255.0F * (opacity?.Value ?? 1.0F)); var code = alpha << 24 | color.RgbColor.Red << 16 | color.RgbColor.Green << 8 | color.RgbColor.Blue; if (this.SolidResourceCache.ContainsKey(code)) { return(this.SolidResourceCache[code]); } var brush = new CanvasSolidColorBrush(this.ResourceCreator, color.ToPlatformColor(alpha)); this.DisposableObjects.Add(brush); this.SolidResourceCache.Add(code, brush); return(brush); } if (paint.PaintType == SvgPaintType.Uri && paint.Uri[0] == '#') { var key = paint.Uri.Substring(1); var grad = this.TargetDocument.GetElementById(key); if (grad.GetType() == typeof(SvgLinearGradientElement)) { return(this.CreateLinearGradient(session, area, (SvgLinearGradientElement)grad)); } if (grad.GetType() == typeof(SvgRadialGradientElement)) { return(this.CreateRadialGradient(session, area, (SvgRadialGradientElement)grad)); } } throw new NotImplementedException(); }
public static bool TryGetBrush(SvgStyleableElement element, string property, Rect bounds, Matrix transform, out Brush brush) { SvgPaint paint = new SvgPaint(element.GetComputedStyle(string.Empty).GetPropertyValue(property)); SvgPaint svgBrush; if (paint.PaintType == SvgPaintType.None) { brush = null; return(false); } if (paint.PaintType == SvgPaintType.CurrentColor) { svgBrush = new SvgPaint(element.GetComputedStyle(string.Empty).GetPropertyValue(CssConstants.PropColor)); } else { svgBrush = paint; } SvgPaintType paintType = svgBrush.PaintType; if (paintType == SvgPaintType.Uri || paintType == SvgPaintType.UriCurrentColor || paintType == SvgPaintType.UriNone || paintType == SvgPaintType.UriRgbColor || paintType == SvgPaintType.UriRgbColorIccColor) { SvgStyleableElement fillNode = null; string absoluteUri = element.ResolveUri(svgBrush.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) { string propertyValue = styleElm.GetComputedStyle(string.Empty).GetPropertyValue(property); if (!string.IsNullOrWhiteSpace(propertyValue)) { SvgPaint importFill = new SvgPaint(styleElm.GetComputedStyle(string.Empty).GetPropertyValue(property)); if (string.Equals(svgBrush.Uri, importFill.Uri, StringComparison.OrdinalIgnoreCase)) { fillNode = element.ImportDocument.GetNodeByUri(absoluteUri) as SvgStyleableElement; } } } } else { fillNode = element.OwnerDocument.GetNodeByUri(absoluteUri) as SvgStyleableElement; } if (fillNode != null) { SvgLinearGradientElement linearGradient; SvgRadialGradientElement radialGradient; SvgPatternElement pattern; if (TryCast.Cast(fillNode, out linearGradient)) { brush = ConstructBrush(linearGradient, bounds, transform); return(true); } if (TryCast.Cast(fillNode, out radialGradient)) { brush = ConstructBrush(radialGradient, bounds, transform); return(true); } if (TryCast.Cast(fillNode, out pattern)) { brush = ConstructBrush(pattern, bounds, transform); return(true); } } } Color solidColor; if (svgBrush == null || svgBrush.RgbColor == null || !TryConvertColor(svgBrush.RgbColor, out solidColor)) { brush = null; return(false); } brush = new SolidColorBrush(solidColor); brush.Opacity = GetOpacity(element, property); if (brush.CanFreeze) { brush.Freeze(); } return(true); }
private void setNodeFillColor(ISvgElement targetNode, Color fillColor, string property) { string szRGB = null, szOpacity = null, szNodeopacity = null; if (targetNode.RenderingHint == SvgRenderingHint.Text || targetNode.RenderingHint == SvgRenderingHint.Shape) { szRGB = (targetNode as SvgElement).GetComputedStyle("").GetPropertyValue(property); //Special handling: if fill color is white and it is inner node, don't change it. //add id for svgs in Social/Gesture/Style BG. need to check svg if load new svg folder. if (string.IsNullOrEmpty(szRGB) || (szRGB == "rgb(255,255,255)" && !string.IsNullOrEmpty((targetNode as SvgElement).Id))) { return; } //Get RGB Color SvgPaint paint = new SvgPaint(szRGB); if (paint.RgbColor == null) { return; } } //Set RGB Color CssCollectedStyleDeclaration All = (targetNode as SvgElement).GetComputedStyle("") as CssCollectedStyleDeclaration; CssStyleSheetType origin = All.GetPropertyOrigin(property); switch (origin) { //For those default fill color is black,and has no this attribute.Add and set it. case CssStyleSheetType.Unknown: case CssStyleSheetType.NonCssPresentationalHints: { string value = string.Format("rgb({0},{1},{2})", fillColor.R, fillColor.G, fillColor.B); targetNode.SetAttribute(property, value); break; } case CssStyleSheetType.Inline: { string value = string.Format("rgb({0},{1},{2})", fillColor.R, fillColor.G, fillColor.B); SvgStyleableElement styleNode = targetNode as SvgStyleableElement; styleNode.Style.SetProperty(property, value, string.Empty); targetNode.SetAttribute("style", styleNode.Style.CssText); break; } case CssStyleSheetType.Author: { break; } } //Set Alpha value szOpacity = (targetNode as SvgElement).GetComputedStyle("").GetPropertyValue(property + @"-opacity"); szNodeopacity = (targetNode as SvgElement).GetComputedStyle("").GetPropertyValue("opacity"); if (szNodeopacity == null && szOpacity == null) { if (fillColor.A == 255) { return; } string value = string.Format("{0}", Convert.ToDouble((double)fillColor.A / (double)255)); targetNode.SetAttribute(property + @"-opacity", value); } else { string Oproperty = null; if (szNodeopacity != null && szNodeopacity.Length > 0) { Oproperty = "opacity"; } else { Oproperty = property + @"-opacity"; } origin = All.GetPropertyOrigin(Oproperty); switch (origin) { case CssStyleSheetType.NonCssPresentationalHints: { string value = string.Format("{0}", Convert.ToDouble((double)fillColor.A / (double)255)); targetNode.SetAttribute(Oproperty, value); break; } case CssStyleSheetType.Unknown: { string value = string.Format("{0}", Convert.ToDouble((double)fillColor.A / (double)255)); targetNode.SetAttribute(Oproperty, value); break; } case CssStyleSheetType.Inline: { string value = string.Format("{0}", Convert.ToDouble((double)fillColor.A / (double)255)); SvgStyleableElement styleNode = targetNode as SvgStyleableElement; styleNode.Style.SetProperty(Oproperty, value, string.Empty); targetNode.SetAttribute("style", styleNode.Style.CssText); break; } case CssStyleSheetType.Author: { break; } } } }
private Brush GetBrush(GraphicsPath gp, string propPrefix) { SvgPaintType curPaintType = this.PaintType; if (curPaintType == SvgPaintType.None) { return(null); } SvgPaint painter = this; if (curPaintType == SvgPaintType.CurrentColor) { painter = new GdiSvgPaint(_element, CssConstants.PropColor); } SvgPaintType paintType = painter.PaintType; if (paintType == SvgPaintType.Uri || paintType == SvgPaintType.UriCurrentColor || paintType == SvgPaintType.UriNone || paintType == SvgPaintType.UriRgbColor || paintType == SvgPaintType.UriRgbColorIccColor) { _paintFill = GetPaintFill(painter.Uri); if (_paintFill != null) { Brush br = _paintFill.GetBrush(gp.GetBounds(), this.GetOpacityValue(propPrefix)); if (_paintFill.FillType == GdiFillType.Pattern) { return(br); } if (_paintFill.FillType == GdiFillType.LinearGradient) { LinearGradientBrush lgb = br as LinearGradientBrush; if (lgb != null) { int opacityl = GetOpacity(propPrefix); for (int i = 0; i < lgb.InterpolationColors.Colors.Length; i++) { lgb.InterpolationColors.Colors[i] = Color.FromArgb(opacityl, lgb.InterpolationColors.Colors[i]); } for (int i = 0; i < lgb.LinearColors.Length; i++) { lgb.LinearColors[i] = Color.FromArgb(opacityl, lgb.LinearColors[i]); } return(br); } } if (_paintFill.FillType == GdiFillType.RadialGradient) { PathGradientBrush pgb = br as PathGradientBrush; if (pgb != null) { int opacityl = GetOpacity(propPrefix); for (int i = 0; i < pgb.InterpolationColors.Colors.Length; i++) { pgb.InterpolationColors.Colors[i] = Color.FromArgb(opacityl, pgb.InterpolationColors.Colors[i]); } for (int i = 0; i < pgb.SurroundColors.Length; i++) { pgb.SurroundColors[i] = Color.FromArgb(opacityl, pgb.SurroundColors[i]); } return(br); } } } else { if (curPaintType == SvgPaintType.UriNone || curPaintType == SvgPaintType.Uri) { return(null); } else if (curPaintType == SvgPaintType.UriCurrentColor) { painter = new GdiSvgPaint(_element, CssConstants.PropColor); } else { painter = this; } } } if (painter == null || painter.RgbColor == null) { return(null); } var brush = new SolidBrush(GdiConverter.ToColor(painter.RgbColor)); int opacity = GetOpacity(propPrefix); brush.Color = Color.FromArgb(opacity, brush.Color); return(brush); }