Преглед на файлове

Fixes #13

FNC1 could also be encoded using C-Table
Florian Sundermann преди 9 години
родител
ревизия
f8b9e11d84
променени са 2 файла, в които са добавени 33 реда и са изтрити 6 реда
  1. 11 5
      code128/encode.go
  2. 22 1
      code128/encode_test.go

+ 11 - 5
code128/encode.go

@@ -29,6 +29,9 @@ func shouldUseCTable(nextRunes []rune, curEncoding byte) bool {
 		return false
 		return false
 	}
 	}
 	for i := 0; i < requiredDigits; i++ {
 	for i := 0; i < requiredDigits; i++ {
+		if i%2 == 0 && nextRunes[i] == FNC1 {
+			continue
+		}
 		if nextRunes[i] < '0' || nextRunes[i] > '9' {
 		if nextRunes[i] < '0' || nextRunes[i] > '9' {
 			return false
 			return false
 		}
 		}
@@ -40,7 +43,6 @@ func getCodeIndexList(content []rune) *utils.BitList {
 	result := new(utils.BitList)
 	result := new(utils.BitList)
 	curEncoding := byte(0)
 	curEncoding := byte(0)
 	for i := 0; i < len(content); i++ {
 	for i := 0; i < len(content); i++ {
-
 		if shouldUseCTable(content[i:], curEncoding) {
 		if shouldUseCTable(content[i:], curEncoding) {
 			if curEncoding != startCSymbol {
 			if curEncoding != startCSymbol {
 				if curEncoding == byte(0) {
 				if curEncoding == byte(0) {
@@ -50,11 +52,15 @@ func getCodeIndexList(content []rune) *utils.BitList {
 				}
 				}
 				curEncoding = startCSymbol
 				curEncoding = startCSymbol
 			}
 			}
-			idx := (content[i] - '0') * 10
-			i++
-			idx = idx + (content[i] - '0')
+			if content[i] == FNC1 {
+				result.AddByte(102)
+			} else {
 
 
-			result.AddByte(byte(idx))
+				idx := (content[i] - '0') * 10
+				i++
+				idx = idx + (content[i] - '0')
+				result.AddByte(byte(idx))
+			}
 		} else {
 		} else {
 			if curEncoding != startBSymbol {
 			if curEncoding != startBSymbol {
 				if curEncoding == byte(0) {
 				if curEncoding == byte(0) {

+ 22 - 1
code128/encode_test.go

@@ -13,11 +13,23 @@ func testEncode(t *testing.T, txt, testResult string) {
 		if code.Bounds().Max.X != len(testResult) {
 		if code.Bounds().Max.X != len(testResult) {
 			t.Errorf("%v: length missmatch", txt)
 			t.Errorf("%v: length missmatch", txt)
 		} else {
 		} else {
+			encoded := ""
+			failed := false
 			for i, r := range testResult {
 			for i, r := range testResult {
+				if code.At(i, 0) == color.Black {
+					encoded += "1"
+				} else {
+					encoded += "0"
+				}
+
 				if (code.At(i, 0) == color.Black) != (r == '1') {
 				if (code.At(i, 0) == color.Black) != (r == '1') {
+					failed = true
 					t.Errorf("%v: code missmatch on position %d", txt, i)
 					t.Errorf("%v: code missmatch on position %d", txt, i)
 				}
 				}
 			}
 			}
+			if failed {
+				t.Log("Encoded: ", encoded)
+			}
 		}
 		}
 	}
 	}
 }
 }
@@ -30,7 +42,8 @@ func Test_EncodeFunctionChars(t *testing.T) {
 	encStartB := "11010010000"
 	encStartB := "11010010000"
 	encStop := "1100011101011"
 	encStop := "1100011101011"
 
 
-	testEncode(t, string(FNC1)+"123", encStartB+encFNC1+"10011100110"+"11001110010"+"11001011100"+"11001000010"+encStop)
+	// Special Case FC1 can also be encoded to C Table therefor using 123 as suffix might have unexpected results.
+	testEncode(t, string(FNC1)+"A23", encStartB+encFNC1+"10100011000"+"11001110010"+"11001011100"+"10100011110"+encStop)
 	testEncode(t, string(FNC2)+"123", encStartB+encFNC2+"10011100110"+"11001110010"+"11001011100"+"11100010110"+encStop)
 	testEncode(t, string(FNC2)+"123", encStartB+encFNC2+"10011100110"+"11001110010"+"11001011100"+"11100010110"+encStop)
 	testEncode(t, string(FNC3)+"123", encStartB+encFNC3+"10011100110"+"11001110010"+"11001011100"+"11101000110"+encStop)
 	testEncode(t, string(FNC3)+"123", encStartB+encFNC3+"10011100110"+"11001110010"+"11001011100"+"11101000110"+encStop)
 	testEncode(t, string(FNC4)+"123", encStartB+encFNC4+"10011100110"+"11001110010"+"11001011100"+"11100011010"+encStop)
 	testEncode(t, string(FNC4)+"123", encStartB+encFNC4+"10011100110"+"11001110010"+"11001011100"+"11100011010"+encStop)
@@ -48,4 +61,12 @@ func Test_Unencodable(t *testing.T) {
 func Test_EncodeCTable(t *testing.T) {
 func Test_EncodeCTable(t *testing.T) {
 	testEncode(t, "HI345678H", "110100100001100010100011000100010101110111101000101100011100010110110000101001011110111011000101000111011000101100011101011")
 	testEncode(t, "HI345678H", "110100100001100010100011000100010101110111101000101100011100010110110000101001011110111011000101000111011000101100011101011")
 	testEncode(t, "334455", "11010011100101000110001000110111011101000110100100111101100011101011")
 	testEncode(t, "334455", "11010011100101000110001000110111011101000110100100111101100011101011")
+
+	testEncode(t, string(FNC1)+"1234",
+		"11010011100"+ // Start C
+			"11110101110"+ // FNC1
+			"10110011100"+ // 12
+			"10001011000"+ // 34
+			"11101001100"+ // CheckSum == 24
+			"1100011101011") // Stop
 }
 }