protected void Clip(GdiGraphicsWrapper gr) { if (element == null) { return; } SvgRenderingHint hint = element.RenderingHint; // todo: should we correct the clipping to adjust to the off-one-pixel drawing? gr.TranslateClip(1, 1); #region Clip with clip // see http://www.w3.org/TR/SVG/masking.html#OverflowAndClipProperties if (element is ISvgSvgElement || element is ISvgMarkerElement || element is ISvgSymbolElement || element is ISvgPatternElement) { // check overflow property CssValue overflow = ((SvgElement)element).GetComputedCssValue("overflow", String.Empty) as CssValue; // TODO: clip can have "rect(10 10 auto 10)" CssPrimitiveValue clip = ((SvgElement)element).GetComputedCssValue("clip", String.Empty) as CssPrimitiveValue; string sOverflow = null; if (overflow != null || overflow.CssText == "") { sOverflow = overflow.CssText; } else { if (this is ISvgSvgElement) sOverflow = "hidden"; } if (sOverflow != null) { // "If the 'overflow' property has a value other than hidden or scroll, the property has no effect (i.e., a clipping rectangle is not created)." if (sOverflow == "hidden" || sOverflow == "scroll") { RectangleF clipRect = RectangleF.Empty; if (clip != null && clip.PrimitiveType == CssPrimitiveType.Rect) { if (element is ISvgSvgElement) { ISvgSvgElement svgElement = (ISvgSvgElement)element; SvgRect viewPort = svgElement.Viewport as SvgRect; clipRect = GdiConverter.ToRectangle(viewPort); ICssRect clipShape = (CssRect)clip.GetRectValue(); if (clipShape.Top.PrimitiveType != CssPrimitiveType.Ident) clipRect.Y += (float)clipShape.Top.GetFloatValue(CssPrimitiveType.Number); if (clipShape.Left.PrimitiveType != CssPrimitiveType.Ident) clipRect.X += (float)clipShape.Left.GetFloatValue(CssPrimitiveType.Number); if (clipShape.Right.PrimitiveType != CssPrimitiveType.Ident) clipRect.Width = (clipRect.Right - clipRect.X) - (float)clipShape.Right.GetFloatValue(CssPrimitiveType.Number); if (clipShape.Bottom.PrimitiveType != CssPrimitiveType.Ident) clipRect.Height = (clipRect.Bottom - clipRect.Y) - (float)clipShape.Bottom.GetFloatValue(CssPrimitiveType.Number); } } else if (clip == null || (clip.PrimitiveType == CssPrimitiveType.Ident && clip.GetStringValue() == "auto")) { if (element is ISvgSvgElement) { ISvgSvgElement svgElement = (ISvgSvgElement)element; SvgRect viewPort = svgElement.Viewport as SvgRect; clipRect = GdiConverter.ToRectangle(viewPort); } else if (element is ISvgMarkerElement || element is ISvgSymbolElement || element is ISvgPatternElement) { // TODO: what to do here? } } if (clipRect != RectangleF.Empty) { gr.SetClip(clipRect); } } } } #endregion #region Clip with clip-path // see: http://www.w3.org/TR/SVG/masking.html#EstablishingANewClippingPath if (hint == SvgRenderingHint.Shape || hint == SvgRenderingHint.Text || hint == SvgRenderingHint.Clipping || hint == SvgRenderingHint.Masking || hint == SvgRenderingHint.Containment) { CssPrimitiveValue clipPath = ((SvgElement)element).GetComputedCssValue("clip-path", String.Empty) as CssPrimitiveValue; if (clipPath != null && clipPath.PrimitiveType == CssPrimitiveType.Uri) { string absoluteUri = ((SvgElement)element).ResolveUri(clipPath.GetStringValue()); SvgClipPathElement eClipPath = element.OwnerDocument.GetNodeByUri(absoluteUri) as SvgClipPathElement; if (eClipPath != null) { GraphicsPath gpClip = CreateClippingRegion(eClipPath); RectangleF clipBounds = gpClip != null ? gpClip.GetBounds() : RectangleF.Empty; if (clipBounds.Width == 0 || clipBounds.Height == 0) { return; } SvgUnitType pathUnits = (SvgUnitType)eClipPath.ClipPathUnits.AnimVal; if (pathUnits == SvgUnitType.ObjectBoundingBox) { SvgTransformableElement transElement = element as SvgTransformableElement; if (transElement != null) { ISvgRect bbox = transElement.GetBBox(); // scale clipping path Matrix matrix = new Matrix(); matrix.Scale((float)bbox.Width, (float)bbox.Height); gpClip.Transform(matrix); gr.SetClip(gpClip); // offset clip gr.TranslateClip((float)bbox.X, (float)bbox.Y); } else { throw new NotImplementedException("clip-path with SvgUnitType.ObjectBoundingBox " + "not supported for this type of element: " + element.GetType()); } } else { gr.SetClip(gpClip); } gpClip.Dispose(); gpClip = null; } } } #endregion }
public override void Render(GdiGraphicsRenderer renderer) { GdiGraphicsWrapper graphics = renderer.GraphicsWrapper; SvgRenderingHint hint = element.RenderingHint; if (hint != SvgRenderingHint.Shape || hint == SvgRenderingHint.Clipping) { return; } if (element.ParentNode is SvgClipPathElement) { return; } SvgStyleableElement styleElm = (SvgStyleableElement)element; string sVisibility = styleElm.GetPropertyValue("visibility"); string sDisplay = styleElm.GetPropertyValue("display"); if (string.Equals(sVisibility, "hidden") || string.Equals(sDisplay, "none")) { return; } GraphicsPath gp = CreatePath(element); if (gp != null) { Clip(graphics); GdiSvgPaint fillPaint = new GdiSvgPaint(styleElm, "fill"); Brush brush = fillPaint.GetBrush(gp); GdiSvgPaint strokePaint = new GdiSvgPaint(styleElm, "stroke"); Pen pen = strokePaint.GetPen(gp); if (brush != null) { if (brush is PathGradientBrush) { GdiGradientFill gps = fillPaint.PaintFill as GdiGradientFill; graphics.SetClip(gps.GetRadialGradientRegion(gp.GetBounds()), CombineMode.Exclude); SolidBrush tempBrush = new SolidBrush(((PathGradientBrush)brush).InterpolationColors.Colors[0]); graphics.FillPath(this, tempBrush, gp); tempBrush.Dispose(); graphics.ResetClip(); } graphics.FillPath(this, brush, gp); brush.Dispose(); brush = null; } if (pen != null) { if (pen.Brush is PathGradientBrush) { GdiGradientFill gps = strokePaint.PaintFill as GdiGradientFill; GdiGraphicsContainer container = graphics.BeginContainer(); graphics.SetClip(gps.GetRadialGradientRegion(gp.GetBounds()), CombineMode.Exclude); SolidBrush tempBrush = new SolidBrush(((PathGradientBrush)pen.Brush).InterpolationColors.Colors[0]); Pen tempPen = new Pen(tempBrush, pen.Width); graphics.DrawPath(this, tempPen, gp); tempPen.Dispose(); tempBrush.Dispose(); graphics.EndContainer(container); } graphics.DrawPath(this, pen, gp); pen.Dispose(); pen = null; } gp.Dispose(); gp = null; } PaintMarkers(renderer, styleElm, graphics); }
private void AddGraphicsPath(SvgTextContentElement element, ref PointF ctp, string text) { if (text.Length == 0) { return; } float emSize = GetComputedFontSize(element); FontFamily family = GetGDIFontFamily(element, emSize); int style = GetGDIFontStyle(element); StringFormat sf = GetGDIStringFormat(element); GraphicsPath textGeometry = new GraphicsPath(); float xCorrection = 0; if (sf.Alignment == StringAlignment.Near) { xCorrection = emSize * 1 / 6; } else if (sf.Alignment == StringAlignment.Far) { xCorrection = -emSize * 1 / 6; } float yCorrection = (float)(family.GetCellAscent(FontStyle.Regular)) / (float)(family.GetEmHeight(FontStyle.Regular)) * emSize; // TODO: font property PointF p = new PointF(ctp.X - xCorrection, ctp.Y - yCorrection); textGeometry.AddString(text, family, style, emSize, p, sf); if (!textGeometry.GetBounds().IsEmpty) { float bboxWidth = textGeometry.GetBounds().Width; if (sf.Alignment == StringAlignment.Center) { bboxWidth /= 2; } else if (sf.Alignment == StringAlignment.Far) { bboxWidth = 0; } ctp.X += bboxWidth + emSize / 4; } GdiSvgPaint fillPaint = new GdiSvgPaint(element, "fill"); Brush brush = fillPaint.GetBrush(textGeometry); GdiSvgPaint strokePaint = new GdiSvgPaint(element, "stroke"); Pen pen = strokePaint.GetPen(textGeometry); if (brush != null) { if (brush is PathGradientBrush) { GdiGradientFill gps = fillPaint.PaintFill as GdiGradientFill; _graphics.SetClip(gps.GetRadialGradientRegion(textGeometry.GetBounds()), CombineMode.Exclude); SolidBrush tempBrush = new SolidBrush(((PathGradientBrush)brush).InterpolationColors.Colors[0]); _graphics.FillPath(this, tempBrush, textGeometry); tempBrush.Dispose(); _graphics.ResetClip(); } _graphics.FillPath(this, brush, textGeometry); brush.Dispose(); } if (pen != null) { if (pen.Brush is PathGradientBrush) { GdiGradientFill gps = strokePaint.PaintFill as GdiGradientFill; GdiGraphicsContainer container = _graphics.BeginContainer(); _graphics.SetClip(gps.GetRadialGradientRegion(textGeometry.GetBounds()), CombineMode.Exclude); SolidBrush tempBrush = new SolidBrush(((PathGradientBrush)pen.Brush).InterpolationColors.Colors[0]); Pen tempPen = new Pen(tempBrush, pen.Width); _graphics.DrawPath(this, tempPen, textGeometry); tempPen.Dispose(); tempBrush.Dispose(); _graphics.EndContainer(container); } _graphics.DrawPath(this, pen, textGeometry); pen.Dispose(); } textGeometry.Dispose(); }
protected void Clip(GdiGraphicsWrapper gr) { if (element == null) { return; } SvgRenderingHint hint = element.RenderingHint; // todo: should we correct the clipping to adjust to the off-one-pixel drawing? gr.TranslateClip(1, 1); #region Clip with clip // see http://www.w3.org/TR/SVG/masking.html#OverflowAndClipProperties if (element is ISvgSvgElement || element is ISvgMarkerElement || element is ISvgSymbolElement || element is ISvgPatternElement) { // check overflow property CssValue overflow = ((SvgElement)element).GetComputedCssValue("overflow", String.Empty) as CssValue; // TODO: clip can have "rect(10 10 auto 10)" CssPrimitiveValue clip = ((SvgElement)element).GetComputedCssValue("clip", String.Empty) as CssPrimitiveValue; string sOverflow = null; if (overflow != null || overflow.CssText == "") { sOverflow = overflow.CssText; } else { if (this is ISvgSvgElement) { sOverflow = "hidden"; } } if (sOverflow != null) { // "If the 'overflow' property has a value other than hidden or scroll, the property has no effect (i.e., a clipping rectangle is not created)." if (sOverflow == "hidden" || sOverflow == "scroll") { RectangleF clipRect = RectangleF.Empty; if (clip != null && clip.PrimitiveType == CssPrimitiveType.Rect) { if (element is ISvgSvgElement) { ISvgSvgElement svgElement = (ISvgSvgElement)element; SvgRect viewPort = svgElement.Viewport as SvgRect; clipRect = GdiConverter.ToRectangle(viewPort); ICssRect clipShape = (CssRect)clip.GetRectValue(); if (clipShape.Top.PrimitiveType != CssPrimitiveType.Ident) { clipRect.Y += (float)clipShape.Top.GetFloatValue(CssPrimitiveType.Number); } if (clipShape.Left.PrimitiveType != CssPrimitiveType.Ident) { clipRect.X += (float)clipShape.Left.GetFloatValue(CssPrimitiveType.Number); } if (clipShape.Right.PrimitiveType != CssPrimitiveType.Ident) { clipRect.Width = (clipRect.Right - clipRect.X) - (float)clipShape.Right.GetFloatValue(CssPrimitiveType.Number); } if (clipShape.Bottom.PrimitiveType != CssPrimitiveType.Ident) { clipRect.Height = (clipRect.Bottom - clipRect.Y) - (float)clipShape.Bottom.GetFloatValue(CssPrimitiveType.Number); } } } else if (clip == null || (clip.PrimitiveType == CssPrimitiveType.Ident && clip.GetStringValue() == "auto")) { if (element is ISvgSvgElement) { ISvgSvgElement svgElement = (ISvgSvgElement)element; SvgRect viewPort = svgElement.Viewport as SvgRect; clipRect = GdiConverter.ToRectangle(viewPort); } else if (element is ISvgMarkerElement || element is ISvgSymbolElement || element is ISvgPatternElement) { // TODO: what to do here? } } if (clipRect != RectangleF.Empty) { gr.SetClip(clipRect); } } } } #endregion #region Clip with clip-path // see: http://www.w3.org/TR/SVG/masking.html#EstablishingANewClippingPath if (hint == SvgRenderingHint.Shape || hint == SvgRenderingHint.Text || hint == SvgRenderingHint.Clipping || hint == SvgRenderingHint.Masking || hint == SvgRenderingHint.Containment) { CssPrimitiveValue clipPath = ((SvgElement)element).GetComputedCssValue("clip-path", String.Empty) as CssPrimitiveValue; if (clipPath != null && clipPath.PrimitiveType == CssPrimitiveType.Uri) { string absoluteUri = ((SvgElement)element).ResolveUri(clipPath.GetStringValue()); SvgClipPathElement eClipPath = element.OwnerDocument.GetNodeByUri(absoluteUri) as SvgClipPathElement; if (eClipPath != null) { GraphicsPath gpClip = CreateClippingRegion(eClipPath); RectangleF clipBounds = gpClip != null?gpClip.GetBounds() : RectangleF.Empty; if (clipBounds.Width == 0 || clipBounds.Height == 0) { return; } SvgUnitType pathUnits = (SvgUnitType)eClipPath.ClipPathUnits.AnimVal; if (pathUnits == SvgUnitType.ObjectBoundingBox) { SvgTransformableElement transElement = element as SvgTransformableElement; if (transElement != null) { ISvgRect bbox = transElement.GetBBox(); // scale clipping path Matrix matrix = new Matrix(); matrix.Scale((float)bbox.Width, (float)bbox.Height); gpClip.Transform(matrix); gr.SetClip(gpClip); // offset clip gr.TranslateClip((float)bbox.X, (float)bbox.Y); } else { throw new NotImplementedException("clip-path with SvgUnitType.ObjectBoundingBox " + "not supported for this type of element: " + element.GetType()); } } else { gr.SetClip(gpClip); } gpClip.Dispose(); gpClip = null; } } } #endregion }