ecc200datamatrix.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. #this code contributed by Kyle Macfarlane see
  2. #https://bitbucket.org/rptlab/reportlab/issues/69/implementations-of-code-128-auto-and-data
  3. __all__= ('ECC200datamatrix',)
  4. FACTORS = {
  5. 5: (228, 48, 15, 111, 62),
  6. 7: (23, 68, 144, 134, 240, 92, 254),
  7. 10: (28, 24, 185, 166, 223, 248, 116, 255, 110, 61),
  8. 11: (175, 138, 205, 12, 194, 168, 39, 245, 60, 97, 120),
  9. 12: (41, 153, 158, 91, 61, 42, 142, 213, 97, 178, 100, 242),
  10. 14: (156, 97, 192, 252, 95, 9, 157, 119, 138, 45, 18, 186, 83, 185),
  11. 18: (83, 195, 100, 39, 188, 75, 66, 61, 241, 213, 109, 129,
  12. 94, 254, 225, 48, 90, 188),
  13. 20: (15, 195, 244, 9, 233, 71, 168, 2, 188, 160, 153, 145,
  14. 253, 79, 108, 82, 27, 174, 186, 172),
  15. 24: (52, 190, 88, 205, 109, 39, 176, 21, 155, 197, 251, 223, 155,
  16. 21, 5, 172, 254, 124, 12, 181, 184, 96, 50, 193),
  17. 28: (211, 231, 43, 97, 71, 96, 103, 174, 37, 151, 170, 53, 75, 34,
  18. 249, 121, 17, 138, 110, 213, 141, 136, 120, 151, 233, 168, 93, 255),
  19. 36: (245, 127, 242, 218, 130, 250, 162, 181, 102, 120, 84, 179, 220, 251,
  20. 80, 182, 229, 18, 2, 4, 68, 33, 101, 137, 95, 119, 115, 44,
  21. 175, 184, 59, 25, 225, 98, 81, 112),
  22. 42: (77, 193, 137, 31, 19, 38, 22, 153, 247, 105, 122, 2, 245, 133,
  23. 242, 8, 175, 95, 100, 9, 167, 105, 214, 111, 57, 121, 21,
  24. 1, 253, 57, 54, 101, 248, 202, 69, 50, 150, 177, 226, 5, 9, 5),
  25. 48: (245, 132, 172, 223, 96, 32, 117, 22, 238, 133, 238, 231, 205, 188,
  26. 237, 87, 191, 106, 16, 147, 118, 23, 37, 90, 170, 205, 131, 88,
  27. 120, 100, 66, 138, 186, 240, 82, 44, 176, 87, 187, 147, 160, 175,
  28. 69, 213, 92, 253, 225, 19),
  29. 56: (175, 9, 223, 238, 12, 17, 220, 208, 100, 29, 175, 170, 230, 192,
  30. 215, 235, 150, 159, 36, 223, 38, 200, 132, 54, 228, 146, 218, 234,
  31. 117, 203, 29, 232, 144, 238, 22, 150, 201, 117, 62, 207, 164, 13,
  32. 137, 245, 127, 67, 247, 28, 155, 43, 203, 107, 233, 53, 143, 46),
  33. 62: (242, 93, 169, 50, 144, 210, 39, 118, 202, 188, 201, 189, 143, 108,
  34. 196, 37, 185, 112, 134, 230, 245, 63, 197, 190, 250, 106, 185, 221,
  35. 175, 64, 114, 71, 161, 44, 147, 6, 27, 218, 51, 63, 87, 10,
  36. 40, 130, 188, 17, 163, 31, 176, 170, 4, 107, 232, 7, 94, 166,
  37. 224, 124, 86, 47, 11, 204),
  38. 68: (220, 228, 173, 89, 251, 149, 159, 56, 89, 33, 147, 244, 154, 36,
  39. 73, 127, 213, 136, 248, 180, 234, 197, 158, 177, 68, 122, 93, 213,
  40. 15, 160, 227, 236, 66, 139, 153, 185, 202, 167, 179, 25, 220, 232,
  41. 96, 210, 231, 136, 223, 239, 181, 241, 59, 52, 172, 25, 49, 232,
  42. 211, 189, 64, 54, 108, 153, 132, 63, 96, 103, 82, 186)
  43. }
  44. LOGVAL = (
  45. -255, 255, 1, 240, 2, 225, 241, 53, 3, 38, 226, 133, 242, 43,
  46. 54, 210, 4, 195, 39, 114, 227, 106, 134, 28, 243, 140, 44, 23,
  47. 55, 118, 211, 234, 5, 219, 196, 96, 40, 222, 115, 103, 228, 78,
  48. 107, 125, 135, 8, 29, 162, 244, 186, 141, 180, 45, 99, 24, 49,
  49. 56, 13, 119, 153, 212, 199, 235, 91, 6, 76, 220, 217, 197, 11,
  50. 97, 184, 41, 36, 223, 253, 116, 138, 104, 193, 229, 86, 79, 171,
  51. 108, 165, 126, 145, 136, 34, 9, 74, 30, 32, 163, 84, 245, 173,
  52. 187, 204, 142, 81, 181, 190, 46, 88, 100, 159, 25, 231, 50, 207,
  53. 57, 147, 14, 67, 120, 128, 154, 248, 213, 167, 200, 63, 236, 110,
  54. 92, 176, 7, 161, 77, 124, 221, 102, 218, 95, 198, 90, 12, 152,
  55. 98, 48, 185, 179, 42, 209, 37, 132, 224, 52, 254, 239, 117, 233,
  56. 139, 22, 105, 27, 194, 113, 230, 206, 87, 158, 80, 189, 172, 203,
  57. 109, 175, 166, 62, 127, 247, 146, 66, 137, 192, 35, 252, 10, 183,
  58. 75, 216, 31, 83, 33, 73, 164, 144, 85, 170, 246, 65, 174, 61,
  59. 188, 202, 205, 157, 143, 169, 82, 72, 182, 215, 191, 251, 47, 178,
  60. 89, 151, 101, 94, 160, 123, 26, 112, 232, 21, 51, 238, 208, 131,
  61. 58, 69, 148, 18, 15, 16, 68, 17, 121, 149, 129, 19, 155, 59,
  62. 249, 70, 214, 250, 168, 71, 201, 156, 64, 60, 237, 130, 111, 20,
  63. 93, 122, 177, 150
  64. )
  65. ALOGVAL = (
  66. 1, 2, 4, 8, 16, 32, 64, 128, 45, 90, 180, 69, 138, 57,
  67. 114, 228, 229, 231, 227, 235, 251, 219, 155, 27, 54, 108, 216, 157,
  68. 23, 46, 92, 184, 93, 186, 89, 178, 73, 146, 9, 18, 36, 72,
  69. 144, 13, 26, 52, 104, 208, 141, 55, 110, 220, 149, 7, 14, 28,
  70. 56, 112, 224, 237, 247, 195, 171, 123, 246, 193, 175, 115, 230, 225,
  71. 239, 243, 203, 187, 91, 182, 65, 130, 41, 82, 164, 101, 202, 185,
  72. 95, 190, 81, 162, 105, 210, 137, 63, 126, 252, 213, 135, 35, 70,
  73. 140, 53, 106, 212, 133, 39, 78, 156, 21, 42, 84, 168, 125, 250,
  74. 217, 159, 19, 38, 76, 152, 29, 58, 116, 232, 253, 215, 131, 43,
  75. 86, 172, 117, 234, 249, 223, 147, 11, 22, 44, 88, 176, 77, 154,
  76. 25, 50, 100, 200, 189, 87, 174, 113, 226, 233, 255, 211, 139, 59,
  77. 118, 236, 245, 199, 163, 107, 214, 129, 47, 94, 188, 85, 170, 121,
  78. 242, 201, 191, 83, 166, 97, 194, 169, 127, 254, 209, 143, 51, 102,
  79. 204, 181, 71, 142, 49, 98, 196, 165, 103, 206, 177, 79, 158, 17,
  80. 34, 68, 136, 61, 122, 244, 197, 167, 99, 198, 161, 111, 222, 145,
  81. 15, 30, 60, 120, 240, 205, 183, 67, 134, 33, 66, 132, 37, 74,
  82. 148, 5, 10, 20, 40, 80, 160, 109, 218, 153, 31, 62, 124, 248,
  83. 221, 151, 3, 6, 12, 24, 48, 96, 192, 173, 119, 238, 241, 207,
  84. 179, 75, 150, 1
  85. )
  86. from reportlab.graphics.barcode.common import Barcode
  87. class ECC200DataMatrix(Barcode):
  88. '''This code only supports a Type 12 (44x44) C40 encoded data matrix.
  89. This is the size and encoding that Royal Mail wants on all mail from October 1st 2015.
  90. see https://bitbucket.org/rptlab/reportlab/issues/69/implementations-of-code-128-auto-and-data
  91. '''
  92. barWidth = 4
  93. def __init__(self, *args, **kwargs):
  94. Barcode.__init__(self,*args, **kwargs)
  95. # These values below are hardcoded for a Type 12 44x44 data matrix
  96. self.row_modules = 44
  97. self.col_modules = 44
  98. self.row_regions = 2
  99. self.col_regions = 2
  100. self.cw_data = 144
  101. self.cw_ecc = 56
  102. self.row_usable_modules = self.row_modules - self.row_regions * 2
  103. self.col_usable_modules = self.col_modules - self.col_regions * 2
  104. def validate(self):
  105. self.valid = 1
  106. for c in self.value:
  107. if ord(c) > 255:
  108. self.valid = 0
  109. break
  110. else:
  111. self.validated = self.value
  112. def _encode_c40_char(self, char):
  113. o = ord(char)
  114. encoded = []
  115. if o == 32 or (o >= 48 and o <= 57) or (o >= 65 and o <= 90):
  116. # Stay in set 0
  117. if o == 32:
  118. encoded.append(o - 29)
  119. elif o >= 48 and o <= 57:
  120. encoded.append(o - 44)
  121. else:
  122. encoded.append(o - 51)
  123. elif o >= 0 and o <= 31:
  124. encoded.append(0) # Shift to set 1
  125. encoded.append(o)
  126. elif (o >= 33 and o <= 64) or (o >= 91 and o <= 95):
  127. encoded.append(1) # Shift to set 2
  128. if o >= 33 and o <= 64:
  129. encoded.append(o - 33)
  130. else:
  131. encoded.append(o - 69)
  132. elif o >= 96 and o <= 127:
  133. encoded.append(2) # Shift to set 3
  134. encoded.append(o - 96)
  135. elif o >= 128 and o <= 255:
  136. # Extended ASCII
  137. encoded.append(1) # Shift to set 2
  138. encoded.append(30) # Upper shift / hibit
  139. encoded += self._encode_c40_char(chr(o - 128))
  140. else:
  141. raise Exception('Cannot encode %s (%s)' % (char, o))
  142. return encoded
  143. def _encode_c40(self, value):
  144. encoded = []
  145. for c in value:
  146. encoded += self._encode_c40_char(c)
  147. while len(encoded) % 3:
  148. encoded.append(0) # Fake padding that makes chunking in the next step easier
  149. codewords = []
  150. codewords.append(230) # Switch to C40 encoding
  151. for i in range(0, len(encoded), 3):
  152. chunk = encoded[i:i+3]
  153. total = chunk[0] * 1600 + chunk[1] * 40 + chunk[2] + 1
  154. codewords.append(total // 256)
  155. codewords.append(total % 256)
  156. codewords.append(254) # End of data
  157. if len(codewords) > self.cw_data:
  158. raise Exception('Too much data to fit into a data matrix of this size')
  159. if len(codewords) < self.cw_data:
  160. # Real padding
  161. codewords.append(129) # Start padding
  162. while len(codewords) < self.cw_data:
  163. r = ((149 * (len(codewords) + 1)) % 253) + 1
  164. codewords.append((129 + r) % 254)
  165. return codewords
  166. def _gfsum(self, int1, int2):
  167. return int1 ^ int2
  168. def _gfproduct(self, int1, int2):
  169. if int1 == 0 or int2 == 0:
  170. return 0
  171. else:
  172. return ALOGVAL[(LOGVAL[int1] + LOGVAL[int2]) % 255]
  173. def _get_reed_solomon_code(self, data, num_code_words):
  174. """
  175. This method is basically verbatim from "huBarcode" which is BSD licensed
  176. https://github.com/hudora/huBarcode/blob/master/hubarcode/datamatrix/reedsolomon.py
  177. """
  178. cw_factors = FACTORS[num_code_words]
  179. code_words = [0] * num_code_words
  180. for data_word in data:
  181. tmp = self._gfsum(data_word, code_words[-1])
  182. for j in range(num_code_words - 1, -1, -1):
  183. code_words[j] = self._gfproduct(tmp, cw_factors[j])
  184. if j > 0:
  185. code_words[j] = self._gfsum(code_words[j - 1], code_words[j])
  186. code_words.reverse()
  187. return code_words
  188. def _get_next_bits(self, data):
  189. value = data.pop(0)
  190. bits = []
  191. for i in range(0, 8):
  192. bits.append(value >> i & 1)
  193. bits.reverse()
  194. return bits
  195. def _place_bit(self, row, col, bit):
  196. if row < 0:
  197. row += self.row_usable_modules
  198. col += (4 - ((self.row_usable_modules + 4) % 8))
  199. if col < 0:
  200. col += self.col_usable_modules
  201. row += (4 - ((self.col_usable_modules + 4) % 8))
  202. self._matrix[row][col] = bit
  203. def _place_bit_corner_1(self, data):
  204. bits = self._get_next_bits(data)
  205. self._place_bit(self.row_usable_modules - 1, 0, bits[0])
  206. self._place_bit(self.row_usable_modules - 1, 1, bits[1])
  207. self._place_bit(self.row_usable_modules - 1, 2, bits[2])
  208. self._place_bit(0, self.col_usable_modules - 2, bits[3])
  209. self._place_bit(0, self.col_usable_modules - 1, bits[4])
  210. self._place_bit(1, self.col_usable_modules - 1, bits[5])
  211. self._place_bit(2, self.col_usable_modules - 1, bits[6])
  212. self._place_bit(3, self.col_usable_modules - 1, bits[7])
  213. def _place_bit_corner_2(self, data):
  214. bits = self._get_next_bits(data)
  215. self._place_bit(self.row_usable_modules - 3, 0, bits[0])
  216. self._place_bit(self.row_usable_modules - 2, 0, bits[1])
  217. self._place_bit(self.row_usable_modules - 1, 0, bits[2])
  218. self._place_bit(0, self.col_usable_modules - 4, bits[3])
  219. self._place_bit(0, self.col_usable_modules - 3, bits[4])
  220. self._place_bit(0, self.col_usable_modules - 2, bits[5])
  221. self._place_bit(0, self.col_usable_modules - 1, bits[6])
  222. self._place_bit(1, self.col_usable_modules - 1, bits[7])
  223. def _place_bit_corner_3(self, data):
  224. bits = self._get_next_bits(data)
  225. self._place_bit(self.row_usable_modules - 3, 0, bits[0])
  226. self._place_bit(self.row_usable_modules - 2, 0, bits[1])
  227. self._place_bit(self.row_usable_modules - 1, 0, bits[2])
  228. self._place_bit(0, self.col_usable_modules - 2, bits[3])
  229. self._place_bit(0, self.col_usable_modules - 1, bits[4])
  230. self._place_bit(1, self.col_usable_modules - 1, bits[5])
  231. self._place_bit(2, self.col_usable_modules - 1, bits[6])
  232. self._place_bit(3, self.col_usable_modules - 1, bits[7])
  233. def _place_bit_corner_4(self, data):
  234. bits = self._get_next_bits(data)
  235. self._place_bit(self.row_usable_modules - 1, 0, bits[0])
  236. self._place_bit(self.row_usable_modules - 1, self.col_usable_modules - 1, bits[1])
  237. self._place_bit(0, self.col_usable_modules - 3, bits[2])
  238. self._place_bit(0, self.col_usable_modules - 2, bits[3])
  239. self._place_bit(0, self.col_usable_modules - 1, bits[4])
  240. self._place_bit(1, self.col_usable_modules - 3, bits[5])
  241. self._place_bit(1, self.col_usable_modules - 2, bits[6])
  242. self._place_bit(1, self.col_usable_modules - 1, bits[7])
  243. def _place_bit_standard(self, data, row, col):
  244. bits = self._get_next_bits(data)
  245. self._place_bit(row - 2, col - 2, bits[0])
  246. self._place_bit(row - 2, col - 1, bits[1])
  247. self._place_bit(row - 1, col - 2, bits[2])
  248. self._place_bit(row - 1, col - 1, bits[3])
  249. self._place_bit(row - 1, col, bits[4])
  250. self._place_bit(row, col - 2, bits[5])
  251. self._place_bit(row, col - 1, bits[6])
  252. self._place_bit(row, col, bits[7])
  253. def _create_matrix(self, data):
  254. """
  255. This method is heavily influenced by "huBarcode" which is BSD licensed
  256. https://github.com/hudora/huBarcode/blob/master/hubarcode/datamatrix/placement.py
  257. """
  258. rows = self.row_usable_modules
  259. cols = self.col_usable_modules
  260. self._matrix = self._create_empty_matrix(rows, cols)
  261. row = 4
  262. col = 0
  263. while True:
  264. if row == rows and col == 0:
  265. self._place_bit_corner_1(data)
  266. elif row == (rows - 2) and col == 0 and (cols % 4):
  267. self._place_bit_corner_2(data)
  268. elif row == (rows - 2) and col == 0 and (cols % 8 == 4):
  269. self._place_bit_corner_3(data)
  270. elif row == (rows + 4) and col == 2 and (cols % 8 == 0):
  271. self._place_bit_corner_4(data)
  272. while True:
  273. if row < rows and col >= 0 and self._matrix[row][col] is None:
  274. self._place_bit_standard(data, row, col)
  275. row -= 2
  276. col += 2
  277. if row < 0 or col >= cols:
  278. break
  279. row += 1
  280. col += 3
  281. while True:
  282. if row >= 0 and col < cols and self._matrix[row][col] is None:
  283. self._place_bit_standard(data, row, col)
  284. row += 2
  285. col -= 2
  286. if row >= rows or col < 0:
  287. break
  288. row += 3
  289. col += 1
  290. if row >= rows and col >= cols:
  291. break
  292. for row in self._matrix:
  293. for i in range(0, cols):
  294. if row[i] is None:
  295. row[i] = 0
  296. return self._matrix
  297. def _create_data_regions(self, matrix):
  298. regions = []
  299. col_offset = 0
  300. row_offset = 0
  301. rows = int(self.row_usable_modules / self.row_regions)
  302. cols = int(self.col_usable_modules / self.col_regions)
  303. while col_offset < self.row_regions:
  304. while row_offset < self.col_regions:
  305. r_offset = col_offset * rows
  306. c_offset = row_offset * cols
  307. region = matrix[r_offset:rows+r_offset]
  308. for i in range(0, len(region)):
  309. region[i] = region[i][c_offset:cols+c_offset]
  310. regions.append(region)
  311. row_offset += 1
  312. row_offset = 0
  313. col_offset += 1
  314. return regions
  315. def _create_empty_matrix(self, row, col):
  316. matrix = []
  317. for i in range(0, row):
  318. matrix.append([None] * col)
  319. return matrix
  320. def _wrap_data_regions_with_finders(self, regions):
  321. wrapped = []
  322. for region in regions:
  323. matrix = self._create_empty_matrix(
  324. int(self.col_modules / self.col_regions),
  325. int(self.row_modules / self.row_regions)
  326. )
  327. for i, rows in enumerate(region):
  328. for j, data in enumerate(rows):
  329. matrix[i+1][j+1] = data
  330. for i, row in enumerate(matrix):
  331. if i == 0:
  332. for j, col in enumerate(row):
  333. row[j] = (j + 1) % 2
  334. elif i + 1 == len(matrix):
  335. for j, col in enumerate(row):
  336. row[j] = 1
  337. else:
  338. row[0] = 1
  339. row[-1] = i % 2
  340. wrapped.append(matrix)
  341. return wrapped
  342. def _merge_data_regions(self, regions):
  343. merged = []
  344. for i in range(0, len(regions), self.row_regions):
  345. chunk = regions[i:i+self.row_regions]
  346. j = 0
  347. while j < len(chunk[0]):
  348. merged_row = []
  349. for row in chunk:
  350. merged_row += row[j]
  351. merged.append(merged_row)
  352. j += 1
  353. return merged
  354. def encode(self):
  355. if hasattr(self, 'encoded'):
  356. return self.encoded
  357. encoded = self._encode_c40(self.validated)
  358. encoded += self._get_reed_solomon_code(encoded, self.cw_ecc)
  359. matrix = self._create_matrix(encoded)
  360. data_regions = self._create_data_regions(matrix)
  361. wrapped = self._wrap_data_regions_with_finders(data_regions)
  362. self.encoded = self._merge_data_regions(wrapped)
  363. self.encoded.reverse() # Helpful since PDFs start at bottom left corner
  364. return self.encoded
  365. def computeSize(self, *args):
  366. self._height = self.row_modules * self.barWidth
  367. self._width = self.col_modules * self.barWidth
  368. def draw(self):
  369. for y, row in enumerate(self.encoded):
  370. for x, data in enumerate(row):
  371. if data:
  372. self.rect(
  373. self.x + x * self.barWidth,
  374. self.y + y * self.barWidth,
  375. self.barWidth,
  376. self.barWidth
  377. )