Skip to content

Commit

Permalink
Add 1D barcode height and EAN font family for SVG renderer (#47)
Browse files Browse the repository at this point in the history
* add height & font family

* add error handle

* fix height

* Update default font

---------

Co-authored-by: Andrey <you@example.com>
  • Loading branch information
MrOzean and Andrey authored Aug 24, 2024
1 parent de228e0 commit 560044c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
39 changes: 27 additions & 12 deletions src/Barcoder.Renderer.Svg/SvgRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public sealed class SvgRenderer : IRenderer
private static readonly int[] Ean13LongerBars = new[] { 0, 2, 46, 48, 92, 94 };

private readonly SvgRendererOptions _options;

public SvgRenderer(SvgRendererOptions options = null)
{
_options = options ?? new SvgRendererOptions();
Expand All @@ -21,6 +21,21 @@ public SvgRenderer(SvgRendererOptions options = null)
private bool IncludeEanContent(IBarcode barcode)
=> _options.IncludeEanContentAsText && (barcode.Metadata.CodeKind == BarcodeType.EAN13 || barcode.Metadata.CodeKind == BarcodeType.EAN8);

private int Get1DBarcodeHeight(IBarcode barcode)
{
var height = _options.BarHeightFor1DBarcode;
if (height <= 0)
throw new ArgumentOutOfRangeException(nameof(_options.BarHeightFor1DBarcode), "Value must be larger than zero");


if (IncludeEanContent(barcode))
{
height += 5; // add extra height for text, 2px margin + 3px text height
}

return height;
}

public void Render(IBarcode barcode, Stream outputStream)
{
barcode = barcode ?? throw new ArgumentNullException(nameof(barcode));
Expand All @@ -36,9 +51,9 @@ public void Render(IBarcode barcode, Stream outputStream)
private void Render1D(IBarcode barcode, Stream outputStream)
{
var document = SvgDocument.Create();
int height = IncludeEanContent(barcode) ? 55 : 50;
int height = Get1DBarcodeHeight(barcode);
int margin = _options.CustomMargin ?? barcode.Margin;

document.ViewBox = new SvgViewBox
{
Left = 0,
Expand Down Expand Up @@ -68,14 +83,14 @@ private void Render1D(IBarcode barcode, Stream outputStream)
{
if (!Ean13LongerBars.Contains(x))
{
lineHeight = 48;
lineHeight -= 7; // strip line height at extra height 5px + margin 2px
}
}
else
{
if (!Ean8LongerBars.Contains(x))
{
lineHeight = 48;
lineHeight -= 7; // strip line height at extra height 5px + margin 2px
}
}
}
Expand Down Expand Up @@ -103,14 +118,14 @@ private void Render1D(IBarcode barcode, Stream outputStream)
{
if (barcode.Metadata.CodeKind == BarcodeType.EAN13)
{
AddText(document, 4, 54.5D, barcode.Content.Substring(0, 1));
AddText(document, 21, 54.5D, barcode.Content.Substring(1, 6));
AddText(document, 67, 54.5D, barcode.Content.Substring(7));
AddText(document, 4, height - 0.5D, barcode.Content.Substring(0, 1));
AddText(document, 21, height - 0.5D, barcode.Content.Substring(1, 6));
AddText(document, 67, height - 0.5D, barcode.Content.Substring(7));
}
else
{
AddText(document, 18, 54.5D, barcode.Content.Substring(0, 4));
AddText(document, 50, 54.5D, barcode.Content.Substring(4));
AddText(document, 18, height - 0.5D, barcode.Content.Substring(0, 4));
AddText(document, 50, height - 0.5D, barcode.Content.Substring(4));
}
}

Expand All @@ -120,7 +135,7 @@ private void Render1D(IBarcode barcode, Stream outputStream)
private void AddText(SvgDocument doc, double x, double y, string t)
{
SvgText text = doc.AddText();
text.FontFamily = "arial";
text.FontFamily = _options.EanFontFamily ?? throw new ArgumentNullException(nameof(_options.EanFontFamily));
text.Text = t;
text.X = x;
text.Y = y;
Expand All @@ -132,7 +147,7 @@ private void AddText(SvgDocument doc, double x, double y, string t)
private void Render2D(IBarcode barcode, Stream outputStream)
{
int margin = _options.CustomMargin ?? barcode.Margin;

var document = SvgDocument.Create();
document.ViewBox = new SvgViewBox
{
Expand Down
6 changes: 5 additions & 1 deletion src/Barcoder.Renderer.Svg/SvgRendererOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
public sealed class SvgRendererOptions
{
public bool IncludeEanContentAsText { get; set; } = false;

public int? CustomMargin { get; set; } = null;

public int BarHeightFor1DBarcode { get; set; } = 50;

public string EanFontFamily { get; set; } = "arial";
}
}

0 comments on commit 560044c

Please sign in to comment.