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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
'''
Copyright (C) 2007 Martin Owens
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'''
import itertools
import sys
from lxml import etree
class Barcode:
def __init__(self, param={}):
self.document = None
self.x = 0
self.y = 0
if param.has_key('document'):
self.document = param['document']
if param.has_key('x'):
self.x = param['x']
if param.has_key('y'):
self.y = param['y']
if param.has_key('height'):
self.height = param['height']
else:
self.height = 30
self.text = param['text']
self.label = self.text
self.string = self.encode( self.text )
if not self.string:
return
self.width = len(self.string)
self.data = self.graphicalArray(self.string)
def generate(self):
svg_uri = u'http://www.w3.org/2000/svg'
if not self.string or not self.data:
return
data = self.data;
# create an SVG document if required
# if not self.document:
# self.document = UNKNOWN
if not self.document:
sys.stderr.write("No document defined to add barcode to\n")
return
# Collect document ids
doc_ids = {}
docIdNodes = self.document.xpath('//@id')
for m in docIdNodes:
doc_ids[m] = 1
# We don't have svg documents so lets do something raw:
name = 'barcode'
# Make sure that the id/name is inique
index = 0
while (doc_ids.has_key(name)):
name = 'barcode' + str(index)
index = index + 1
# use an svg group element to contain the barcode
barcode = etree.Element('{%s}%s' % (svg_uri,'g'))
barcode.set('id', name)
barcode.set('style', 'fill: black;')
draw = 1
wOffset = int(self.x)
id = 1
for datum in data:
# Datum 0 tells us what style of bar is to come next
style = self.getStyle(int(datum[0]))
# Datum 1 tells us what width in units,
# style tells us how wide a unit is
width = int(datum[1]) * int(style['width'])
if style['write']:
# Add height for styles such as EA8 where
# the barcode goes into the text
rect = etree.SubElement(barcode,'{%s}%s' % (svg_uri,'rect'))
rect.set('x', str(wOffset))
rect.set('y', str(style['top']))
rect.set('width', str(width))
rect.set('height', str(style['height']))
rect.set('id', name + '_bar' + str(id))
wOffset = int(wOffset) + int(width)
id = id + 1
barwidth = wOffset - int(self.x)
# Add text at the bottom of the barcode
text = etree.SubElement(barcode,'{%s}%s' % (svg_uri,'text'))
text.set( 'x', str(int(self.x) + int(barwidth / 2)) )
text.set( 'y', str(int(self.height) + 10 + int(self.y)) )
text.set( 'style', 'font-size:' + self.fontSize() + 'px;text-align:center;text-anchor:middle;' )
text.set( '{http://www.w3.org/XML/1998/namespace}space', 'preserve' )
text.set( 'id', name + '_bottomtext' )
text.text = str(self.label)
return barcode
# Converts black and white markers into a space array
def graphicalArray(self, code):
return [(x,len(list(y))) for x, y in itertools.groupby(code)]
def getStyle(self, index):
result = { 'width' : 1, 'top' : int(self.y), 'write' : False }
if index==1: # Black Bar
result['height'] = int(self.height)
result['write'] = True
return result
def fontSize(self):
return '9'
|