summaryrefslogtreecommitdiffstats
path: root/share/extensions/Barcode/Base.py
blob: 8fee6a9960b69affcea4448c72f573d7861b0012 (plain)
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
#
# Copyright (C) 2010 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
#
"""
Base module for rendering barcodes for Inkscape.
"""

import itertools
import sys
from lxml import etree

(WHITE_BAR, BLACK_BAR, TALL_BAR) = range(3)
TEXT_TEMPLATE = 'font-size:%dpx;text-align:center;text-anchor:middle;'

class Barcode(object):
    """Provide a base class for all barcode renderers"""
    name = None

    def error(self, bar, msg):
        """Cause an error to be reported"""
        sys.stderr.write(
            "Error encoding '%s' as %s barcode: %s\n" % (bar, self.name, msg))

    def __init__(self, param={}):
        self.document = param.get('document', None)
        self.x        = int(param.get('x', 0))
        self.y        = int(param.get('y', 0))
        self.height   = param.get('height', 30)
        self.label    = param.get('text', None)
        self.string   = self.encode( self.label )

        if not self.string:
            return

        self.width  = len(self.string)
        self.data   = self.graphicalArray(self.string)

    def generate(self):
        """Generate the actual svg from the coding"""
        svg_uri = u'http://www.w3.org/2000/svg'
        if not self.string or not self.data:
            return
        if not self.document:
            return self.error("No document defined")
        
        data = self.data
        # 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;')
        barcode.set('transform', 'translate(%d,%d)' % (self.x, self.y))

        bar_offset = 0
        bar_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']:
                rect = etree.SubElement(barcode,'{%s}%s' % (svg_uri,'rect'))
                rect.set('x',      str(bar_offset))
                rect.set('y',      str(style['top']))
                rect.set('width',  str(width))
                rect.set('height', str(style['height']))
                rect.set('id',     "%s_bar%d" % (name, bar_id))
            bar_offset += width
            bar_id += 1

        bar_width = bar_offset
        # Add text at the bottom of the barcode
        text = etree.SubElement(barcode,'{%s}%s' % (svg_uri,'text'))
        text.set( 'x', str(int(bar_width / 2)))
        text.set( 'y', str(self.height + self.fontSize() ))
        text.set( 'style', TEXT_TEMPLATE % self.fontSize() )
        text.set( '{http://www.w3.org/XML/1998/namespace}space', 'preserve' )
        text.set( 'id', '%s_text' % name )
        text.text = str(self.label)
        return barcode

    def graphicalArray(self, code):
        """Converts black and white markets into a space array"""
        return [(x,len(list(y))) for x, y in itertools.groupby(code)]

    def getStyle(self, index):
        """Returns the styles that should be applied to each bar"""
        result = { 'width' : 1, 'top' : 0, 'write' : True }
        if index == BLACK_BAR:
            result['height'] = int(self.height)
        if index == TALL_BAR:
            result['height'] = int(self.height) + int(self.fontSize() / 2)
        if index == WHITE_BAR:
            result['write'] = False
        return result

    def fontSize(self):
        """Return the ideal font size, defaults to 9px"""
        return 9