From 6cefe92f7707a17cbd1bce68e0a104d330f4e3f1 Mon Sep 17 00:00:00 2001 From: Raffael Herrmann Date: Wed, 24 Apr 2024 23:09:02 +0200 Subject: [PATCH 1/3] Re-used existing helper function in Svg tests --- QRCoderTests/SvgQRCodeRendererTests.cs | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/QRCoderTests/SvgQRCodeRendererTests.cs b/QRCoderTests/SvgQRCodeRendererTests.cs index 0981e6e2..0204c4af 100644 --- a/QRCoderTests/SvgQRCodeRendererTests.cs +++ b/QRCoderTests/SvgQRCodeRendererTests.cs @@ -15,16 +15,6 @@ namespace QRCoderTests public class SvgQRCodeRendererTests { - private string GetAssemblyPath() - { - return -#if NET5_0 - AppDomain.CurrentDomain.BaseDirectory; -#else - Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).Replace("file:\\", ""); -#endif - } - [Fact] [Category("QRRenderer/SvgQRCode")] public void can_render_svg_qrcode_simple() @@ -113,7 +103,7 @@ public void can_render_svg_qrcode_with_png_logo_bitmap() var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H); //Used logo is licensed under public domain. Ref.: https://thenounproject.com/Iconathon1/collection/redefining-women/?i=2909346 - var logoBitmap = (Bitmap)Image.FromFile(GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png"); + var logoBitmap = (Bitmap)Image.FromFile(HelperFunctions.GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png"); var logoObj = new SvgQRCode.SvgLogo(iconRasterized: logoBitmap, 15); logoObj.GetMediaType().ShouldBe(SvgQRCode.SvgLogo.MediaType.PNG); @@ -133,7 +123,7 @@ public void can_render_svg_qrcode_with_png_logo_bytearray() var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H); //Used logo is licensed under public domain. Ref.: https://thenounproject.com/Iconathon1/collection/redefining-women/?i=2909346 - var logoBitmap = System.IO.File.ReadAllBytes(GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png"); + var logoBitmap = System.IO.File.ReadAllBytes(HelperFunctions.GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png"); var logoObj = new SvgQRCode.SvgLogo(iconRasterized: logoBitmap, 15); logoObj.GetMediaType().ShouldBe(SvgQRCode.SvgLogo.MediaType.PNG); @@ -152,7 +142,7 @@ public void can_render_svg_qrcode_with_svg_logo_embedded() var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H); //Used logo is licensed under public domain. Ref.: https://thenounproject.com/Iconathon1/collection/redefining-women/?i=2909361 - var logoSvg = File.ReadAllText(GetAssemblyPath() + "\\assets\\noun_Scientist_2909361.svg"); + var logoSvg = File.ReadAllText(HelperFunctions.GetAssemblyPath() + "\\assets\\noun_Scientist_2909361.svg"); var logoObj = new SvgQRCode.SvgLogo(logoSvg, 20); logoObj.GetMediaType().ShouldBe(SvgQRCode.SvgLogo.MediaType.SVG); @@ -171,7 +161,7 @@ public void can_render_svg_qrcode_with_svg_logo_image_tag() var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H); //Used logo is licensed under public domain. Ref.: https://thenounproject.com/Iconathon1/collection/redefining-women/?i=2909361 - var logoSvg = File.ReadAllText(GetAssemblyPath() + "\\assets\\noun_Scientist_2909361.svg"); + var logoSvg = File.ReadAllText(HelperFunctions.GetAssemblyPath() + "\\assets\\noun_Scientist_2909361.svg"); var logoObj = new SvgQRCode.SvgLogo(logoSvg, 20, iconEmbedded: false); var svg = new SvgQRCode(data).GetGraphic(10, Color.DarkGray, Color.White, logo: logoObj); From f7610dab4499c75382b7ce3c0011fae7f4562930 Mon Sep 17 00:00:00 2001 From: Raffael Herrmann Date: Wed, 24 Apr 2024 23:33:36 +0200 Subject: [PATCH 2/3] Fixed logo background rendering without quietzones in SvgQRCode --- QRCoder/SvgQRCode.cs | 2 +- QRCoderTests/SvgQRCodeRendererTests.cs | 40 +++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/QRCoder/SvgQRCode.cs b/QRCoder/SvgQRCode.cs index 81cc2486..89599680 100644 --- a/QRCoder/SvgQRCode.cs +++ b/QRCoder/SvgQRCode.cs @@ -126,7 +126,7 @@ public string GetGraphic(Size viewBox, string darkColorHex, string lightColorHex for (int xi = 0; xi < drawableModulesCount; xi += 1) { matrix[yi, xi] = 0; - if (bitArray[xi+offset] && (logo == null || !logo.FillLogoBackground() || !IsBlockedByLogo((xi+offset)*pixelsPerModule, (yi+offset) * pixelsPerModule, logoAttr, pixelsPerModule))) + if (bitArray[xi+offset] && (logo == null || !logo.FillLogoBackground() || !IsBlockedByLogo(xi * pixelsPerModule, yi * pixelsPerModule, logoAttr, pixelsPerModule))) { if(x0 == -1) { diff --git a/QRCoderTests/SvgQRCodeRendererTests.cs b/QRCoderTests/SvgQRCodeRendererTests.cs index 0204c4af..68e61a55 100644 --- a/QRCoderTests/SvgQRCodeRendererTests.cs +++ b/QRCoderTests/SvgQRCodeRendererTests.cs @@ -108,10 +108,48 @@ public void can_render_svg_qrcode_with_png_logo_bitmap() logoObj.GetMediaType().ShouldBe(SvgQRCode.SvgLogo.MediaType.PNG); var svg = new SvgQRCode(data).GetGraphic(10, Color.DarkGray, Color.White, logo: logoObj); - + File.WriteAllText(@"C:\Users\netbl\Downloads\1.svg", svg); var result = HelperFunctions.StringToHash(svg); result.ShouldBe("78e02e8ba415f15817d5ed88c4afca31"); } + + [Fact] + [Category("QRRenderer/SvgQRCode")] + public void can_render_svg_qrcode_with_png_logo_bitmap_without_background() + { + //Create QR code + var gen = new QRCodeGenerator(); + var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H); + + //Used logo is licensed under public domain. Ref.: https://thenounproject.com/Iconathon1/collection/redefining-women/?i=2909346 + var logoBitmap = (Bitmap)Image.FromFile(HelperFunctions.GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png"); + var logoObj = new SvgQRCode.SvgLogo(iconRasterized: logoBitmap, 15, false); + logoObj.GetMediaType().ShouldBe(SvgQRCode.SvgLogo.MediaType.PNG); + + var svg = new SvgQRCode(data).GetGraphic(10, Color.DarkGray, Color.White, logo: logoObj); + File.WriteAllText(@"C:\Users\netbl\Downloads\2.svg", svg); + var result = HelperFunctions.StringToHash(svg); + result.ShouldBe("f221b2baecc2883f8e8ae54f12ba701b"); + } + + [Fact] + [Category("QRRenderer/SvgQRCode")] + public void can_render_svg_qrcode_with_png_logo_bitmap_without_quietzones() + { + //Create QR code + var gen = new QRCodeGenerator(); + var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H); + + //Used logo is licensed under public domain. Ref.: https://thenounproject.com/Iconathon1/collection/redefining-women/?i=2909346 + var logoBitmap = (Bitmap)Image.FromFile(HelperFunctions.GetAssemblyPath() + "\\assets\\noun_software engineer_2909346.png"); + var logoObj = new SvgQRCode.SvgLogo(iconRasterized: logoBitmap, 15); + logoObj.GetMediaType().ShouldBe(SvgQRCode.SvgLogo.MediaType.PNG); + + var svg = new SvgQRCode(data).GetGraphic(10, Color.Black, Color.White, drawQuietZones: false, logo: logoObj); + File.WriteAllText(@"C:\Users\netbl\Downloads\3.svg", svg); + var result = HelperFunctions.StringToHash(svg); + result.ShouldBe("8b4d114136c7fd26e0b34e5a15daac3b"); + } #endif [Fact] From 538301dca1aca1632a914a45cbc43f470a24099d Mon Sep 17 00:00:00 2001 From: Raffael Herrmann Date: Wed, 24 Apr 2024 23:34:29 +0200 Subject: [PATCH 3/3] Cleaned test cases --- QRCoderTests/SvgQRCodeRendererTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/QRCoderTests/SvgQRCodeRendererTests.cs b/QRCoderTests/SvgQRCodeRendererTests.cs index 68e61a55..158d9bc9 100644 --- a/QRCoderTests/SvgQRCodeRendererTests.cs +++ b/QRCoderTests/SvgQRCodeRendererTests.cs @@ -108,7 +108,7 @@ public void can_render_svg_qrcode_with_png_logo_bitmap() logoObj.GetMediaType().ShouldBe(SvgQRCode.SvgLogo.MediaType.PNG); var svg = new SvgQRCode(data).GetGraphic(10, Color.DarkGray, Color.White, logo: logoObj); - File.WriteAllText(@"C:\Users\netbl\Downloads\1.svg", svg); + var result = HelperFunctions.StringToHash(svg); result.ShouldBe("78e02e8ba415f15817d5ed88c4afca31"); } @@ -127,7 +127,7 @@ public void can_render_svg_qrcode_with_png_logo_bitmap_without_background() logoObj.GetMediaType().ShouldBe(SvgQRCode.SvgLogo.MediaType.PNG); var svg = new SvgQRCode(data).GetGraphic(10, Color.DarkGray, Color.White, logo: logoObj); - File.WriteAllText(@"C:\Users\netbl\Downloads\2.svg", svg); + var result = HelperFunctions.StringToHash(svg); result.ShouldBe("f221b2baecc2883f8e8ae54f12ba701b"); } @@ -146,7 +146,7 @@ public void can_render_svg_qrcode_with_png_logo_bitmap_without_quietzones() logoObj.GetMediaType().ShouldBe(SvgQRCode.SvgLogo.MediaType.PNG); var svg = new SvgQRCode(data).GetGraphic(10, Color.Black, Color.White, drawQuietZones: false, logo: logoObj); - File.WriteAllText(@"C:\Users\netbl\Downloads\3.svg", svg); + var result = HelperFunctions.StringToHash(svg); result.ShouldBe("8b4d114136c7fd26e0b34e5a15daac3b"); }