Skip to content
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

Address project wide lint warnings, remove useless comments, remove broken reference #211

Merged
merged 1 commit into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
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
18 changes: 7 additions & 11 deletions BarcodeStandard/BarcodeCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace BarcodeStandard
internal abstract class BarcodeCommon
{
public string RawData { get; protected set; } = "";
public List<string> Errors { get; } = new List<string>();
public List<string> Errors { get; } = [];

protected void Error(string errorMessage)
{
Expand All @@ -27,16 +27,12 @@ internal static bool IsNumericOnly(string s)

internal static int GetAlignmentShiftAdjustment(Barcode barcode)
{
switch (barcode.Alignment)
return barcode.Alignment switch
{
case AlignmentPositions.Left:
return 0;
case AlignmentPositions.Right:
return (barcode.Width % barcode.EncodedValue.Length);
case AlignmentPositions.Center:
default:
return (barcode.Width % barcode.EncodedValue.Length) / 2;
}//switch
AlignmentPositions.Left => 0,
AlignmentPositions.Right => (barcode.Width % barcode.EncodedValue.Length),
_ => (barcode.Width % barcode.EncodedValue.Length) / 2,
};
}
}//BarcodeVariables abstract class
}//namespace
}
444 changes: 175 additions & 269 deletions BarcodeStandard/BarcodeLib.cs

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions BarcodeStandard/BarcodeStandard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>3.1.3</Version>
<Version>3.1.4</Version>
<PackageId>BarcodeLib</PackageId>
<Company>Pnuema Productions</Company>
<Product>BarcodeLib</Product>
Expand All @@ -18,8 +18,8 @@
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageIcon>upca.jpg</PackageIcon>
<PackageIconUrl />
<AssemblyVersion>3.1.3.0</AssemblyVersion>
<FileVersion>3.1.3.0</FileVersion>
<AssemblyVersion>3.1.4.0</AssemblyVersion>
<FileVersion>3.1.4.0</FileVersion>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<SignAssembly>true</SignAssembly>
Expand All @@ -28,14 +28,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="SkiaSharp" Version="2.88.6" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.6" />
<PackageReference Include="System.Resources.Extensions" Version="7.0.0" />
<PackageReference Include="System.Text.Json" Version="7.0.3" />
<PackageReference Include="SkiaSharp" Version="2.88.8" />
<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.8" />
<PackageReference Include="System.Resources.Extensions" Version="8.0.0" />
<PackageReference Include="System.Text.Json" Version="8.0.4" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion BarcodeStandard/IBarcode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ List<string> Errors
}//Errors

}//interface
}//namespace
}
114 changes: 54 additions & 60 deletions BarcodeStandard/Labels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,53 +26,51 @@ public static SKImage Label_ITF14(Barcode barcode, SKBitmap img)
var labelPadding = textBounds.Height / 2f;
var backY = img.Height - textBounds.Height - labelPadding * 2f;

using (var canvas = new SKCanvas(img))
using var canvas = new SKCanvas(img);
//draw bounding box side overdrawn by label
using (var pen = new SKPaint())
{
//draw bounding box side overdrawn by label
using (var pen = new SKPaint())
{
pen.FilterQuality = SKFilterQuality.High;
pen.IsAntialias = true;
pen.ColorF = barcode.ForeColor;
pen.StrokeWidth = (float)img.Height / 16;

canvas.DrawLine(new SKPoint(0, backY - pen.StrokeWidth / 2f),
new SKPoint(img.Width, backY - pen.StrokeWidth / 2f), pen); //bottom
}

//color a box at the bottom of the barcode to hold the string of data
using (var paint = new SKPaint(font))
{
paint.FilterQuality = SKFilterQuality.High;
paint.IsAntialias = true;
paint.ColorF = barcode.BackColor;
paint.Style = SKPaintStyle.Fill;

var rect = SKRect.Create(0, backY, img.Width, textBounds.Height + labelPadding * 2f);
canvas.DrawRect(rect, paint);
}

//draw datastring under the barcode image
foreBrush.FilterQuality = SKFilterQuality.High;
foreBrush.IsAntialias = true;
foreBrush.ColorF = barcode.ForeColor;
foreBrush.TextAlign = SKTextAlign.Center;

var labelX = img.Width / 2f;
var labelY = img.Height - textBounds.Height + labelPadding;

canvas.DrawText(str, labelX, labelY, foreBrush);

canvas.Save();
} //using
pen.FilterQuality = SKFilterQuality.High;
pen.IsAntialias = true;
pen.ColorF = barcode.ForeColor;
pen.StrokeWidth = (float)img.Height / 16;

canvas.DrawLine(new SKPoint(0, backY - pen.StrokeWidth / 2f),
new SKPoint(img.Width, backY - pen.StrokeWidth / 2f), pen); //bottom
}

//color a box at the bottom of the barcode to hold the string of data
using (var paint = new SKPaint(font))
{
paint.FilterQuality = SKFilterQuality.High;
paint.IsAntialias = true;
paint.ColorF = barcode.BackColor;
paint.Style = SKPaintStyle.Fill;

var rect = SKRect.Create(0, backY, img.Width, textBounds.Height + labelPadding * 2f);
canvas.DrawRect(rect, paint);
}

//draw datastring under the barcode image
foreBrush.FilterQuality = SKFilterQuality.High;
foreBrush.IsAntialias = true;
foreBrush.ColorF = barcode.ForeColor;
foreBrush.TextAlign = SKTextAlign.Center;

var labelX = img.Width / 2f;
var labelY = img.Height - textBounds.Height + labelPadding;

canvas.DrawText(str, labelX, labelY, foreBrush);

canvas.Save();
}

return SKImage.FromBitmap(img);
}//try
}
catch (Exception ex)
{
throw new Exception("ELABEL_ITF14-1: " + ex.Message);
}//catch
}
}

/// <summary>
Expand Down Expand Up @@ -128,11 +126,11 @@ public static SKImage Label_Generic(Barcode barcode, SKBitmap img)
g.Save();
}
return SKImage.FromBitmap(img);
}//try
}
catch (Exception ex)
{
throw new Exception("ELABEL_GENERIC-1: " + ex.Message);
}//catch
}
finally
{
foreBrush.Dispose();
Expand Down Expand Up @@ -220,11 +218,11 @@ public static SKImage Label_EAN13(Barcode barcode, SKBitmap img)
}

return SKImage.FromBitmap(img);
}//try
}
catch (Exception ex)
{
throw new Exception("ELABEL_EAN13-1: " + ex.Message);
}//catch
}
finally
{
foreBrush.Dispose();
Expand Down Expand Up @@ -316,19 +314,19 @@ public static SKImage Label_UPCA(Barcode barcode, SKBitmap img)
}

return SKImage.FromBitmap(img);
}//try
}
catch (Exception ex)
{
throw new Exception("ELABEL_UPCA-1: " + ex.Message);
}//catch
}
finally
{
foreBrush.Dispose();
backBrush.Dispose();
}
}//Label_UPCA

private static int GetFontSize(Barcode barcode, int wid, int hgt, string lbl)
/*private static int GetFontSize(int wid, int hgt, string lbl)
{
//Returns the optimal font size for the specified dimensions
var fontSize = 10;
Expand All @@ -338,23 +336,19 @@ private static int GetFontSize(Barcode barcode, int wid, int hgt, string lbl)
var bounds = SKRect.Empty;
for (var i = 1; i <= 100; i++)
{
using (var testFont = new SKFont(SKTypeface.FromFamilyName("Arial", SKFontStyle.Normal), i))
{
// Make a Graphics object to measure the text.
using (var gr = new SKPaint(testFont))
{
gr.MeasureText(lbl, ref bounds);

if (!(bounds.Width > wid) && !(bounds.Height > hgt)) continue;
fontSize = i - 1;
break;
}
}
using var testFont = new SKFont(SKTypeface.FromFamilyName("Arial", SKFontStyle.Normal), i);
// Make a Graphics object to measure the text.
using var gr = new SKPaint(testFont);
gr.MeasureText(lbl, ref bounds);

if (!(bounds.Width > wid) && !(bounds.Height > hgt)) continue;
fontSize = i - 1;
break;
}

};

return fontSize;
}
}*/
}
}
26 changes: 13 additions & 13 deletions BarcodeStandard/Symbologies/Codabar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ namespace BarcodeStandard.Symbologies
/// </summary>
internal class Codabar : BarcodeCommon, IBarcode
{
private readonly System.Collections.Hashtable Codabar_Code = new System.Collections.Hashtable(); //is initialized by init_Codabar()
private readonly System.Collections.Hashtable Codabar_Code = []; //is initialized by init_Codabar()

internal Codabar(string input)
{
RawData = input;
}//Codabar
}

/// <summary>
/// Encode the raw data using the Codabar algorithm.
Expand All @@ -29,7 +29,7 @@ private string Encode_Codabar()
case "D": break;
default: Error("ECODABAR-2: Data format invalid. (Invalid START character)");
break;
}//switch
}

//check the ending char to make sure its a start/stop char
switch (RawData[RawData.Trim().Length - 1].ToString().ToUpper().Trim())
Expand All @@ -40,10 +40,10 @@ private string Encode_Codabar()
case "D": break;
default: Error("ECODABAR-3: Data format invalid. (Invalid STOP character)");
break;
}//switch
}

//populate the hashtable to begin the process
init_Codabar();
Init_Codabar();

//replace non-numeric VALID chars with empty strings before checking for all numerics
var temp = RawData;
Expand All @@ -53,8 +53,8 @@ private string Encode_Codabar()
if (!IsNumericOnly(c.ToString()))
{
temp = temp.Replace(c, '1');
}//if
}//if
}
}

//now that all the valid non-numeric chars have been replaced with a number check if all numeric exist
if (!IsNumericOnly(temp))
Expand All @@ -66,7 +66,7 @@ private string Encode_Codabar()
{
result += Codabar_Code[c].ToString();
result += "0"; //inter-character space
}//foreach
}

//remove the extra 0 at the end of the result
result = result.Remove(result.Length - 1);
Expand All @@ -78,8 +78,8 @@ private string Encode_Codabar()
RawData = RawData.Trim().Substring(1, RawData.Trim().Length - 2);

return result;
}//Encode_Codabar
private void init_Codabar()
}
private void Init_Codabar()
{
Codabar_Code.Clear();
Codabar_Code.Add('0', "101010011");
Expand All @@ -106,13 +106,13 @@ private void init_Codabar()
Codabar_Code.Add('b', "1010010011");
Codabar_Code.Add('c', "1001001011");
Codabar_Code.Add('d', "1010011001");
}//init_Codeabar
}

#region IBarcode Members

public string Encoded_Value => Encode_Codabar();

#endregion

}//class
}//namespace
}
}
18 changes: 9 additions & 9 deletions BarcodeStandard/Symbologies/Code11.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ namespace BarcodeStandard.Symbologies
/// </summary>
internal class Code11 : BarcodeCommon, IBarcode
{
private readonly string[] C11_Code = { "101011", "1101011", "1001011", "1100101", "1011011", "1101101", "1001101", "1010011", "1101001", "110101", "101101", "1011001" };
private readonly string[] C11_Code = ["101011", "1101011", "1001011", "1100101", "1011011", "1101101", "1001101", "1010011", "1101001", "110101", "101101", "1011001"];

internal Code11(string input)
{
RawData = input;
}//Code11
}
/// <summary>
/// Encode the raw data using the Code 11 algorithm.
/// </summary>
Expand All @@ -37,7 +37,7 @@ private string Encode_Code11()
cTotal += Int32.Parse(RawData[i].ToString()) * weight++;
else
cTotal += 10 * weight++;
}//for
}
var checksumC = cTotal % 11;

dataToEncodeWithChecksums += checksumC.ToString();
Expand All @@ -58,10 +58,10 @@ private string Encode_Code11()
kTotal += Int32.Parse(dataToEncodeWithChecksums[i].ToString()) * weight++;
else
kTotal += 10 * weight++;
}//for
}
var checksumK = kTotal % 11;
dataToEncodeWithChecksums += checksumK.ToString();
}//if
}

//encode data
var space = "0";
Expand All @@ -74,18 +74,18 @@ private string Encode_Code11()

//inter-character space
result += space;
}//foreach
}

//stop bars
result += C11_Code[11];

return result;
}//Encode_Code11
}

#region IBarcode Members

public string Encoded_Value => Encode_Code11();

#endregion
}//class
}//namespace
}
}
Loading