Skip to content

Commit 6b6272b

Browse files
authored
Auto-Analyze GS1-128 (#238)
1 parent 3482955 commit 6b6272b

File tree

2 files changed

+90
-10
lines changed

2 files changed

+90
-10
lines changed

src/BinaryKits.Zpl.Viewer/ElementDrawers/Barcode128ElementDrawer.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,12 @@ public override void Draw(ZplElementBase element, DrawerOptions options)
3434
}
3535
else if (barcode.Mode == "D")
3636
{
37-
codeSet = Code128CodeSet.Code128C;
37+
codeSet = Code128CodeSet.Code128;
3838
gs1 = true;
3939
}
4040
else if (barcode.Mode == "U")
4141
{
4242
codeSet = Code128CodeSet.Code128C;
43-
gs1 = true;
4443
content = content.PadLeft(19, '0').Substring(0, 19);
4544
int checksum = 0;
4645
for (int i = 0; i < 19; i++)

src/BinaryKits.Zpl.Viewer/Symologies/ZplCode128Symbology.cs

+89-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ public class ZplCode128Symbology
2222
private static readonly Regex startCRegex = new Regex(@"^\d{4}", RegexOptions.Compiled);
2323
private static readonly Regex digitPairRegex = new Regex(@"^\d\d", RegexOptions.Compiled);
2424

25+
// GS1 specific
26+
private static readonly Regex gs1SwitchCRegex = new Regex(@"^\d\d(?:\d\d|>8)+(?!\d)", RegexOptions.Compiled);
27+
private static readonly Regex gs1StartCRegex = new Regex(@"^(?:\d\d|>8){2}", RegexOptions.Compiled);
28+
private static readonly Regex gs1DigitPairRegex = new Regex(@"^(?:\d\d|>8)", RegexOptions.Compiled);
29+
private static readonly Regex fnc1Regex = new Regex(@"^>8", RegexOptions.Compiled);
30+
2531
private static readonly Regex startCodeRegex = new Regex(@"^(>[9:;])(.+)$", RegexOptions.Compiled);
2632

2733
private static readonly Dictionary<string, int> invocationMap = new Dictionary<string, int>() {
@@ -44,6 +50,12 @@ public class ZplCode128Symbology
4450
{ ">;", Code128CodeSet.Code128C }
4551
};
4652

53+
private static readonly Dictionary<Code128CodeSet, string> codeSetCodeMap = new Dictionary<Code128CodeSet, string>() {
54+
{ Code128CodeSet.Code128A, CODE_A },
55+
{ Code128CodeSet.Code128B, CODE_B },
56+
{ Code128CodeSet.Code128C, CODE_C }
57+
};
58+
4759
private const string FNC_1 = "FNC_1";
4860
private const string FNC_2 = "FNC_2";
4961
private const string FNC_3 = "FNC_3";
@@ -208,7 +220,20 @@ static ZplCode128Symbology() {
208220
public static (List<bool>, string) Encode(string content, Code128CodeSet initialCodeSet, bool gs1)
209221
{
210222
List<bool> result = new List<bool>();
211-
var (data, interpretation) = Analyze(content, initialCodeSet);
223+
List<int> data;
224+
string interpretation;
225+
if (gs1)
226+
{
227+
(data, interpretation) = AnalyzeGS1(content);
228+
}
229+
else if (initialCodeSet == Code128CodeSet.Code128)
230+
{
231+
(data, interpretation) = AnalyzeAuto(content);
232+
}
233+
else
234+
{
235+
(data, interpretation) = Analyze(content, initialCodeSet);
236+
}
212237

213238
// TODO: magic constant FNC_1
214239
if (gs1 && data[1] != 102)
@@ -265,11 +290,6 @@ private static (List<int>, string) Analyze(string content, Code128CodeSet initia
265290
startCodeMatch = startCodeRegex.Match(content);
266291
}
267292

268-
if (codeSet == Code128CodeSet.Code128)
269-
{
270-
return AnalyzeAuto(content);
271-
}
272-
273293
(var codeChars, var codeMap) = codeMaps[codeSet];
274294

275295
data.Add((int)codeSet);
@@ -303,8 +323,14 @@ private static (List<int>, string) Analyze(string content, Code128CodeSet initia
303323
}
304324
else if (startCodeMap.ContainsKey(symbol))
305325
{
306-
codeSet = startCodeMap[symbol];
307-
(codeChars, codeMap) = codeMaps[codeSet];
326+
Code128CodeSet newCodeSet = startCodeMap[symbol];
327+
if (newCodeSet != codeSet)
328+
{
329+
int value = codeMap[codeSetCodeMap[newCodeSet]];
330+
data.Add(value);
331+
codeSet = newCodeSet;
332+
(codeChars, codeMap) = codeMaps[codeSet];
333+
}
308334
}
309335
else
310336
{
@@ -322,6 +348,8 @@ private static (List<int>, string) Analyze(string content, Code128CodeSet initia
322348
}
323349
else
324350
{
351+
int value = codeMap[CODE_B];
352+
data.Add(value);
325353
codeSet = Code128CodeSet.Code128B;
326354
(codeChars, codeMap) = codeMaps[codeSet];
327355
}
@@ -387,5 +415,58 @@ private static (List<int>, string) AnalyzeAuto(string content)
387415
return (data, interpretation);
388416
}
389417

418+
private static (List<int>, string) AnalyzeGS1(string content)
419+
{
420+
List<int> data = new List<int>();
421+
string interpretation = "";
422+
Code128CodeSet codeSet = Code128CodeSet.Code128B;
423+
var codeMap = codeBMap;
424+
if (gs1StartCRegex.IsMatch(content))
425+
{
426+
codeSet = Code128CodeSet.Code128C;
427+
codeMap = codeCMap;
428+
}
429+
data.Add((int)codeSet);
430+
431+
while (content.Length > 0)
432+
{
433+
if (codeSet != Code128CodeSet.Code128C && gs1SwitchCRegex.IsMatch(content))
434+
{
435+
data.Add(codeMap[CODE_C]);
436+
codeSet = Code128CodeSet.Code128C;
437+
codeMap = codeCMap;
438+
}
439+
else if (codeSet == Code128CodeSet.Code128C && !gs1DigitPairRegex.IsMatch(content))
440+
{
441+
data.Add(codeMap[CODE_B]);
442+
codeSet = Code128CodeSet.Code128B;
443+
codeMap = codeBMap;
444+
}
445+
else if (fnc1Regex.IsMatch(content))
446+
{
447+
content = content.Substring(2);
448+
data.Add(codeMap[FNC_1]);
449+
}
450+
else
451+
{
452+
string symbol = content[0].ToString();
453+
if (codeSet == Code128CodeSet.Code128C)
454+
{
455+
symbol += content[1];
456+
content = content.Substring(2);
457+
}
458+
else
459+
{
460+
content = content.Substring(1);
461+
}
462+
463+
data.Add(codeMap[symbol]);
464+
interpretation += symbol;
465+
}
466+
}
467+
468+
return (data, interpretation);
469+
}
470+
390471
}
391472
}

0 commit comments

Comments
 (0)