summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 7a20e2e)
raw | patch | inline | side by side (parent: 7a20e2e)
author | acspike <acspike@users.sourceforge.net> | |
Wed, 26 Sep 2007 02:42:03 +0000 (02:42 +0000) | ||
committer | acspike <acspike@users.sourceforge.net> | |
Wed, 26 Sep 2007 02:42:03 +0000 (02:42 +0000) |
share/extensions/Barcode/Base.py | patch | blob | history |
index e8d5e5654503e4ea50ad86547b242fe4103942ea..ccfca0093f5929d42b097ad01c452e4183ab138b 100644 (file)
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'''
-
+\r
import itertools
import sys
+from lxml import etree\r
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):\r
+ 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
+ \r
+ # Collect document ids\r
+ doc_ids = {}\r
+ docIdNodes = self.document.xpath('//@id')\r
+ for m in docIdNodes:\r
+ doc_ids[m] = 1\r
+
+ # 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'