// Public members public static DashStyle GetDashStyle(StyleSheets.BorderStyle borderStyle) { switch (borderStyle) { case StyleSheets.BorderStyle.Dashed: return(DashStyle.Dash); case StyleSheets.BorderStyle.Dotted: return(DashStyle.Dot); default: return(DashStyle.Solid); } }
public void PaintBorder(Graphics graphics, Rectangle rectangle, IRuleset ruleset) { GraphicsState state = graphics.Save(); bool hasRadius = ruleset.GetBorderRadii().Any(r => r > 0); if (hasRadius) { graphics.SmoothingMode = SmoothingMode.AntiAlias; } double topWidth = ruleset.BorderTopWidth?.Value ?? 0; double rightWidth = ruleset.BorderRightWidth?.Value ?? 0; double bottomWidth = ruleset.BorderBottomWidth?.Value ?? 0; double leftWidth = ruleset.BorderLeftWidth?.Value ?? 0; double topLeftRadius = ruleset.BorderTopLeftRadius?.Value ?? 0; double topRightRadius = ruleset.BorderTopRightRadius?.Value ?? 0; double bottomRightRadius = ruleset.BorderBottomRightRadius?.Value ?? 0; double bottomLeftRadius = ruleset.BorderBottomLeftRadius?.Value ?? 0; double horizontalBorderWidth = leftWidth + rightWidth; double verticalBorderWidth = topWidth + bottomWidth; int rectX = rectangle.X + (int)(leftWidth / 2); int rectY = rectangle.Y + (int)(topWidth / 2); int rectWidth = rectangle.Width - (int)(horizontalBorderWidth / 2); int rectHeight = rectangle.Height - (int)(verticalBorderWidth / 2); Rectangle drawRect = new Rectangle(rectX, rectY, rectWidth - (rightWidth == 1 && leftWidth <= 0 ? 1 : 0), rectHeight - (bottomWidth == 1 && topWidth <= 0 ? 1 : 0)); float opacity = (float)(ruleset.Opacity?.Value ?? 1.0f); using (Pen pen = new Pen(Color.Black)) { pen.Alignment = PenAlignment.Center; pen.StartCap = LineCap.Square; if (topWidth > 0) { StyleSheets.BorderStyle borderStyle = ruleset.BorderTopStyle?.Value ?? StyleSheets.BorderStyle.Solid; if (borderStyle != StyleSheets.BorderStyle.None && borderStyle != StyleSheets.BorderStyle.Hidden) { pen.Width = (float)topWidth; pen.Color = RenderUtilities.GetColorWithAlpha(ruleset.BorderTopColor?.Value ?? default, opacity); pen.DashStyle = RenderUtilities.GetDashStyle(borderStyle); using (GraphicsPath path = RenderUtilities.CreateBorderPath(drawRect, BorderPathType.Top, topLeftRadius, topRightRadius, bottomRightRadius, bottomLeftRadius)) graphics.DrawPath(pen, path); } } if (rightWidth > 0) { StyleSheets.BorderStyle borderStyle = ruleset.BorderRightStyle?.Value ?? StyleSheets.BorderStyle.Solid; if (borderStyle != StyleSheets.BorderStyle.None && borderStyle != StyleSheets.BorderStyle.Hidden) { pen.Width = (float)rightWidth; pen.Color = RenderUtilities.GetColorWithAlpha(ruleset.BorderRightColor?.Value ?? default, opacity); pen.DashStyle = RenderUtilities.GetDashStyle(borderStyle); using (GraphicsPath path = RenderUtilities.CreateBorderPath(drawRect, BorderPathType.Right, topLeftRadius, topRightRadius, bottomRightRadius, bottomLeftRadius)) graphics.DrawPath(pen, path); } } if (bottomWidth > 0) { StyleSheets.BorderStyle borderStyle = ruleset.BorderBottomStyle?.Value ?? StyleSheets.BorderStyle.Solid; if (borderStyle != StyleSheets.BorderStyle.None && borderStyle != StyleSheets.BorderStyle.Hidden) { pen.Width = (float)bottomWidth; pen.Color = RenderUtilities.GetColorWithAlpha(ruleset.BorderBottomColor?.Value ?? default, opacity); pen.DashStyle = RenderUtilities.GetDashStyle(borderStyle); using (GraphicsPath path = RenderUtilities.CreateBorderPath(drawRect, BorderPathType.Bottom, topLeftRadius, topRightRadius, bottomRightRadius, bottomLeftRadius)) graphics.DrawPath(pen, path); } } if (leftWidth > 0) { StyleSheets.BorderStyle borderStyle = ruleset.BorderLeftStyle?.Value ?? StyleSheets.BorderStyle.Solid; if (borderStyle != StyleSheets.BorderStyle.None && borderStyle != StyleSheets.BorderStyle.Hidden) { pen.Width = (float)leftWidth; pen.Color = RenderUtilities.GetColorWithAlpha(ruleset.BorderLeftColor?.Value ?? default, opacity); pen.DashStyle = RenderUtilities.GetDashStyle(borderStyle); using (GraphicsPath path = RenderUtilities.CreateBorderPath(drawRect, BorderPathType.Left, topLeftRadius, topRightRadius, bottomRightRadius, bottomLeftRadius)) graphics.DrawPath(pen, path); } } } graphics.Restore(state); }