~mechiscogo/aeroo/mx

« back to all changes in this revision

Viewing changes to barcode/code39.py

  • Committer: mechiscogo
  • Date: 2011-02-19 00:02:13 UTC
  • Revision ID: mechiscogo@openerp-20110219000213-rskbcrjm3zr9gaz8
Agrega moneda MXN y traducciones al español en currency_to_text. Todas las traducciones llevan formato para México <importe con letra> <centavos>/100 <moneda>

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2008 marscel.wordpress.com
 
2
#
 
3
# Copyright (c) 2010 SIA "KN dati". (http://kndati.lv) All Rights Reserved.
 
4
#                    General contacts <info@kndati.lv>
 
5
 
 
6
# Code39.py v1
 
7
# Requires Python and Python Imaging Library (PIL), 
 
8
# has been tested with Python v2.6 and PIL v1.1.6
 
9
 
 
10
# Usage example:
 
11
# code39.py 100 2 "Hello World" barcode.png
 
12
#
 
13
# This creates a PNG image "barcode.png" containing a barcode of the height of 100px
 
14
# a min line width of 2px with "Hello World" encoded as "*HELLO WORLD*" in Code 39
 
15
 
 
16
import Image, ImageDraw, ImageFont, sys
 
17
from tools import config
 
18
 
 
19
marginx = 10
 
20
marginy = 10
 
21
fontsize = 15
 
22
 
 
23
charmap = {
 
24
'*':[0,3,0,1,2,1,2,1,0],
 
25
'-':[0,3,0,1,0,1,2,1,2],
 
26
'$':[0,3,0,3,0,3,0,1,0],
 
27
'%':[0,1,0,3,0,3,0,3,0],
 
28
' ':[0,3,2,1,0,1,2,1,0],
 
29
'.':[2,3,0,1,0,1,2,1,0],
 
30
'/':[0,3,0,3,0,1,0,3,0],
 
31
'+':[0,3,0,1,0,3,0,3,0],
 
32
'0':[0,1,0,3,2,1,2,1,0],
 
33
'1':[2,1,0,3,0,1,0,1,2],
 
34
'2':[0,1,2,3,0,1,0,1,2],
 
35
'3':[2,1,2,3,0,1,0,1,0],
 
36
'4':[0,1,0,3,2,1,0,1,2],
 
37
'5':[2,1,0,3,2,1,0,1,0],
 
38
'6':[0,1,2,3,2,1,0,1,0],
 
39
'7':[0,1,0,3,0,1,2,1,2],
 
40
'8':[2,1,0,3,0,1,2,1,0],
 
41
'9':[0,1,2,3,0,1,2,1,0],
 
42
'A':[2,1,0,1,0,3,0,1,2],
 
43
'B':[0,1,2,1,0,3,0,1,2],
 
44
'C':[2,1,2,1,0,3,0,1,0],
 
45
'D':[0,1,0,1,2,3,0,1,2],
 
46
'E':[2,1,0,1,2,3,0,1,0],
 
47
'F':[0,1,2,1,2,3,0,1,0],
 
48
'G':[0,1,0,1,0,3,2,1,2],
 
49
'H':[2,1,0,1,0,3,2,1,0],
 
50
'I':[0,1,2,1,0,3,2,1,0],
 
51
'J':[0,1,0,1,2,3,2,1,0],
 
52
'K':[2,1,0,1,0,1,0,3,2],
 
53
'L':[0,1,2,1,0,1,0,3,2],
 
54
'M':[2,1,2,1,0,1,0,3,0],
 
55
'N':[0,1,0,1,2,1,0,3,2],
 
56
'O':[2,1,0,1,2,1,0,3,0],
 
57
'P':[0,1,2,1,2,1,0,3,0],
 
58
'Q':[0,1,0,1,0,1,2,3,2],
 
59
'R':[2,1,0,1,0,1,2,3,0],
 
60
'S':[0,1,2,1,0,1,2,3,0],
 
61
'T':[0,1,0,1,2,1,2,3,0],
 
62
'U':[2,3,0,1,0,1,0,1,2],
 
63
'V':[0,3,2,1,0,1,0,1,2],
 
64
'W':[2,3,2,1,0,1,0,1,0],
 
65
'X':[0,3,0,1,2,1,0,1,2],
 
66
'Y':[2,3,0,1,2,1,0,1,0],
 
67
'Z':[0,3,2,1,2,1,0,1,0]
 
68
}
 
69
 
 
70
def create_c39(height, smallest, text):
 
71
    pixel_length = 0
 
72
    i = 0
 
73
    newtext = ""
 
74
    machinetext = "*" + text + "*"
 
75
    seglist = []
 
76
    while i < len(machinetext):
 
77
        char = machinetext[i].capitalize()
 
78
        i = i + 1
 
79
        try:
 
80
            map = charmap[char]
 
81
            if len(map) != 9:
 
82
                continue
 
83
            
 
84
            j = 0
 
85
            while j < 9:
 
86
                seg = int(map[j])
 
87
                
 
88
                if seg == 0 or seg == 1:
 
89
                    pixel_length = pixel_length + smallest
 
90
                    seglist.append(seg)
 
91
                elif seg == 2 or seg == 3:
 
92
                    pixel_length = pixel_length + smallest * 3
 
93
                    seglist.append(seg)
 
94
                
 
95
                j = j + 1
 
96
            
 
97
            newtext += char
 
98
        except:
 
99
            continue
 
100
    
 
101
    pixel_length = pixel_length + 2*marginx + len(newtext) * smallest
 
102
    pixel_height = height + 2*marginy + fontsize
 
103
    
 
104
    barcode_img = Image.new('RGB', [pixel_length, pixel_height], "white")
 
105
    
 
106
    if len(seglist) == 0:
 
107
        return barcode_img
 
108
    
 
109
    i = 0
 
110
    draw = ImageDraw.Draw(barcode_img)
 
111
    current_x = marginx
 
112
    
 
113
    while i < len(seglist):
 
114
        seg = seglist[i]
 
115
        color = (255, 255, 255)
 
116
        wdth = smallest
 
117
        
 
118
        if seg == 0 or seg == 2:
 
119
            color = 0
 
120
            if seg == 0:
 
121
                wdth = smallest
 
122
            else:
 
123
                wdth = smallest * 3
 
124
        elif seg == 1 or seg == 3:
 
125
            color = (255, 255, 255)
 
126
            if seg == 1:
 
127
                wdth = smallest
 
128
            else:
 
129
                wdth = smallest * 3
 
130
        
 
131
        j = 1
 
132
        
 
133
        while j <= wdth:
 
134
            draw.line((current_x, marginy, current_x, marginy+height), fill=color)
 
135
            current_x = current_x + 1            
 
136
            j = j + 1
 
137
        
 
138
        if ((i+1) % 9) == 0:        
 
139
            j = 1
 
140
            while j <= smallest:
 
141
                draw.line((current_x, marginy, current_x, marginy+height), fill=(255,255,255))
 
142
                current_x = current_x + 1
 
143
                j = j + 1            
 
144
        i = i + 1
 
145
 
 
146
    font = ImageFont.truetype(config['addons_path']+"/report_aeroo/barcode/FreeMonoBold.ttf", fontsize)
 
147
    
 
148
    draw.text((pixel_length/2 - len(newtext)*(fontsize/2)/2-len(newtext), height+fontsize), newtext, font=font, fill=0)
 
149
    
 
150
    del draw
 
151
    
 
152
    return barcode_img
 
153