Skip to content

Commit 19c9435

Browse files
committed
* Added simple ITF (Interleaved 2-of-5) support.
1 parent a59f932 commit 19c9435

4 files changed

Lines changed: 94 additions & 0 deletions

File tree

barcode/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from barcode.ean import EAN8, EAN13, JAN
4040
from barcode.isxn import ISBN10, ISBN13, ISSN
4141
from barcode.upc import UPCA
42+
from barcode.itf import ITF
4243

4344
try:
4445
_strbase = basestring # lint:ok
@@ -62,6 +63,7 @@
6263
code39=Code39,
6364
pzn=PZN,
6465
code128=Code128,
66+
itf=ITF,
6567
)
6668

6769
PROVIDED_BARCODES = list(__BARCODE_MAP.keys())

barcode/charsets/itf.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from __future__ import unicode_literals
4+
5+
# W = Wide bar
6+
# w = wide space
7+
# N = Narrow bar
8+
# n = narrow space
9+
10+
START = 'NnNn'
11+
STOP = 'WnN'
12+
CODES = ('NNWWN', 'WNNNW', 'NWNNW', 'WWNNN', 'NNWNW', 'WNWNN', 'NWWNN', 'NNNWW', 'WNNWN', 'NWNWN')

barcode/itf.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from __future__ import unicode_literals
4+
5+
"""Module: barcode.itf
6+
7+
:Provided barcodes: Interleaved 2 of 5
8+
"""
9+
__docformat__ = 'restructuredtext en'
10+
11+
from barcode.base import Barcode
12+
from barcode.charsets import itf
13+
from barcode.errors import *
14+
15+
MIN_SIZE = 0.2
16+
MIN_QUIET_ZONE = 6.4
17+
18+
class ITF(Barcode):
19+
"""Initializes a new ITF instance.
20+
21+
:parameters:
22+
code : String
23+
ITF (Interleaved 2 of 5) numeric string
24+
writer : barcode.writer Instance
25+
The writer to render the barcode (default: SVGWriter).
26+
narrow: Integer
27+
Width of the narrow elements (default: 2)
28+
wide: Integer
29+
Width of the wide elements (default: 5)
30+
wide/narrow must be in the range 2..3
31+
"""
32+
33+
name = 'ITF'
34+
35+
def __init__(self, code, writer=None, narrow=2, wide=5):
36+
if not code.isdigit():
37+
raise IllegalCharacterError('ITF code can only contain numbers.')
38+
#Length must be even, prepend 0 if necessary
39+
if len(code) % 2 != 0:
40+
code = '0' + code
41+
self.code = code
42+
self.writer = writer or Barcode.default_writer()
43+
self.narrow = narrow
44+
self.wide = wide
45+
46+
def __unicode__(self):
47+
return self.code
48+
49+
__str__ = __unicode__
50+
51+
def get_fullcode(self):
52+
return self.code
53+
54+
def build(self):
55+
data = itf.START
56+
for i in range(0, len(self.code), 2):
57+
bars_digit = int(self.code[i])
58+
spaces_digit = int(self.code[i+1])
59+
for j in range(5):
60+
data += itf.CODES[bars_digit][j].upper()
61+
data += itf.CODES[spaces_digit][j].lower()
62+
data += itf.STOP
63+
print data
64+
raw = ''
65+
for e in data:
66+
if e == 'W':
67+
raw += '1' * self.wide
68+
if e == 'w':
69+
raw += '0' * self.wide
70+
if e == 'N':
71+
raw += '1' * self.narrow
72+
if e == 'n':
73+
raw += '0' * self.narrow
74+
return [raw]
75+
76+
def render(self, writer_options):
77+
options = dict(module_width=MIN_SIZE/self.narrow, quiet_zone=MIN_QUIET_ZONE)
78+
options.update(writer_options or {})
79+
return Barcode.render(self, options)

test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
('code39', 'Example Code 39'),
6161
('pzn', '487780'),
6262
('code128', 'Example Code 128 998866'),
63+
('itf', '12341234'),
6364
)
6465

6566

0 commit comments

Comments
 (0)