Skip to content

Commit

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

* add height & font family

* add error handle

* fix height

* Update default font

---------

Co-authored-by: Andrey <[email protected]>

* Add new GS1 Tags, fix bugs (#42)

7040 missing symbol ^;
7240 missing symbol ^;
235 change tag from 243, set Fnc1Required to true;
Update tags RegularExpression;
New tags:
3100 NET WEIGHT (kg)
3950-3959 PRICE/UoM
4300 SHIP TO COMP
4301 SHIP TO NAME
4302 SHIP TO ADD1
4303 SHIP TO ADD2
4304 SHIP TO SUB
4305 SHIP TO LOC
4306 SHIP TO REG
4307 SHIP TO COUNTRY          
4308 SHIP TO PHONE            
4309 SHIP TO GEO
4310 RTN TO COMP
4311 RTN TO NAME
4312 RTN TO ADD1
4313 RTN TO ADD2
4314 RTN TO SUB
4315 RTN TO LOC
4316 RTN TO REG
4317 RTN TO COUNTRY
4318 RTN TO POST
4319 RTN TO PHONE
4320 SRV DESCRIPTION
4321 DANGEROUS GOODS
4322 AUTH TO LEAVE
4323 SIG REQUIRED
4324 NBEF DEL DT
4325 NAFT DEL DT
4326 REL DATE
7011 TEST BY DATE
715 NHRN NDC

Data obtained from the site: https://www.gs1.org/standards/barcodes/application-identifiers/

---------

Co-authored-by: MrOzean <[email protected]>
Co-authored-by: Andrey <[email protected]>
Co-authored-by: LaMerXaKer <[email protected]>
  • Loading branch information
4 people authored Aug 24, 2024
1 parent ce4abc8 commit 4787bfd
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 87 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";
}
}
Loading

0 comments on commit 4787bfd

Please sign in to comment.