Skip to content

Commit 0b8832b

Browse files
authored
Merge pull request #118 from nitrovent/master
Fix svg rendering
2 parents bcb8937 + f3efa72 commit 0b8832b

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

QRCoder/SvgQRCode.cs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,24 @@ public string GetGraphic(Size viewBox, Color darkColor, Color lightColor, bool d
4040
}
4141

4242
public string GetGraphic(Size viewBox, string darkColorHex, string lightColorHex, bool drawQuietZones = true)
43-
{
44-
var svgFile = new StringBuilder(@"<svg version=""1.1"" baseProfile=""full"" shape-rendering=""crispEdges"" width=""" +viewBox.Width+ @""" height="""+viewBox.Height+ @""" xmlns=""http://www.w3.org/2000/svg"">");
45-
var drawableModulesCount = this.QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8);
46-
var unitsPerModule = Math.Round(Convert.ToDouble(Math.Min(viewBox.Width, viewBox.Height)) / drawableModulesCount,4);
47-
if (unitsPerModule * drawableModulesCount > viewBox.Width)
48-
unitsPerModule -= 0.0001;
49-
var offset = drawQuietZones ? 4 * unitsPerModule : 0;
50-
var offsetModules = drawQuietZones ? 0 : 4;
51-
var qrSize = unitsPerModule * drawableModulesCount;
52-
53-
svgFile.AppendLine($@"<rect x=""0"" y=""0"" width=""{CleanSvgVal(qrSize)}"" height=""{CleanSvgVal(qrSize)}"" fill=""" + lightColorHex + @""" />");
54-
int xi = 0, yi = 0;
55-
for (var x = 0d; x < qrSize; x = x + unitsPerModule)
43+
{
44+
var offset = drawQuietZones ? 0 : 4;
45+
var drawableModulesCount = this.QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : offset * 2);
46+
var pixelsPerModule = (double)Math.Min(viewBox.Width, viewBox.Height) / (double)drawableModulesCount;
47+
var qrSize = drawableModulesCount * pixelsPerModule;
48+
var svgFile = new StringBuilder($@"<svg version=""1.1"" baseProfile=""full"" shape-rendering=""crispEdges"" width=""{viewBox.Width}"" height=""{viewBox.Height}"" xmlns=""http://www.w3.org/2000/svg"">");
49+
svgFile.AppendLine($@"<rect x=""0"" y=""0"" width=""{CleanSvgVal(qrSize)}"" height=""{CleanSvgVal(qrSize)}"" fill=""{lightColorHex}"" />");
50+
for (int xi = offset; xi < offset + drawableModulesCount; xi++)
5651
{
57-
yi = 0;
58-
for (var y = 0d; y < qrSize; y = y + unitsPerModule)
52+
for (int yi = offset; yi < offset + drawableModulesCount; yi++)
5953
{
60-
if (this.QrCodeData.ModuleMatrix[yi + offsetModules][xi + offsetModules])
54+
if (this.QrCodeData.ModuleMatrix[xi][yi])
6155
{
62-
svgFile.AppendLine($@"<rect x=""{CleanSvgVal(x)}"" y=""{CleanSvgVal(y)}"" width=""{CleanSvgVal(unitsPerModule)}"" height=""{CleanSvgVal(unitsPerModule)}"" fill=""{darkColorHex}"" />");
56+
var x = (xi - offset) * pixelsPerModule;
57+
var y = (yi - offset) * pixelsPerModule;
58+
svgFile.AppendLine($@"<rect x=""{CleanSvgVal(x)}"" y=""{CleanSvgVal(y)}"" width=""{CleanSvgVal(pixelsPerModule)}"" height=""{CleanSvgVal(pixelsPerModule)}"" fill=""{darkColorHex}"" />");
6359
}
64-
yi++;
6560
}
66-
xi++;
6761
}
6862
svgFile.Append(@"</svg>");
6963
return svgFile.ToString();

0 commit comments

Comments
 (0)