Skip to content

Fix svg rendering #118

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

Merged
merged 1 commit into from
Apr 21, 2018
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
32 changes: 13 additions & 19 deletions QRCoder/SvgQRCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,24 @@ public string GetGraphic(Size viewBox, Color darkColor, Color lightColor, bool d
}

public string GetGraphic(Size viewBox, string darkColorHex, string lightColorHex, bool drawQuietZones = true)
{
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"">");
var drawableModulesCount = this.QrCodeData.ModuleMatrix.Count - (drawQuietZones ? 0 : 8);
var unitsPerModule = Math.Round(Convert.ToDouble(Math.Min(viewBox.Width, viewBox.Height)) / drawableModulesCount,4);
if (unitsPerModule * drawableModulesCount > viewBox.Width)
unitsPerModule -= 0.0001;
var offset = drawQuietZones ? 4 * unitsPerModule : 0;
var offsetModules = drawQuietZones ? 0 : 4;
var qrSize = unitsPerModule * drawableModulesCount;

svgFile.AppendLine($@"<rect x=""0"" y=""0"" width=""{CleanSvgVal(qrSize)}"" height=""{CleanSvgVal(qrSize)}"" fill=""" + lightColorHex + @""" />");
int xi = 0, yi = 0;
for (var x = 0d; x < qrSize; x = x + unitsPerModule)
{
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 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"">");
svgFile.AppendLine($@"<rect x=""0"" y=""0"" width=""{CleanSvgVal(qrSize)}"" height=""{CleanSvgVal(qrSize)}"" fill=""{lightColorHex}"" />");
for (int xi = offset; xi < offset + drawableModulesCount; xi++)
{
yi = 0;
for (var y = 0d; y < qrSize; y = y + unitsPerModule)
for (int yi = offset; yi < offset + drawableModulesCount; yi++)
{
if (this.QrCodeData.ModuleMatrix[yi + offsetModules][xi + offsetModules])
if (this.QrCodeData.ModuleMatrix[xi][yi])
{
svgFile.AppendLine($@"<rect x=""{CleanSvgVal(x)}"" y=""{CleanSvgVal(y)}"" width=""{CleanSvgVal(unitsPerModule)}"" height=""{CleanSvgVal(unitsPerModule)}"" fill=""{darkColorHex}"" />");
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}"" />");
}
yi++;
}
xi++;
}
svgFile.Append(@"</svg>");
return svgFile.ToString();
Expand Down