diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..b7c430a --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,19 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Run-Tests", + "type": "go", + "request": "launch", + "mode": "debug", + "remotePath": "", + "port": 2345, + "host": "127.0.0.1", + //"program": "${workspaceRoot}", + "program": "/Users/boombuler/Sources/barcode", + "env": {}, + "args": [], + "showLog": true + } + ] +} \ No newline at end of file diff --git a/pdf417/data_encoder.go b/pdf417/data_encoder.go deleted file mode 100644 index 525a4e3..0000000 --- a/pdf417/data_encoder.go +++ /dev/null @@ -1,122 +0,0 @@ -package pdf417 - -import ( - "fmt" -) - -const ( - switchCodeText = 900 - switchCodeNumber = 902 -) - -type encoder interface { - CanEncode(char rune) bool - GetSwitchCode(data string) int - Encode(data string) ([]int, error) -} - -type dataEncoder struct { - Encoders []encoder - DefaultEncoder encoder -} - -type chain struct { - Data string - Encoder encoder -} - -func (c chain) Encode() ([]int, error) { - return c.Encoder.Encode(c.Data) -} - -func (c chain) GetSwitchCode() int { - return c.Encoder.GetSwitchCode(c.Data) -} - -func newDataEncoder() *dataEncoder { - textEncoder := newTextEncoder() - - encoder := &dataEncoder{ - []encoder{ - newNumberEncoder(), - textEncoder, - }, - textEncoder, - } - - return encoder -} - -func (dataEncoder *dataEncoder) Encode(data string) ([]int, error) { - if len(data) == 0 { - return nil, fmt.Errorf("Nothing to encode") - } - - chains, err := dataEncoder.SplitToChains(data) - if err != nil { - return nil, err - } - - if len(chains) == 0 { - return nil, fmt.Errorf("%q can not be encoded!", data) - } - - currentSwitchCode := switchCodeText - - cws := []int{} - - for _, chain := range chains { - encoded, err := chain.Encode() - if err != nil { - return cws, err - } - - if switchCode := chain.GetSwitchCode(); currentSwitchCode != switchCode { - cws = append(cws, switchCode) - currentSwitchCode = switchCode - } - - cws = append(cws, encoded...) - } - - return cws, nil -} - -func (dataEncoder *dataEncoder) SplitToChains(data string) ([]chain, error) { - chains := []chain{} - chainData := "" - encoder := dataEncoder.DefaultEncoder - - for _, char := range data { - newEncoder, err := dataEncoder.getEncoder(char) - if err != nil { - return nil, err - } - - if newEncoder != encoder { - if len(chainData) > 0 { - chains = append(chains, chain{chainData, encoder}) - chainData = "" - } - - encoder = newEncoder - } - - chainData = chainData + string(char) - } - - if len(chainData) > 0 { - chains = append(chains, chain{chainData, encoder}) - } - - return chains, nil -} - -func (dataEncoder *dataEncoder) getEncoder(char rune) (encoder, error) { - for _, encoder := range dataEncoder.Encoders { - if encoder.CanEncode(char) { - return encoder, nil - } - } - return nil, fmt.Errorf("Cannot encode character %q", string(char)) -} diff --git a/pdf417/data_encoder_test.go b/pdf417/data_encoder_test.go deleted file mode 100644 index f83fd1c..0000000 --- a/pdf417/data_encoder_test.go +++ /dev/null @@ -1,36 +0,0 @@ -package pdf417 - -import "testing" - -func TestEncode(t *testing.T) { - encoder := newDataEncoder() - - // When starting with text, the first code word does not need to be the switch - if result, err := encoder.Encode("ABC123"); err != nil { - t.Error(err) - } else { - compareIntSlice(t, []int{1, 89, 902, 1, 223}, result) - } - // When starting with numbers, we do need to switchresult := encoder.Encode("ABC123") - if result, err := encoder.Encode("123ABC"); err != nil { - t.Error(err) - } else { - compareIntSlice(t, []int{902, 1, 223, 900, 1, 89}, result) - } - - /* - // Also with bytes - if result, err := encoder.Encode("\x0B"); err != nil { - t.Error(err) - } else { - compareIntSlice(t, []int{901, 11}, result) - } - - // Alternate bytes switch code when number of bytes is divisble by 6 - if result, err := encoder.Encode("\x0B\x0B\x0B\x0B\x0B\x0B"); err != nil { - t.Error(err) - } else { - compareIntSlice(t, []int{924, 18, 455, 694, 754, 291}, result) - } - */ -} diff --git a/pdf417/encoder.go b/pdf417/encoder.go index 482e4b6..1bc495e 100644 --- a/pdf417/encoder.go +++ b/pdf417/encoder.go @@ -20,8 +20,8 @@ func Encode(data string, securityLevel byte) (barcode.Barcode, error) { sl := securitylevel(securityLevel) - dataEncoder := newDataEncoder() - dataWords, err := dataEncoder.Encode(data) + //dataEncoder := newDataEncoder() + dataWords, err := highlevelEncode(data) if err != nil { return nil, err } diff --git a/pdf417/errorcorrection_test.go b/pdf417/errorcorrection_test.go index 99b29d4..55c0d59 100644 --- a/pdf417/errorcorrection_test.go +++ b/pdf417/errorcorrection_test.go @@ -9,53 +9,53 @@ var inputData = []int{16, 902, 1, 278, 827, 900, 295, 902, 2, 326, 823, 544, 900 func TestReedSolomonComputeLevel0(t *testing.T) { var level securitylevel = 0 expected := []int{156, 765} - compareIntSlice(t, expected, level.Compute(inputData)) + compareIntSlice(t, expected, level.Compute(inputData), "SecLvl 0") } func TestReedSolomonComputeLevel1(t *testing.T) { var level securitylevel = 1 expected := []int{168, 875, 63, 355} - compareIntSlice(t, expected, level.Compute(inputData)) + compareIntSlice(t, expected, level.Compute(inputData), "SecLvl 1") } func TestReedSolomonComputeLevel2(t *testing.T) { var level securitylevel = 2 expected := []int{628, 715, 393, 299, 863, 601, 169, 708} - compareIntSlice(t, expected, level.Compute(inputData)) + compareIntSlice(t, expected, level.Compute(inputData), "SecLvl 2") } func TestReedSolomonComputeLevel3(t *testing.T) { var level securitylevel = 3 expected := []int{232, 176, 793, 616, 476, 406, 855, 445, 84, 518, 522, 721, 607, 2, 42, 578} - compareIntSlice(t, expected, level.Compute(inputData)) + compareIntSlice(t, expected, level.Compute(inputData), "SecLvl 3") } func TestReedSolomonComputeLevel4(t *testing.T) { var level securitylevel = 4 expected := []int{281, 156, 276, 668, 44, 252, 877, 30, 549, 856, 773, 639, 420, 330, 693, 329, 283, 723, 480, 482, 102, 925, 535, 892, 374, 472, 837, 331, 343, 608, 390, 364} - compareIntSlice(t, expected, level.Compute(inputData)) + compareIntSlice(t, expected, level.Compute(inputData), "SecLvl 4") } func TestReedSolomonComputeLevel5(t *testing.T) { var level securitylevel = 5 expected := []int{31, 850, 18, 870, 53, 477, 837, 130, 533, 186, 266, 450, 39, 492, 542, 653, 499, 887, 618, 103, 364, 313, 906, 396, 270, 735, 593, 81, 557, 712, 810, 48, 167, 533, 205, 577, 503, 126, 449, 189, 859, 471, 493, 849, 554, 76, 878, 893, 168, 497, 251, 704, 311, 650, 283, 268, 462, 223, 659, 763, 176, 34, 544, 304} - compareIntSlice(t, expected, level.Compute(inputData)) + compareIntSlice(t, expected, level.Compute(inputData), "SecLvl 5") } func TestReedSolomonComputeLevel6(t *testing.T) { var level securitylevel = 6 expected := []int{345, 775, 909, 489, 650, 568, 869, 577, 574, 349, 885, 317, 492, 222, 783, 451, 647, 385, 168, 366, 118, 655, 643, 551, 179, 880, 880, 752, 132, 206, 765, 862, 727, 240, 32, 266, 911, 287, 813, 437, 868, 201, 681, 867, 567, 398, 508, 564, 504, 676, 785, 554, 831, 566, 424, 93, 515, 275, 61, 544, 272, 621, 374, 922, 779, 663, 789, 295, 631, 536, 755, 465, 485, 416, 76, 412, 76, 431, 28, 614, 767, 419, 600, 779, 94, 584, 647, 846, 121, 97, 790, 205, 424, 793, 263, 271, 694, 522, 437, 817, 382, 164, 113, 849, 178, 602, 554, 261, 415, 737, 401, 675, 203, 271, 649, 120, 765, 209, 522, 687, 420, 32, 60, 266, 270, 228, 304, 270} - compareIntSlice(t, expected, level.Compute(inputData)) + compareIntSlice(t, expected, level.Compute(inputData), "SecLvl 6") } func TestReedSolomonComputeLevel7(t *testing.T) { var level securitylevel = 7 expected := []int{142, 203, 799, 4, 105, 137, 793, 914, 225, 636, 60, 171, 490, 180, 414, 141, 399, 599, 829, 288, 108, 268, 444, 481, 795, 146, 655, 778, 189, 32, 597, 206, 208, 711, 845, 608, 642, 636, 540, 795, 845, 466, 492, 659, 138, 800, 912, 171, 92, 438, 225, 301, 777, 449, 230, 448, 326, 182, 892, 681, 543, 582, 732, 758, 162, 587, 685, 378, 646, 356, 354, 25, 839, 839, 556, 253, 501, 771, 745, 616, 473, 293, 669, 822, 613, 684, 229, 265, 110, 438, 144, 727, 317, 605, 414, 497, 82, 278, 267, 323, 43, 894, 624, 282, 790, 579, 430, 255, 802, 553, 922, 604, 68, 692, 809, 909, 663, 589, 735, 670, 298, 158, 201, 68, 124, 64, 67, 338, 694, 373, 225, 579, 309, 699, 920, 432, 717, 72, 126, 819, 142, 755, 473, 630, 331, 758, 730, 65, 359, 451, 236, 16, 56, 31, 87, 587, 125, 385, 384, 197, 352, 383, 173, 271, 38, 558, 810, 260, 521, 680, 7, 319, 650, 334, 695, 708, 0, 562, 365, 204, 114, 185, 560, 746, 767, 449, 797, 688, 63, 135, 818, 805, 3, 536, 908, 532, 400, 698, 49, 212, 630, 93, 157, 275, 3, 20, 611, 179, 302, 282, 876, 665, 241, 206, 474, 80, 217, 460, 462, 751, 719, 571, 536, 794, 522, 385, 598, 756, 162, 212, 758, 662, 361, 223, 587, 857, 503, 382, 615, 86, 283, 541, 847, 518, 406, 736, 486, 408, 226, 342, 784, 772, 211, 888, 234, 335} - compareIntSlice(t, expected, level.Compute(inputData)) + compareIntSlice(t, expected, level.Compute(inputData), "SecLvl 7") } func TestReedSolomonComputeLevel8(t *testing.T) { var level securitylevel = 8 expected := []int{538, 446, 840, 510, 163, 708, 177, 666, 423, 600, 707, 913, 770, 571, 156, 683, 676, 697, 898, 776, 128, 851, 163, 854, 135, 661, 880, 279, 92, 324, 397, 207, 379, 223, 574, 9, 70, 858, 878, 579, 61, 551, 261, 388, 315, 856, 266, 865, 923, 38, 313, 62, 381, 198, 265, 256, 385, 878, 347, 532, 821, 53, 855, 225, 697, 826, 263, 334, 207, 565, 460, 496, 705, 599, 383, 289, 178, 168, 401, 268, 555, 190, 922, 284, 180, 810, 891, 832, 636, 813, 894, 495, 701, 484, 204, 793, 129, 164, 444, 228, 636, 98, 809, 57, 736, 697, 727, 534, 889, 480, 898, 773, 234, 851, 880, 843, 714, 443, 412, 489, 578, 468, 367, 663, 11, 686, 319, 352, 345, 670, 106, 106, 219, 466, 439, 350, 538, 66, 852, 175, 465, 731, 332, 110, 926, 491, 18, 422, 736, 797, 624, 376, 728, 526, 735, 200, 502, 923, 789, 529, 923, 706, 384, 869, 172, 548, 520, 463, 813, 384, 793, 231, 190, 653, 864, 351, 400, 525, 487, 828, 654, 307, 141, 638, 770, 775, 282, 54, 758, 197, 492, 320, 86, 790, 275, 237, 923, 25, 591, 605, 61, 824, 79, 631, 532, 337, 867, 423, 340, 597, 682, 923, 287, 408, 503, 361, 881, 196, 468, 759, 746, 389, 124, 784, 198, 865, 538, 451, 178, 772, 653, 121, 497, 598, 711, 716, 241, 159, 429, 88, 799, 761, 639, 105, 54, 807, 351, 435, 793, 873, 360, 8, 881, 479, 693, 576, 849, 875, 771, 621, 134, 863, 8, 171, 799, 924, 103, 63, 491, 538, 597, 855, 697, 499, 7, 886, 286, 85, 107, 220, 319, 124, 197, 150, 729, 899, 585, 540, 676, 414, 256, 856, 596, 259, 882, 436, 26, 273, 753, 127, 679, 390, 654, 42, 276, 420, 247, 629, 116, 803, 131, 25, 403, 645, 462, 897, 151, 622, 108, 167, 227, 831, 887, 662, 739, 263, 829, 56, 624, 317, 908, 378, 39, 393, 861, 338, 202, 179, 907, 109, 360, 736, 554, 342, 594, 125, 433, 394, 195, 698, 844, 912, 530, 842, 337, 294, 528, 231, 735, 93, 8, 579, 42, 148, 609, 233, 782, 887, 888, 915, 620, 78, 137, 161, 282, 217, 775, 564, 33, 195, 36, 584, 679, 775, 476, 309, 230, 303, 708, 143, 679, 502, 814, 193, 508, 532, 542, 580, 603, 641, 338, 361, 542, 537, 810, 394, 764, 136, 167, 611, 881, 775, 267, 433, 142, 202, 828, 363, 101, 728, 660, 583, 483, 786, 717, 190, 809, 422, 567, 741, 695, 310, 120, 177, 47, 494, 345, 508, 16, 639, 402, 625, 286, 298, 358, 54, 705, 916, 291, 424, 375, 883, 655, 675, 498, 498, 884, 862, 365, 310, 805, 763, 855, 354, 777, 543, 53, 773, 120, 408, 234, 728, 438, 914, 3, 670, 546, 465, 449, 923, 51, 546, 709, 648, 96, 320, 682, 326, 848, 234, 855, 791, 20, 97, 901, 351, 317, 764, 767, 312, 206, 139, 610, 578, 646, 264, 389, 238, 675, 595, 430, 88} - compareIntSlice(t, expected, level.Compute(inputData)) + compareIntSlice(t, expected, level.Compute(inputData), "SecLvl 8") } diff --git a/pdf417/highlevel.go b/pdf417/highlevel.go new file mode 100644 index 0000000..e41f1bc --- /dev/null +++ b/pdf417/highlevel.go @@ -0,0 +1,354 @@ +package pdf417 + +import ( + "errors" + "math/big" + + "github.com/boombuler/barcode/utils" +) + +type encodingMode byte + +type subMode byte + +const ( + encText encodingMode = iota + encNumeric + encBinary + + subUpper subMode = iota + subLower + subMixed + subPunct + + latch_to_text = 900 + latch_to_byte_padded = 901 + latch_to_numeric = 902 + latch_to_byte = 924 + shift_to_byte = 913 + + min_numeric_count = 13 +) + +var ( + mixedMap map[rune]int + punctMap map[rune]int +) + +func init() { + mixedMap = make(map[rune]int) + mixedRaw := []rune{ + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 38, 13, 9, 44, 58, + 35, 45, 46, 36, 47, 43, 37, 42, 61, 94, 0, 32, 0, 0, 0, + } + for idx, ch := range mixedRaw { + if ch > 0 { + mixedMap[ch] = idx + } + } + + punctMap = make(map[rune]int) + punctRaw := []rune{ + 59, 60, 62, 64, 91, 92, 93, 95, 96, 126, 33, 13, 9, 44, 58, + 10, 45, 46, 36, 47, 34, 124, 42, 40, 41, 63, 123, 125, 39, 0, + } + for idx, ch := range punctRaw { + if ch > 0 { + punctMap[ch] = idx + } + } +} + +func determineConsecutiveDigitCount(data []rune) int { + cnt := 0 + for _, r := range data { + if utils.RuneToInt(r) == -1 { + break + } + cnt++ + } + return cnt +} + +func encodeNumeric(digits []rune) ([]int, error) { + digitCount := len(digits) + chunkCount := digitCount / 44 + if digitCount%44 != 0 { + chunkCount++ + } + + codeWords := []int{} + + for i := 0; i < chunkCount; i++ { + start := i * 44 + end := start + 44 + if end > digitCount { + end = digitCount + } + chunk := digits[start:end] + + chunkNum := big.NewInt(0) + _, ok := chunkNum.SetString("1"+string(chunk), 10) + + if !ok { + return nil, errors.New("Failed converting: " + string(chunk)) + } + + cws := []int{} + + for chunkNum.Cmp(big.NewInt(0)) > 0 { + newChunk, cw := chunkNum.DivMod(chunkNum, big.NewInt(900), big.NewInt(0)) + chunkNum = newChunk + cws = append([]int{int(cw.Int64())}, cws...) + } + + codeWords = append(codeWords, cws...) + } + + return codeWords, nil +} + +func determineConsecutiveTextCount(msg []rune) int { + result := 0 + + isText := func(ch rune) bool { + return ch == '\t' || ch == '\n' || ch == '\r' || (ch >= 32 && ch <= 126) + } + + for i, ch := range msg { + numericCount := determineConsecutiveDigitCount(msg[i:]) + if numericCount >= min_numeric_count || (numericCount == 0 && !isText(ch)) { + break + } + + result++ + } + return result +} + +func encodeText(text []rune, submode subMode) (subMode, []int) { + isAlphaUpper := func(ch rune) bool { + return ch == ' ' || (ch >= 'A' && ch <= 'Z') + } + isAlphaLower := func(ch rune) bool { + return ch == ' ' || (ch >= 'a' && ch <= 'z') + } + isMixed := func(ch rune) bool { + _, ok := mixedMap[ch] + return ok + } + isPunctuation := func(ch rune) bool { + _, ok := punctMap[ch] + return ok + } + + idx := 0 + var tmp []int + for idx < len(text) { + ch := text[idx] + switch submode { + case subUpper: + if isAlphaUpper(ch) { + if ch == ' ' { + tmp = append(tmp, 26) //space + } else { + tmp = append(tmp, int(ch-'A')) + } + } else { + if isAlphaLower(ch) { + submode = subLower + tmp = append(tmp, 27) // lower latch + continue + } else if isMixed(ch) { + submode = subMixed + tmp = append(tmp, 28) // mixed latch + continue + } else { + tmp = append(tmp, 29) // punctuation switch + tmp = append(tmp, punctMap[ch]) + break + } + } + break + case subLower: + if isAlphaLower(ch) { + if ch == ' ' { + tmp = append(tmp, 26) //space + } else { + tmp = append(tmp, int(ch-'a')) + } + } else { + if isAlphaUpper(ch) { + tmp = append(tmp, 27) //upper switch + tmp = append(tmp, int(ch-'A')) + break + } else if isMixed(ch) { + submode = subMixed + tmp = append(tmp, 28) //mixed latch + continue + } else { + tmp = append(tmp, 29) //punctuation switch + tmp = append(tmp, punctMap[ch]) + break + } + } + break + case subMixed: + if isMixed(ch) { + tmp = append(tmp, mixedMap[ch]) + } else { + if isAlphaUpper(ch) { + submode = subUpper + tmp = append(tmp, 28) //upper latch + continue + } else if isAlphaLower(ch) { + submode = subLower + tmp = append(tmp, 27) //lower latch + continue + } else { + if idx+1 < len(text) { + next := text[idx+1] + if isPunctuation(next) { + submode = subPunct + tmp = append(tmp, 25) //punctuation latch + continue + } + } + tmp = append(tmp, 29) //punctuation switch + tmp = append(tmp, punctMap[ch]) + } + } + break + default: //subPunct + if isPunctuation(ch) { + tmp = append(tmp, punctMap[ch]) + } else { + submode = subUpper + tmp = append(tmp, 29) //upper latch + continue + } + } + idx++ + } + + h := 0 + result := []int{} + for i, val := range tmp { + if i%2 != 0 { + h = (h * 30) + val + result = append(result, h) + } else { + h = val + } + } + if len(tmp)%2 != 0 { + result = append(result, (h*30)+29) + } + return submode, result +} + +func determineConsecutiveBinaryCount(msg []byte) int { + result := 0 + + for i, _ := range msg { + numericCount := determineConsecutiveDigitCount([]rune(string(msg[i:]))) + if numericCount >= min_numeric_count { + break + } + textCount := determineConsecutiveTextCount([]rune(string(msg[i:]))) + if textCount > 5 { + break + } + result++ + } + return result +} + +func encodeBinary(data []byte, startmode encodingMode) []int { + result := []int{} + + count := len(data) + if count == 1 && startmode == encText { + result = append(result, shift_to_byte) + } else if (count % 6) == 0 { + result = append(result, latch_to_byte) + } else { + result = append(result, latch_to_byte_padded) + } + + idx := 0 + // Encode sixpacks + if count >= 6 { + words := make([]int, 5) + for (count - idx) >= 6 { + var t int64 = 0 + for i := 0; i < 6; i++ { + t = t << 8 + t += int64(data[idx+i]) + } + for i := 0; i < 5; i++ { + words[4-i] = int(t % 900) + t = t / 900 + } + result = append(result, words...) + idx += 6 + } + } + //Encode rest (remaining n<5 bytes if any) + for i := idx; i < count; i++ { + result = append(result, int(data[i]&0xff)) + } + return result +} + +func highlevelEncode(dataStr string) ([]int, error) { + encodingMode := encText + textSubMode := subUpper + + result := []int{} + + data := []byte(dataStr) + + for len(data) > 0 { + numericCount := determineConsecutiveDigitCount([]rune(string(data))) + if numericCount >= min_numeric_count || numericCount == len(data) { + result = append(result, latch_to_numeric) + encodingMode = encNumeric + textSubMode = subUpper + numData, err := encodeNumeric([]rune(string(data[:numericCount]))) + if err != nil { + return nil, err + } + result = append(result, numData...) + data = data[numericCount:] + } else { + textCount := determineConsecutiveTextCount([]rune(string(data))) + if textCount >= 5 || textCount == len(data) { + if encodingMode != encText { + result = append(result, latch_to_text) + encodingMode = encText + textSubMode = subUpper + } + var txtData []int + textSubMode, txtData = encodeText([]rune(string(data[:textCount])), textSubMode) + result = append(result, txtData...) + data = data[textCount:] + } else { + binaryCount := determineConsecutiveBinaryCount(data) + if binaryCount == 0 { + binaryCount = 1 + } + bytes := data[:binaryCount] + if len(bytes) != 1 || encodingMode != encText { + encodingMode = encBinary + textSubMode = subUpper + } + byteData := encodeBinary(bytes, encodingMode) + result = append(result, byteData...) + data = data[binaryCount:] + } + } + } + + return result, nil +} diff --git a/pdf417/highlevel_test.go b/pdf417/highlevel_test.go new file mode 100644 index 0000000..366aa5d --- /dev/null +++ b/pdf417/highlevel_test.go @@ -0,0 +1,34 @@ +package pdf417 + +import "testing" + +func compareIntSlice(t *testing.T, expected, actual []int, testStr string) { + if len(actual) != len(expected) { + t.Errorf("Invalid slice size. Expected %d got %d while encoding %q", len(expected), len(actual), testStr) + return + } + for i, a := range actual { + if e := expected[i]; e != a { + t.Errorf("Unexpected value at position %d. Expected %d got %d while encoding %q", i, e, a, testStr) + } + } +} + +func TestHighlevelEncode(t *testing.T) { + runTest := func(msg string, expected []int) { + if codes, err := highlevelEncode(msg); err != nil { + t.Error(err) + } else { + compareIntSlice(t, expected, codes, msg) + } + } + + runTest("01234", []int{902, 112, 434}) + runTest("Super !", []int{567, 615, 137, 809, 329}) + runTest("Super ", []int{567, 615, 137, 809}) + runTest("ABC123", []int{1, 88, 32, 119}) + runTest("123ABC", []int{841, 63, 840, 32}) + + runTest("\x0B", []int{913, 11}) + runTest("\x0B\x0B\x0B\x0B\x0B\x0B", []int{924, 18, 455, 694, 754, 291}) +} diff --git a/pdf417/number_encoder.go b/pdf417/number_encoder.go deleted file mode 100644 index 523d9af..0000000 --- a/pdf417/number_encoder.go +++ /dev/null @@ -1,76 +0,0 @@ -package pdf417 - -import ( - "errors" - "math/big" - - "github.com/boombuler/barcode/utils" -) - -type numberEncoder struct{} - -func newNumberEncoder() *numberEncoder { - return new(numberEncoder) -} - -func (encoder numberEncoder) CanEncode(char rune) bool { - return utils.RuneToInt(char) != -1 -} - -func (encoder numberEncoder) GetSwitchCode(data string) int { - return switchCodeNumber -} - -func (encoder numberEncoder) Encode(digits string) ([]int, error) { - digitCount := len(digits) - chunkCount := digitCount / 44 - if digitCount%44 != 0 { - chunkCount++ - } - - codeWords := []int{} - - for i := 0; i < chunkCount; i++ { - start := i * 44 - end := start + 44 - if end > digitCount { - end = digitCount - } - chunk := digits[start:end] - - cws, err := encodeChunk(chunk) - if err != nil { - return codeWords, err - } - - codeWords = append(codeWords, cws...) - } - - return codeWords, nil -} - -func encodeChunk(chunkInput string) ([]int, error) { - chunk := big.NewInt(0) - - _, ok := chunk.SetString("1"+chunkInput, 10) - - if !ok { - return nil, errors.New("Failed converting") - } - - cws := []int{} - - for chunk.Cmp(big.NewInt(0)) > 0 { - newChunk, cw := chunk.DivMod( - chunk, - big.NewInt(900), - big.NewInt(0), - ) - - chunk = newChunk - - cws = append([]int{int(cw.Int64())}, cws...) - } - - return cws, nil -} diff --git a/pdf417/number_encoder_test.go b/pdf417/number_encoder_test.go deleted file mode 100644 index 7dd418e..0000000 --- a/pdf417/number_encoder_test.go +++ /dev/null @@ -1,47 +0,0 @@ -package pdf417 - -import ( - "testing" -) - -func TestNumberEncoder_CanEncode(t *testing.T) { - encoder := newNumberEncoder() - - shouldEncode := func(tests ...rune) { - for _, test := range tests { - if !encoder.CanEncode(test) { - t.Errorf("NumberEncoder should be able to encode %q", string(test)) - } - } - } - shouldEncode('0', '1', '2', '3', '4', '5', '6', '7', '8', '9') - - shouldNotEncode := func(tests ...rune) { - for _, test := range tests { - if encoder.CanEncode(test) { - t.Errorf("NumberEncoder should not be able to encode %q", string(test)) - } - } - } - shouldNotEncode('a', 'q', '\t') -} - -func TestNumberEncoder_GetSwitchCode(t *testing.T) { - encoder := newNumberEncoder() - if sc := encoder.GetSwitchCode("123"); sc != switchCodeNumber { - t.Errorf("Unexpected switchcode. Got %v", sc) - } - if sc := encoder.GetSwitchCode("foo"); sc != switchCodeNumber { - t.Errorf("Unexpected switchcode. Got %v", sc) - } -} - -func TestNumberEncoder_Encode(t *testing.T) { - encoder := newNumberEncoder() - - if codes, err := encoder.Encode("01234"); err != nil { - t.Error(err) - } else { - compareIntSlice(t, []int{112, 434}, codes) - } -} diff --git a/pdf417/text_encoder.go b/pdf417/text_encoder.go deleted file mode 100644 index 5cb0ed8..0000000 --- a/pdf417/text_encoder.go +++ /dev/null @@ -1,249 +0,0 @@ -package pdf417 - -import "fmt" - -// Since each code word consists of 2 characters, a padding value is -// needed when encoding a single character. 29 is used as padding because -// it's a switch in all 4 submodes, and doesn't add any data. -const padding_value = 29 - -type subMode byte - -const ( - subUpper subMode = iota - subLower - subMixed - subPunct -) - -const ( - switchUpper = '\u00f1' - switchUpperSingle = '\u00f2' - switchLower = '\u00f3' - switchMixed = '\u00f4' - switchPunct = '\u00f5' - switchPunctSingle = '\u00f6' -) - -type textEncoder struct { - Switching map[subMode]map[subMode][]rune - SwitchSubmode map[rune]subMode - ReverseLookup map[rune]map[subMode]int -} - -func newTextEncoder() *textEncoder { - encoder := new(textEncoder) - - characterTables := map[subMode][]rune{ - subUpper: []rune{ - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', - 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', - 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' ', - switchLower, - switchMixed, - switchPunctSingle, - }, - - subLower: []rune{ - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', - 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', - 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ' ', - switchUpperSingle, - switchMixed, - switchPunctSingle, - }, - - subMixed: []rune{ - '0', '1', '2', '3', '4', '5', '6', '7', '8', - '9', '&', '\r', '\t', ',', ':', '#', '-', '.', - '$', '/', '+', '%', '*', '=', '^', - switchPunct, ' ', - switchLower, - switchUpper, - switchPunctSingle, - }, - - subPunct: []rune{ - ';', '<', '>', '@', '[', '\\', ']', '_', '`', - '~', '!', '\r', '\t', ',', ':', '\n', '-', '.', - '$', '/', 'g', '|', '*', '(', ')', '?', '{', '}', '\'', - switchUpper, - }, - } - - encoder.Switching = map[subMode]map[subMode][]rune{ - subUpper: map[subMode][]rune{ - subLower: []rune{switchLower}, - subMixed: []rune{switchMixed}, - subPunct: []rune{switchMixed, switchPunct}, - }, - - subLower: map[subMode][]rune{ - subUpper: []rune{switchMixed, switchUpper}, - subMixed: []rune{switchMixed}, - subPunct: []rune{switchMixed, switchPunct}, - }, - - subMixed: map[subMode][]rune{ - subUpper: []rune{switchUpper}, - subLower: []rune{switchLower}, - subPunct: []rune{switchPunct}, - }, - - subPunct: map[subMode][]rune{ - subUpper: []rune{switchUpper}, - subLower: []rune{switchUpper, switchLower}, - subMixed: []rune{switchUpper, switchMixed}, - }, - } - - encoder.SwitchSubmode = map[rune]subMode{ - switchUpper: subUpper, - switchLower: subLower, - switchPunct: subPunct, - switchMixed: subMixed, - } - - encoder.ReverseLookup = make(map[rune]map[subMode]int) - for submode, codes := range characterTables { - for row, char := range codes { - if encoder.ReverseLookup[char] == nil { - encoder.ReverseLookup[char] = make(map[subMode]int) - } - - encoder.ReverseLookup[char][submode] = int(row) - } - } - - return encoder -} - -func (encoder *textEncoder) CanEncode(char rune) bool { - switch char { - case switchUpper, - switchUpperSingle, - switchLower, - switchMixed, - switchPunct, - switchPunctSingle: - return false - default: - return encoder.ReverseLookup[char] != nil - } -} - -func (*textEncoder) GetSwitchCode(data string) int { - return switchCodeText -} - -func (encoder *textEncoder) Encode(data string) ([]int, error) { - interim, err := encoder.encodeInterim(data) - if err != nil { - return interim, err - } - - return encoder.encodeFinal(interim), nil -} - -func (encoder *textEncoder) encodeInterim(data string) ([]int, error) { - submode := subUpper - - codes := []int{} - var err error - for _, char := range data { - if !encoder.existsInSubmode(char, submode) { - prevSubmode := submode - - submode, err = encoder.getSubmode(char) - if err != nil { - return codes, err - } - - switchCodes := encoder.getSwitchCodes(prevSubmode, submode) - - codes = append(codes, switchCodes...) - } - - codes = append( - codes, - encoder.getCharacterCode(char, submode), - ) - } - - return codes, nil -} - -func (encoder *textEncoder) getSubmode(char rune) (subMode, error) { - if lookup, ok := encoder.ReverseLookup[char]; ok { - for key := range lookup { - return key, nil - } - } - return subLower, fmt.Errorf("unable to find submode for %q", char) -} - -func (encoder *textEncoder) getSwitchCodes(from, to subMode) []int { - switches := encoder.Switching[from][to] - - codes := []int{} - - for _, switcher := range switches { - codes = append(codes, encoder.getCharacterCode(switcher, from)) - - from = encoder.SwitchSubmode[switcher] - } - - return codes -} - -func (*textEncoder) encodeFinal(codes []int) []int { - codeWords := []int{} - - chunks := [][]int{} - chunkPart := []int{} - i := 0 - for _, code := range codes { - chunkPart = append(chunkPart, code) - - i++ - - if i%2 == 0 { - chunks = append(chunks, chunkPart) - - chunkPart = []int{} - } - } - - if len(chunkPart) > 0 { - chunks = append(chunks, chunkPart) - } - - for _, chunk := range chunks { - if len(chunk) == 1 { - chunk = append(chunk, padding_value) - } - - codeWords = append( - codeWords, - 30*chunk[0]+chunk[1], - ) - } - - return codeWords -} - -func (encoder *textEncoder) getCharacterCode(char rune, submode subMode) int { - cw, ok := encoder.ReverseLookup[char][submode] - - if !ok { - panic("This is not possible") - } - - return cw -} - -func (encoder *textEncoder) existsInSubmode(char rune, submode subMode) bool { - _, ok := encoder.ReverseLookup[char][submode] - - return ok -} diff --git a/pdf417/text_encoder_test.go b/pdf417/text_encoder_test.go deleted file mode 100644 index 5ffee24..0000000 --- a/pdf417/text_encoder_test.go +++ /dev/null @@ -1,59 +0,0 @@ -package pdf417 - -import ( - "testing" -) - -func compareIntSlice(t *testing.T, expected, actual []int) { - if len(actual) != len(expected) { - t.Errorf("Invalid slice size. Expected %d got %d", len(expected), len(actual)) - return - } - for i, a := range actual { - if e := expected[i]; e != a { - t.Errorf("Unexpected value at position %d. Expected %d got %d", i, e, a) - } - } -} - -func TestTextEncoder_CanEncode(t *testing.T) { - encoder := newTextEncoder() - - for ord := int(' '); ord < int('Z'); ord++ { - chr := rune(ord) - - if chr == '"' { - continue - } - - if !encoder.CanEncode(chr) { - t.Errorf("Unable to encode: %d %c", ord, chr) - } - } -} - -func TestTextEncoder_GetSwitchCode(t *testing.T) { - encoder := newTextEncoder() - if sc := encoder.GetSwitchCode("123"); sc != switchCodeText { - t.Errorf("Unexpected switchcode. Got %v", sc) - } - if sc := encoder.GetSwitchCode("foo"); sc != switchCodeText { - t.Errorf("Unexpected switchcode. Got %v", sc) - } -} - -func TestTextEncoder_Encode(t *testing.T) { - encoder := newTextEncoder() - - if codes, err := encoder.Encode("Super !"); err != nil { - t.Error(err) - } else { - compareIntSlice(t, []int{567, 615, 137, 808, 760}, codes) - } - - if codes, err := encoder.Encode("Super "); err != nil { - t.Error(err) - } else { - compareIntSlice(t, []int{567, 615, 137, 809}, codes) - } -}