~centralelyon2010/inkscape/imagelinks2

« back to all changes in this revision

Viewing changes to share/extensions/Barcode/Code128.py

  • Committer: JazzyNico
  • Date: 2011-08-29 20:25:30 UTC
  • Revision ID: nicoduf@yahoo.fr-20110829202530-6deuoz11q90usldv
Code refactoring and merging with trunk (revision 10599).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
'''
2
 
Copyright (C) 2007 Martin Owens
3
 
 
4
 
Debugged by Ralf Heinecke & Martin Siepmann 09/07/2007
5
 
Debugged by Horst Schottky Feb. 27. 2010
6
 
 
7
 
This program is free software; you can redistribute it and/or modify
8
 
it under the terms of the GNU General Public License as published by
9
 
the Free Software Foundation; either version 2 of the License, or
10
 
(at your option) any later version.
11
 
 
12
 
This program is distributed in the hope that it will be useful,
13
 
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
GNU General Public License for more details.
16
 
 
17
 
You should have received a copy of the GNU General Public License
18
 
along with this program; if not, write to the Free Software
19
 
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 
 
21
 
 
22
 
 
23
 
'''
 
1
#
 
2
# Authored by Martin Owens <doctormo@ubuntu.com>
 
3
# Debugged by Ralf Heinecke & Martin Siepmann 2007-09-07
 
4
#             Horst Schottky 2010-02-27
 
5
#
 
6
# Copyright (C) 2007 Martin Owens
 
7
#
 
8
# This program is free software; you can redistribute it and/or modify
 
9
# it under the terms of the GNU General Public License as published by
 
10
# the Free Software Foundation; either version 2 of the License, or
 
11
# (at your option) any later version.
 
12
#
 
13
# This program is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
# GNU General Public License for more details.
 
17
#
 
18
# You should have received a copy of the GNU General Public License
 
19
# along with this program; if not, write to the Free Software
 
20
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
21
#
 
22
"""
 
23
Python barcode renderer for Code128/EAN128 barcodes. Designed for use with Inkscape.
 
24
"""
24
25
 
25
26
from Base import Barcode
26
27
import math
29
30
map = [ '11011001100','11001101100','11001100110','10010011000','10010001100','10001001100','10011001000','10011000100','10001100100','11001001000','11001000100','11000100100','10110011100','10011011100','10011001110','10111001100','10011101100','10011100110','11001110010','11001011100','11001001110','11011100100','11001110100','11101101110','11101001100','11100101100','11100100110','11101100100','11100110100','11100110010','11011011000','11011000110','11000110110','10100011000','10001011000','10001000110','10110001000','10001101000','10001100010','11010001000','11000101000','11000100010','10110111000','10110001110','10001101110','10111011000','10111000110','10001110110','11101110110','11010001110','11000101110','11011101000','11011100010','11011101110','11101011000','11101000110','11100010110','11101101000','11101100010','11100011010','11101111010','11001000010','11110001010','10100110000','10100001100','10010110000','10010000110','10000101100','10000100110','10110010000','10110000100','10011010000','10011000010','10000110100','10000110010','11000010010','11001010000','11110111010','11000010100','10001111010','10100111100','10010111100','10010011110','10111100100','10011110100','10011110010','11110100100','11110010100','11110010010','11011011110','11011110110','11110110110','10101111000','10100011110','10001011110','10111101000','10111100010','11110101000','11110100010','10111011110','10111101110','11101011110','11110101110','11010000100','11010010000','11010011100','11000111010','11' ]
30
31
 
31
32
def mapExtra(sd, chars):
32
 
        result = list(sd)
33
 
        for char in chars:
34
 
                result.append(chr(char))
35
 
        result.append('FNC3')
36
 
        result.append('FNC2')
37
 
        result.append('SHIFT')
38
 
        return result
 
33
    result = list(sd)
 
34
    for char in chars:
 
35
        result.append(chr(char))
 
36
    result.append('FNC3')
 
37
    result.append('FNC2')
 
38
    result.append('SHIFT')
 
39
    return result
39
40
 
40
41
# The mapExtra method is used to slim down the amount
41
42
# of pre code and instead we generate the lists
44
45
charB = mapExtra(charAB, range(96, 125)) # Offset -32
45
46
 
46
47
class Object(Barcode):
47
 
        def encode(self, text):
48
 
                result = ''
49
 
                blocks = []
50
 
                block  = ''
51
 
 
52
 
                # Split up into sections of numbers, or charicters
53
 
                # This makes sure that all the charicters are encoded
54
 
                # In the best way posible for Code128
55
 
                for datum in re.findall(r'(?:(?:\d\d){2,})|(?:^\d\d)|.', text):
56
 
                        if len(datum) == 1:
57
 
                                block = block + datum
58
 
                        else:
59
 
                                if block:
60
 
                                        blocks.append(self.bestBlock(block))
61
 
                                        block = ''
62
 
                                blocks.append( [ 'C', datum ] )
63
 
 
64
 
                if block:
65
 
                        blocks.append(self.bestBlock(block))
66
 
                        block = '';
67
 
                
68
 
                self.inclabel = text
69
 
                return self.encodeBlocks(blocks)
70
 
 
71
 
        def bestBlock(self, block):
72
 
                # If this has lower case then select B over A
73
 
                if block.upper() == block:
74
 
                        return [ 'A', block ]
75
 
                return [ 'B', block ]
76
 
                
77
 
        def encodeBlocks(self, blocks):
78
 
                total  = 0
79
 
                pos    = 0
80
 
                encode = '';
81
 
        
82
 
                for block in blocks:
83
 
                        set   = block[0]
84
 
                        datum = block[1]
85
 
 
86
 
                        # POS :   0,   1
87
 
                        # A   : 101, 103
88
 
                        # B   : 100, 104
89
 
                        # C   :  99, 105
90
 
                        num = 0;
91
 
                        if set == 'A':
92
 
                                num = 103
93
 
                        elif set == 'B':
94
 
                                num = 104
95
 
                        elif set == 'C':
96
 
                                num = 105
97
 
 
98
 
                        i = pos
99
 
                        if pos:
100
 
                                num = 204 - num
101
 
                        else:
102
 
                                i = 1
103
 
 
104
 
                        total = total + num * i
105
 
                        encode = encode + map[num]
106
 
                        pos = pos + 1
107
 
 
108
 
                        if set == 'A' or set == 'B':
109
 
                                chars = charB
110
 
                                if set == 'A':
111
 
                                        chars = charA
112
 
 
113
 
                                for char in datum:
114
 
                                        total = total + (chars.index(char) * pos)
115
 
                                        encode = encode + map[chars.index(char)]
116
 
                                        pos = pos + 1
117
 
                        else:
118
 
                                for char in (datum[i:i+2] for i in range(0, len(datum), 2)):
119
 
                                        total = total + (int(char) * pos)
120
 
                                        encode = encode + map[int(char)]
121
 
                                        pos = pos + 1
122
 
        
123
 
                checksum = total % 103
124
 
                encode = encode + map[checksum]
125
 
                encode = encode + map[106]
126
 
                encode = encode + map[107]
127
 
        
128
 
                return encode
 
48
    def encode(self, text):
 
49
        result = ''
 
50
        blocks = []
 
51
        block  = ''
 
52
 
 
53
        # Split up into sections of numbers, or charicters
 
54
        # This makes sure that all the charicters are encoded
 
55
        # In the best way posible for Code128
 
56
        for datum in re.findall(r'(?:(?:\d\d){2,})|(?:^\d\d)|.', text):
 
57
            if len(datum) == 1:
 
58
                block = block + datum
 
59
            else:
 
60
                if block:
 
61
                    blocks.append(self.bestBlock(block))
 
62
                    block = ''
 
63
                blocks.append( [ 'C', datum ] )
 
64
 
 
65
        if block:
 
66
            blocks.append(self.bestBlock(block))
 
67
            block = '';
 
68
        
 
69
        self.inclabel = text
 
70
        return self.encodeBlocks(blocks)
 
71
 
 
72
    def bestBlock(self, block):
 
73
        # If this has lower case then select B over A
 
74
        if block.upper() == block:
 
75
            return [ 'A', block ]
 
76
        return [ 'B', block ]
 
77
        
 
78
    def encodeBlocks(self, blocks):
 
79
        total  = 0
 
80
        pos    = 0
 
81
        encode = '';
 
82
    
 
83
        for block in blocks:
 
84
            set   = block[0]
 
85
            datum = block[1]
 
86
 
 
87
            # POS :   0,   1
 
88
            # A   : 101, 103
 
89
            # B   : 100, 104
 
90
            # C   :  99, 105
 
91
            num = 0;
 
92
            if set == 'A':
 
93
                num = 103
 
94
            elif set == 'B':
 
95
                num = 104
 
96
            elif set == 'C':
 
97
                num = 105
 
98
 
 
99
            i = pos
 
100
            if pos:
 
101
                num = 204 - num
 
102
            else:
 
103
                i = 1
 
104
 
 
105
            total = total + num * i
 
106
            encode = encode + map[num]
 
107
            pos = pos + 1
 
108
 
 
109
            if set == 'A' or set == 'B':
 
110
                chars = charB
 
111
                if set == 'A':
 
112
                    chars = charA
 
113
 
 
114
                for char in datum:
 
115
                    total = total + (chars.index(char) * pos)
 
116
                    encode = encode + map[chars.index(char)]
 
117
                    pos = pos + 1
 
118
            else:
 
119
                for char in (datum[i:i+2] for i in range(0, len(datum), 2)):
 
120
                    total = total + (int(char) * pos)
 
121
                    encode = encode + map[int(char)]
 
122
                    pos = pos + 1
 
123
    
 
124
        checksum = total % 103
 
125
        encode = encode + map[checksum]
 
126
        encode = encode + map[106]
 
127
        encode = encode + map[107]
 
128
    
 
129
        return encode
129
130