From 005ae8b4de4a2ae5ad75f26e68a61cb721ca941f Mon Sep 17 00:00:00 2001 From: acspike Date: Wed, 26 Sep 2007 02:42:03 +0000 Subject: [PATCH] long overdue conversion of Barcode to use lxml --- share/extensions/Barcode/Base.py | 218 ++++++++++++++++--------------- 1 file changed, 112 insertions(+), 106 deletions(-) diff --git a/share/extensions/Barcode/Base.py b/share/extensions/Barcode/Base.py index e8d5e5654..ccfca0093 100644 --- a/share/extensions/Barcode/Base.py +++ b/share/extensions/Barcode/Base.py @@ -15,113 +15,119 @@ 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): - 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 - - # We don't have svg documents so lets do something raw: - name = 'barcode' - - # Make sure that the id/name is inique - index = 0 - while (self.document.getElementById(name)): - name = 'barcode' + str(index) - index = index + 1 - - # use an svg group element to contain the barcode - barcode = self.document.createElement('svg:g') - barcode.setAttribute('id', name) - barcode.setAttribute('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 = self.document.createElement('svg:rect') - rect.setAttribute('x', str(wOffset)) - rect.setAttribute('y', str(style['top'])) - rect.setAttribute('width', str(width)) - rect.setAttribute('height', str(style['height'])) - rect.setAttribute('id', name + '_bar' + str(id)) - barcode.appendChild(rect) - wOffset = int(wOffset) + int(width) - id = id + 1 - - barwidth = wOffset - int(self.x) - # Add text at the bottom of the barcode - text = self.document.createElement('svg:text') - text.setAttribute( 'x', str(int(self.x) + int(barwidth / 2)) ) - text.setAttribute( 'y', str(int(self.height) + 10 + int(self.y)) ) - text.setAttribute( 'style', 'font-size:' + self.fontSize() + 'px;text-align:center;text-anchor:middle;' ) - text.setAttribute( 'xml:space', 'preserve' ) - text.setAttribute( 'id', name + '_bottomtext' ) - - text.appendChild(self.document.createTextNode(str(self.label))) - barcode.appendChild(text) - - 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' + 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( 'xml: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' -- 2.30.2