|
| 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) |
0 commit comments