summaryrefslogtreecommitdiffstats
path: root/share/extensions/barcode/Rm4scc.py
blob: b4d52bab1a7c68932c8355170d809f4f9dcd80a1 (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
131
132
133
134
135
136
137
138
139
# coding=utf-8
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
"""
Python barcode renderer for RM4CC barcodes. Designed for use with Inkscape.
"""

from .Base import Barcode

charmap = {
    "(": "25",
    ")": "3",
    "0": "05053535",
    "1": "05152535",
    "2": "05153525",
    "3": "15052535",
    "4": "15053525",
    "5": "15152525",
    "6": "05251535",
    "7": "05350535",
    "8": "05351525",
    "9": "15250535",
    "A": "15251525",
    "B": "15350525",
    "C": "05253515",
    "D": "05352515",
    "E": "05353505",
    "F": "15252515",
    "G": "15253505",
    "H": "15352505",
    "I": "25051535",
    "J": "25150535",
    "K": "25151525",
    "L": "35050535",
    "M": "35051525",
    "N": "35150525",
    "O": "25053525",
    "P": "25152515",
    "Q": "25153505",
    "R": "35052515",
    "S": "35053505",
    "T": "35152505",
    "U": "25251515",
    "V": "25350515",
    "W": "25351505",
    "X": "35250515",
    "Y": "35251505",
    "Z": "35350505",
}

check = ["ZUVWXY", "501234", "B6789A", "HCDEFG", "NIJKLM", "TOPQRS"]
(BAR_TRACK, BAR_DOWN, BAR_UP, BAR_FULL, BAR_NONE, WHITE_SPACE) = range(6)


class Rm4scc(Barcode):
    """Provice a Rm4scc barcode generator"""

    default_height = 18

    def encode(self, text):
        result = ""

        text = text.upper()
        text.replace("(", "")
        text.replace(")", "")

        text = "(" + text + Rm4scc.checksum(text) + ")"

        i = 0
        for char in text:
            if char in charmap:
                result = result + charmap[char]
                i += 1

        return result

    @staticmethod
    def checksum(text):
        """given a string of data, return the check character"""
        total_lower = 0
        total_upper = 0
        for char in text:
            if char in charmap:
                bars = charmap[char][0:8:2]
                lower = 0
                upper = 0

                if int(bars[0]) & 1:
                    lower += 4
                if int(bars[1]) & 1:
                    lower += 2
                if int(bars[2]) & 1:
                    lower += 1
                if int(bars[0]) & 2:
                    upper += 4
                if int(bars[1]) & 2:
                    upper += 2
                if int(bars[2]) & 2:
                    upper += 1
            total_lower += lower % 6
            total_upper += upper % 6

        total_lower = total_upper % 6
        total_upper %= 6

        checkchar = check[total_upper][total_lower]
        return checkchar

    def get_style(self, index):
        """Royal Mail Barcodes use a completely different style"""
        result = {"width": 2, "write": True, "top": 0}
        if index == BAR_TRACK:  # Track Bar
            result["top"] = 6
            result["height"] = 5
        elif index == BAR_DOWN:  # Decender Bar
            result["top"] = 6
            result["height"] = 11
        elif index == BAR_UP:  # Accender Bar
            result["height"] = 11
        elif index == BAR_FULL:  # Full Bar
            result["height"] = 17
        elif index == WHITE_SPACE:  # White Space
            result["write"] = False
        return result