diff options
Diffstat (limited to 'source/codes/Itf.mc')
| -rw-r--r-- | source/codes/Itf.mc | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/source/codes/Itf.mc b/source/codes/Itf.mc new file mode 100644 index 0000000..0d74c3b --- /dev/null +++ b/source/codes/Itf.mc @@ -0,0 +1,39 @@ +// Author: cirakg & claude ai +// Summary: Interleaved 2 of 5 (ITF) encoder: digit pairs, optional mod-10 check digit, wide bars 3 modules (as zxing). + +import Toybox.Lang; + +module Itf { + + // Five elements per digit, first element = bit 4, 1 = wide. + const PATTERNS = [0x06, 0x11, 0x09, 0x18, 0x05, 0x14, 0x0C, 0x03, 0x12, 0x0A]; + + function encode(text as String, withCheck as Boolean) as Codes.LinearCode or Codes.CodeError { + var values = Codes.digits(text); + if (values == null || values.size() == 0) { + return new Codes.CodeError("ITF takes digits only"); + } + if (withCheck) { + values.add(Ean.checkDigit(values)); + } + if (values.size() % 2 != 0) { + return new Codes.CodeError(withCheck + ? "ITF with a check digit needs an odd number of digits: add a leading 0" + : "ITF needs an even number of digits: add a leading 0"); + } + var m = []b; + Codes.addRuns(m, [1, 1, 1, 1], true); + for (var i = 0; i < values.size(); i += 2) { + var bars = PATTERNS[values[i]] as Number; + var spaces = PATTERNS[values[i + 1]] as Number; + var widths = [] as Array<Number>; + for (var k = 4; k >= 0; k--) { + widths.add(((bars >> k) & 1) == 1 ? 3 : 1); + widths.add(((spaces >> k) & 1) == 1 ? 3 : 1); + } + Codes.addRuns(m, widths, true); + } + Codes.addRuns(m, [3, 1, 1], true); + return new Codes.LinearCode(m, Codes.digitsText(values)); + } +} |
