diff options
| author | cirakg <cirakg@cirakg.xyz> | 2026-10-20 02:43:08 +0200 |
|---|---|---|
| committer | cirakg <cirakg@cirakg.xyz> | 2026-10-20 02:43:08 +0200 |
| commit | 776a563b97b99b054323dc06f02b4d374805777a (patch) | |
| tree | 1d37e37cb401ceeb5a519ee36c036dab118f59e2 /source/codes/Itf.mc | |
| parent | 0edb4bb6d76939aa456cbbc8deafeed50fd6182d (diff) | |
feat(codes): add EAN/UPC, Code 128, Code 39, ITF and Codabar encoders
Check digits added, verified, fixed or off; canonical UPC-E zero
suppression; Code 128 sets B and C with zxing's switching rules; module
rows match zxing-cpp (Code 128 by length, the tie-breaking differs).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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)); + } +} |
