Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the number of SVG rectangles #265

Merged
merged 1 commit into from
Apr 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 69 additions & 14 deletions QRCoder/SvgQRCode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#if NETFRAMEWORK || NETSTANDARD2_0
#if NETFRAMEWORK || NETSTANDARD2_0
using System;
using System.Collections;
using System.Drawing;
using System.Text;
using static QRCoder.QRCodeGenerator;
Expand Down Expand Up @@ -44,22 +45,76 @@ public string GetGraphic(Size viewBox, Color darkColor, Color lightColor, bool d

public string GetGraphic(Size viewBox, string darkColorHex, string lightColorHex, bool drawQuietZones = true, SizingMode sizingMode = SizingMode.WidthHeightAttribute)
{
var offset = drawQuietZones ? 0 : 4;
var drawableModulesCount = this.QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : offset * 2);
var pixelsPerModule = (double)Math.Min(viewBox.Width, viewBox.Height) / (double)drawableModulesCount;
var qrSize = drawableModulesCount * pixelsPerModule;
var svgSizeAttributes = (sizingMode == SizingMode.WidthHeightAttribute) ? $@"width=""{viewBox.Width}"" height=""{viewBox.Height}""" : $@"viewBox=""0 0 {viewBox.Width} {viewBox.Height}""";
var svgFile = new StringBuilder($@"<svg version=""1.1"" baseProfile=""full"" shape-rendering=""crispEdges"" {svgSizeAttributes} xmlns=""http://www.w3.org/2000/svg"">");
int offset = drawQuietZones ? 0 : 4;
int drawableModulesCount = this.QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : offset * 2);
double pixelsPerModule = Math.Min(viewBox.Width, viewBox.Height) / (double)drawableModulesCount;
double qrSize = drawableModulesCount * pixelsPerModule;
string svgSizeAttributes = (sizingMode == SizingMode.WidthHeightAttribute) ? $@"width=""{viewBox.Width}"" height=""{viewBox.Height}""" : $@"viewBox=""0 0 {viewBox.Width} {viewBox.Height}""";

// Merge horizontal rectangles
int[,] matrix = new int[drawableModulesCount, drawableModulesCount];
for (int yi = 0; yi < drawableModulesCount; yi += 1)
{
BitArray bitArray = this.QrCodeData.ModuleMatrix[yi];

int x0 = -1;
int xL = 0;
for (int xi = 0; xi < drawableModulesCount; xi += 1)
{
matrix[yi, xi] = 0;
if (bitArray[xi])
{
if(x0 == -1)
{
x0 = xi;
}
xL += 1;
}
else
{
if(xL > 0)
{
matrix[yi, x0] = xL;
x0 = -1;
xL = 0;
}
}
}

if (xL > 0)
{
matrix[yi, x0] = xL;
}
}

StringBuilder svgFile = new StringBuilder($@"<svg version=""1.1"" baseProfile=""full"" shape-rendering=""crispEdges"" {svgSizeAttributes} xmlns=""http://www.w3.org/2000/svg"">");
svgFile.AppendLine($@"<rect x=""0"" y=""0"" width=""{CleanSvgVal(qrSize)}"" height=""{CleanSvgVal(qrSize)}"" fill=""{lightColorHex}"" />");
for (int xi = offset; xi < offset + drawableModulesCount; xi++)
for (int yi = 0; yi < drawableModulesCount; yi += 1)
{
for (int yi = offset; yi < offset + drawableModulesCount; yi++)
double y = yi * pixelsPerModule;
for (int xi = 0; xi < drawableModulesCount; xi += 1)
{
if (this.QrCodeData.ModuleMatrix[yi][xi])
int xL = matrix[yi, xi];
if(xL > 0)
{
var x = (xi - offset) * pixelsPerModule;
var y = (yi - offset) * pixelsPerModule;
svgFile.AppendLine($@"<rect x=""{CleanSvgVal(x)}"" y=""{CleanSvgVal(y)}"" width=""{CleanSvgVal(pixelsPerModule)}"" height=""{CleanSvgVal(pixelsPerModule)}"" fill=""{darkColorHex}"" />");
// Merge vertical rectangles
int yL = 1;
for (int y2 = yi + 1; y2 < drawableModulesCount; y2 += 1)
{
if(matrix[y2, xi] == xL)
{
matrix[y2, xi] = 0;
yL += 1;
}
else
{
break;
}
}

// Output SVG rectangles
double x = xi * pixelsPerModule;
svgFile.AppendLine($@"<rect x=""{CleanSvgVal(x)}"" y=""{CleanSvgVal(y)}"" width=""{CleanSvgVal(xL * pixelsPerModule)}"" height=""{CleanSvgVal(yL * pixelsPerModule)}"" fill=""{darkColorHex}"" />");
}
}
}
Expand Down Expand Up @@ -92,4 +147,4 @@ public static string GetQRCode(string plainText, int pixelsPerModule, string dar
}
}

#endif
#endif