This repository was archived by the owner on Apr 23, 2025. It is now read-only.
forked from WhyNotHugo/python-barcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitf.py
More file actions
81 lines (65 loc) · 2.23 KB
/
Copy pathitf.py
File metadata and controls
81 lines (65 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from barcode.base import Barcode
from barcode.charsets import itf
from barcode.errors import *
"""Module: barcode.itf
:Provided barcodes: Interleaved 2 of 5
"""
__docformat__ = 'restructuredtext en'
MIN_SIZE = 0.2
MIN_QUIET_ZONE = 6.4
class ITF(Barcode):
"""Initializes a new ITF instance.
:parameters:
code : String
ITF (Interleaved 2 of 5) numeric string
writer : barcode.writer Instance
The writer to render the barcode (default: SVGWriter).
narrow: Integer
Width of the narrow elements (default: 2)
wide: Integer
Width of the wide elements (default: 5)
wide/narrow must be in the range 2..3
"""
name = 'ITF'
def __init__(self, code, writer=None, narrow=2, wide=5):
if not code.isdigit():
raise IllegalCharacterError('ITF code can only contain numbers.')
# Length must be even, prepend 0 if necessary
if len(code) % 2 != 0:
code = '0' + code
self.code = code
self.writer = writer or Barcode.default_writer()
self.narrow = narrow
self.wide = wide
def __unicode__(self):
return self.code
__str__ = __unicode__
def get_fullcode(self):
return self.code
def build(self):
data = itf.START
for i in range(0, len(self.code), 2):
bars_digit = int(self.code[i])
spaces_digit = int(self.code[i+1])
for j in range(5):
data += itf.CODES[bars_digit][j].upper()
data += itf.CODES[spaces_digit][j].lower()
data += itf.STOP
raw = ''
for e in data:
if e == 'W':
raw += '1' * self.wide
if e == 'w':
raw += '0' * self.wide
if e == 'N':
raw += '1' * self.narrow
if e == 'n':
raw += '0' * self.narrow
return [raw]
def render(self, writer_options, text=None):
options = dict(module_width=MIN_SIZE/self.narrow,
quiet_zone=MIN_QUIET_ZONE)
options.update(writer_options or {})
return Barcode.render(self, options, text)