From 8fd2ec0b71812b8ca1e1a6feec3d27ee776656e0 Mon Sep 17 00:00:00 2001 From: Piripe Date: Fri, 22 Jul 2022 02:55:13 +0200 Subject: [PATCH] Implemented negative `border-radius` (Used to make concave border) --- Oxygen/Modules/Exporter.cs | 13 +++---------- Oxygen/Modules/Extensions.cs | 35 ++++++++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/Oxygen/Modules/Exporter.cs b/Oxygen/Modules/Exporter.cs index f225afe..bc80967 100644 --- a/Oxygen/Modules/Exporter.cs +++ b/Oxygen/Modules/Exporter.cs @@ -388,7 +388,7 @@ void RenderSrc() } if (int.TryParse((tgaInfo.Attribute("height") ?? new XAttribute("height", imageWidth.ToString())).Value, out int srcHeight)) { - if (srcWidth < 1) + if (srcHeight < 1) { ErrorManager.Error("\"height\" Attribute must be positive.", file, destfile); return; @@ -403,20 +403,13 @@ void RenderSrc() if (float.TryParse((tgaInfo.Attribute("stroke-width") ?? new XAttribute("stroke-width", "1")).Value, out float strokeWidth)) { - if (srcWidth < 1) + if (strokeWidth < 1) { ErrorManager.Error("\"stroke-width\" Attribute must be positive.", file, destfile); return; } if (int.TryParse((tgaInfo.Attribute("border-radius") ?? new XAttribute("border-radius", "0")).Value, out int borderRadius)) { - - if (srcWidth < 0) - { - ErrorManager.Error("\"border-radius\" Attribute must be greater or equal to 0.", file, destfile); - return; - } - int strokePathOffset = 0; switch (strokeAlign) @@ -453,7 +446,7 @@ void RenderSrc() { img_g.SmoothingMode = SmoothingMode.HighQuality; graphicsPath.AddRoundedRectangle(new RectangleF(srcX - 0.5f, srcY - 0.5f, srcWidth, srcHeight), borderRadius); - strokeGraphicsPath.AddRoundedRectangle(new RectangleF(srcX - strokePathOffset / 2f - 0.5f, srcY - strokePathOffset / 2f - 0.5f, srcWidth + strokePathOffset, srcHeight + strokePathOffset), borderRadius + strokePathOffset/2); + strokeGraphicsPath.AddRoundedRectangle(new RectangleF(srcX - strokePathOffset / 2f - 0.5f, srcY - strokePathOffset / 2f - 0.5f, srcWidth + strokePathOffset, srcHeight + strokePathOffset), borderRadius + (borderRadius > 0 ? strokePathOffset : -strokePathOffset) / 2); } break; case "ellipse": diff --git a/Oxygen/Modules/Extensions.cs b/Oxygen/Modules/Extensions.cs index 7ff516e..da8ea6d 100644 --- a/Oxygen/Modules/Extensions.cs +++ b/Oxygen/Modules/Extensions.cs @@ -53,15 +53,32 @@ internal static void SetOrAdd(this Dictionary dict, T key, U value) internal static void AddRoundedRectangle(this GraphicsPath path, RectangleF rect, float radius) { - radius = Math.Min(radius, rect.Width/2); - path.AddArc(rect.Left, rect.Top, radius*2, radius * 2, 180, 90); - path.AddLine(rect.Left+radius,rect.Top,rect.Right-radius,rect.Top); - path.AddArc(rect.Right-radius * 2, rect.Top, radius * 2, radius * 2, 270, 90); - path.AddLine(rect.Right, rect.Top + radius, rect.Right, rect.Bottom-radius); - path.AddArc(rect.Right - radius * 2, rect.Bottom-radius * 2, radius * 2, radius * 2, 0, 90); - path.AddLine(rect.Right-radius, rect.Bottom, rect.Left+radius, rect.Bottom); - path.AddArc(rect.Left, rect.Bottom - radius * 2, radius * 2, radius * 2, 90, 90); - path.AddLine(rect.Left, rect.Bottom-radius, rect.Left, rect.Top+radius); + if (radius > 0) + { + radius = Math.Min(radius, rect.Width / 2); + path.AddArc(rect.Left, rect.Top, radius * 2, radius * 2, 180, 90); + path.AddLine(rect.Left + radius, rect.Top, rect.Right - radius, rect.Top); + path.AddArc(rect.Right - radius * 2, rect.Top, radius * 2, radius * 2, 270, 90); + path.AddLine(rect.Right, rect.Top + radius, rect.Right, rect.Bottom - radius); + path.AddArc(rect.Right - radius * 2, rect.Bottom - radius * 2, radius * 2, radius * 2, 0, 90); + path.AddLine(rect.Right - radius, rect.Bottom, rect.Left + radius, rect.Bottom); + path.AddArc(rect.Left, rect.Bottom - radius * 2, radius * 2, radius * 2, 90, 90); + path.AddLine(rect.Left, rect.Bottom - radius, rect.Left, rect.Top + radius); + path.CloseFigure(); + } + else + { + radius = Math.Max(radius, -rect.Width / 2); + path.AddArc(rect.Left+radius, rect.Top+radius, radius * -2, radius * -2, 90, -90); + path.AddLine(rect.Left - radius, rect.Top, rect.Right + radius, rect.Top); + path.AddArc(rect.Right + radius, rect.Top + radius, radius * -2, radius * -2, 180, -90); + path.AddLine(rect.Right, rect.Top - radius, rect.Right, rect.Bottom + radius); + path.AddArc(rect.Right + radius, rect.Bottom + radius, radius * -2, radius * -2, 270, -90); + path.AddLine(rect.Right + radius, rect.Bottom, rect.Left - radius, rect.Bottom); + path.AddArc(rect.Left + radius, rect.Bottom + radius, radius * -2, radius * -2, 0, -90); + path.AddLine(rect.Left, rect.Bottom + radius, rect.Left, rect.Top - radius); + path.CloseFigure(); + } } #endregion