From 6c29817c5004cea3c2648398612879daf623661b Mon Sep 17 00:00:00 2001 From: Jaex Date: Wed, 24 Aug 2016 22:18:54 +0300 Subject: [PATCH] Handle speech balloon border drawing issues --- .../Drawing/SpeechBalloonDrawingShape.cs | 88 +++++++++++++------ .../Shapes/Drawing/TextDrawingShape.cs | 5 ++ 2 files changed, 67 insertions(+), 26 deletions(-) diff --git a/ShareX.ScreenCaptureLib/Shapes/Drawing/SpeechBalloonDrawingShape.cs b/ShareX.ScreenCaptureLib/Shapes/Drawing/SpeechBalloonDrawingShape.cs index e32f7aa6d..d4efd9000 100644 --- a/ShareX.ScreenCaptureLib/Shapes/Drawing/SpeechBalloonDrawingShape.cs +++ b/ShareX.ScreenCaptureLib/Shapes/Drawing/SpeechBalloonDrawingShape.cs @@ -54,55 +54,91 @@ public override void OnNodeVisible() public override void OnDraw(Graphics g) { - if (Rectangle.Width > 10 && Rectangle.Height > 10 && !Rectangle.Contains(TailNode.Position)) + if (Rectangle.Width > 10 && Rectangle.Height > 10) { - DrawTail(g); - } + GraphicsPath gpTail = null; - base.OnDraw(g); - } + if (!Rectangle.Contains(TailNode.Position)) + { + g.SmoothingMode = SmoothingMode.HighQuality; + g.ExcludeClip(Rectangle); - private void DrawTail(Graphics g) - { - g.ExcludeClip(Rectangle); + gpTail = CreateTailPath(TailWidth); - using (GraphicsPath gpTail = new GraphicsPath()) - { - Point center = Rectangle.Center(); - int tailOrigin = TailWidth / 2; - int tailLength = (int)MathHelpers.Distance(center, TailNode.Position); - gpTail.AddLine(0, -tailOrigin, 0, tailOrigin); - gpTail.AddLine(0, tailOrigin, tailLength, 0); - gpTail.CloseFigure(); + if (FillColor.A > 0) + { + using (Brush brush = new SolidBrush(FillColor)) + { + g.FillPath(brush, gpTail); + } + } - g.TranslateTransform(center.X, center.Y); - float tailDegree = MathHelpers.LookAtDegree(center, TailNode.Position); - g.RotateTransform(tailDegree); + if (BorderSize > 0 && BorderColor.A > 0) + { + using (Pen pen = new Pen(BorderColor, BorderSize)) + { + g.DrawPath(pen, gpTail); + } + } - g.SmoothingMode = SmoothingMode.HighQuality; + g.SmoothingMode = SmoothingMode.None; + g.ResetClip(); + } if (FillColor.A > 0) { using (Brush brush = new SolidBrush(FillColor)) { - g.FillPath(brush, gpTail); + g.FillRectangle(brush, Rectangle); } } if (BorderSize > 0 && BorderColor.A > 0) { - using (Pen pen = new Pen(BorderColor, BorderSize)) + if (gpTail != null) { - g.DrawPath(pen, gpTail); + using (Region region = new Region(gpTail)) + { + g.ExcludeClip(region); + } } + + Rectangle rect = Rectangle.Offset(BorderSize - 1); + + using (Pen pen = new Pen(BorderColor, BorderSize) { Alignment = PenAlignment.Inset }) + { + g.DrawRectangleProper(pen, rect); + } + + g.ResetClip(); } - g.SmoothingMode = SmoothingMode.None; + if (gpTail != null) + { + gpTail.Dispose(); + } - g.ResetTransform(); + DrawText(g); } + } - g.ResetClip(); + protected GraphicsPath CreateTailPath(int tailWidth) + { + GraphicsPath gpTail = new GraphicsPath(); + Point center = Rectangle.Center(); + int tailOrigin = tailWidth / 2; + int tailLength = (int)MathHelpers.Distance(center, TailNode.Position); + gpTail.AddLine(0, -tailOrigin, 0, tailOrigin); + gpTail.AddLine(0, tailOrigin, tailLength, 0); + gpTail.CloseFigure(); + using (Matrix matrix = new Matrix()) + { + matrix.Translate(center.X, center.Y); + float tailDegree = MathHelpers.LookAtDegree(center, TailNode.Position); + matrix.Rotate(tailDegree); + gpTail.Transform(matrix); + } + return gpTail; } public override void OnNodeUpdate() diff --git a/ShareX.ScreenCaptureLib/Shapes/Drawing/TextDrawingShape.cs b/ShareX.ScreenCaptureLib/Shapes/Drawing/TextDrawingShape.cs index 14260fa11..cac44be6e 100644 --- a/ShareX.ScreenCaptureLib/Shapes/Drawing/TextDrawingShape.cs +++ b/ShareX.ScreenCaptureLib/Shapes/Drawing/TextDrawingShape.cs @@ -56,6 +56,11 @@ public override void OnDraw(Graphics g) { base.OnDraw(g); + DrawText(g); + } + + protected void DrawText(Graphics g) + { if (!string.IsNullOrEmpty(Text) && Rectangle.Width > 10 && Rectangle.Height > 10) { using (Font font = new Font(TextOptions.Font, TextOptions.Size, TextOptions.Style))