From 776a563b97b99b054323dc06f02b4d374805777a Mon Sep 17 00:00:00 2001 From: cirakg Date: Tue, 20 Oct 2026 02:43:08 +0200 Subject: 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 --- source/codes/Itf.mc | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 source/codes/Itf.mc (limited to 'source/codes/Itf.mc') 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; + 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)); + } +} -- cgit v1.2.3