Skip to content

Commit 2a8a8a3

Browse files
authored
Fix SVG to have a view box based on the encoding size and enable scaling to be handled by the Width/Height and the renderer (browser). (#241)
This improves the central positioning of QR Code, before the QR Code depending on the size of the encoded value would have different padding on the right/bottom since the scaling was all in integers.
1 parent ff4d5af commit 2a8a8a3

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

CodeBeam.MudBlazor.Extensions/Components/Barcode/MudBarcode.razor

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22
@inherits MudComponentBase
33

44
<a href="@(Clickable ? Value : null)" target="@Target">
5-
@{
5+
@{
66
var content = GetCode();
77
}
88
@if (content != null)
99
{
10-
<svg width="@Width" height="@Height" style="background-color:@BackgroundColor">
10+
var viewBoxWidth = @content.ModuleSizeX * content.Columns;
11+
var viewBoxHeight = @content.ModuleSizeY * content.Rows;
12+
13+
<svg width="@Width" height="@Height" style="background-color:@BackgroundColor" viewBox="0 0 @viewBoxWidth @viewBoxHeight">
1114
@for (int y = 0; y < content.Rows; y++)
1215
{
13-
@for(int x = 0; x < content.Columns; x++)
16+
@for (int x = 0; x < content.Columns; x++)
1417
{
15-
@if(content[x,y])
18+
@if (content[x, y])
1619
{
1720
<rect class="d-flex align-center justify-center" width="@content.ModuleSizeX" height="@content.ModuleSizeY" style="@($"fill:{Color}; stroke-width:{StrokeWidth}px; stroke:{Color}")"
1821
x="@(x * content.ModuleSizeX)" y="@(y * content.ModuleSizeY)" />

CodeBeam.MudBlazor.Extensions/Components/Barcode/MudBarcode.razor.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public partial class MudBarcode : MudComponentBase
2828
/// Increase the stroke width if readers can not read the barcode easily.
2929
/// </summary>
3030
[Parameter]
31-
public int StrokeWidth { get; set; }
31+
public double StrokeWidth { get; set; }
3232

3333
[Parameter]
3434
public int Height { get; set; } = 200;
@@ -57,14 +57,9 @@ protected BarcodeResult GetCode()
5757

5858
try
5959
{
60-
var width = Width;
61-
var height = Height;
62-
6360
var matrix = Encoder.encode(Value, BarcodeFormat, 0, 0);
6461

65-
var moduleSizeX = width / matrix.Width;
66-
var moduleSizeY = height / matrix.Height;
67-
var result = new BarcodeResult(matrix, moduleSizeX, moduleSizeY);
62+
var result = new BarcodeResult(matrix, 1, 1);
6863
ErrorText = null;
6964
return result;
7065
}

0 commit comments

Comments
 (0)