summaryrefslogtreecommitdiffstats
path: root/share/extensions/render_barcode_datamatrix.py
blob: 5b3906cde0401ff11e466edbff40a09373a4f9a2 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# Copyright (C) 2009 John Beard john.j.beard@gmail.com
#
# 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.
#
"""
This extension renders a DataMatrix 2D barcode, as specified in
BS ISO/IEC 16022:2006. Only ECC200 codes are considered, as these are the only
ones recommended for an "open" system.

The size of the DataMatrix is variable between 10x10 to 144x144

The absolute size of the DataMatrix modules (the little squares) is also
variable.

If more data is given than can be contained in one DataMatrix,
more than one DataMatrices will be produced.

Text is encoded as ASCII (the standard provides for other options, but these are
not implemented). Consecutive digits are encoded in a compressed form, halving
the space required to store them.

The basis processing flow is;
    * Convert input string to codewords (modified ASCII and compressed digits)
    * Split codewords into blocks of the right size for Reed-Solomon coding
    * Interleave the blocks if required
    * Apply Reed-Solomon coding
    * De-interleave the blocks if required
    * Place the codewords into the matrix bit by bit
    * Render the modules in the matrix as squares
"""

import inkex
from inkex import Rectangle
from inkex.localization import inkex_gettext as _

INVALID_BIT = 2

# return parameters for the selected datamatrix size
#   drow        number of rows in each data region
#   dcol        number of cols in each data region
#   reg_row     number of rows of data regions
#   reg_col     number of cols of data regions
#   nd          number of data codewords per reed-solomon block
#   nc          number of ECC codewords per reed-solomon block
#   inter       number of interleaved Reed-Solomon blocks
SYMBOLS = {
    # 'id': (nrow, ncol, drow, dcol, reg_row, reg_col, nd, nc, inter)
    "sq10": (10, 10, 8, 8, 1, 1, 3, 5, 1),
    "sq12": (12, 12, 10, 10, 1, 1, 5, 7, 1),
    "sq14": (14, 14, 12, 12, 1, 1, 8, 10, 1),
    "sq16": (16, 16, 14, 14, 1, 1, 12, 12, 1),
    "sq18": (18, 18, 16, 16, 1, 1, 18, 14, 1),
    "sq20": (20, 20, 18, 18, 1, 1, 22, 18, 1),
    "sq22": (22, 22, 20, 20, 1, 1, 30, 20, 1),
    "sq24": (24, 24, 22, 22, 1, 1, 36, 24, 1),
    "sq26": (26, 26, 24, 24, 1, 1, 44, 28, 1),
    "sq32": (32, 32, 14, 14, 2, 2, 62, 36, 1),
    "sq36": (36, 36, 16, 16, 2, 2, 86, 42, 1),
    "sq40": (40, 40, 18, 18, 2, 2, 114, 48, 1),
    "sq44": (44, 44, 20, 20, 2, 2, 144, 56, 1),
    "sq48": (48, 48, 22, 22, 2, 2, 174, 68, 1),
    "sq52": (52, 52, 24, 24, 2, 2, 102, 42, 2),
    "sq64": (64, 64, 14, 14, 4, 4, 140, 56, 2),
    "sq72": (72, 72, 16, 16, 4, 4, 92, 36, 4),
    "sq80": (80, 80, 18, 18, 4, 4, 114, 48, 4),
    "sq88": (88, 88, 20, 20, 4, 4, 144, 56, 4),
    "sq96": (96, 96, 22, 22, 4, 4, 174, 68, 4),
    "sq104": (104, 104, 24, 24, 4, 4, 136, 56, 6),
    "sq120": (120, 120, 18, 18, 6, 6, 175, 68, 6),
    "sq132": (132, 132, 20, 20, 6, 6, 163, 62, 8),
    # there are two separate sections of the data matrix with different interleaving
    # and reed-solomon parameters. this will be handled separately.
    "sq144": (144, 144, 22, 22, 6, 6, 0, 0, 0),
    "rect8x18": (8, 18, 6, 16, 1, 1, 5, 7, 1),
    "rect8x32": (8, 32, 6, 14, 1, 2, 10, 11, 1),
    "rect12x26": (12, 26, 10, 24, 1, 1, 16, 14, 1),
    "rect12x36": (12, 36, 10, 16, 1, 2, 22, 18, 1),
    "rect16x36": (16, 36, 14, 16, 1, 2, 32, 24, 1),
    "rect16x48": (16, 48, 14, 22, 1, 2, 49, 28, 1),
}


# CODEWORD STREAM GENERATION =========================================
# take the text input and return the codewords,
# including the Reed-Solomon error-correcting codes.
# =====================================================================


def get_codewords(text, nd, nc, inter, size144):
    # convert the data to the codewords
    data = list(encode_to_ascii(text))

    if not size144:  # render a "normal" datamatrix
        data_blocks = partition_data(
            data, nd * inter
        )  # partition into data blocks of length nd*inter -> inter Reed-Solomon block

        data_blocks = interleave(
            data_blocks, inter
        )  # interleave consecutive inter blocks if required

        data_blocks = reed_solomon(
            data_blocks, nd, nc
        )  # generate and append the Reed-Solomon codewords

        data_blocks = combine_interleaved(
            data_blocks, inter, nd, nc, False
        )  # concatenate Reed-Solomon blocks bound for the same datamatrix

    else:  # we have a 144x144 datamatrix
        data_blocks = partition_data(
            data, 1558
        )  # partition the data into datamtrix-sized chunks (1558 =156*8 + 155*2 )

        for i in range(len(data_blocks)):  # for each datamtrix

            inter = 8
            nd = 156
            nc = 62
            block1 = data_blocks[i][0 : 156 * 8]
            block1 = interleave([block1], inter)  # interleave into 8 blocks
            block1 = reed_solomon(
                block1, nd, nc
            )  # generate and append the Reed-Solomon codewords

            inter = 2
            nd = 155
            nc = 62
            block2 = data_blocks[i][156 * 8 :]
            block2 = interleave([block2], inter)  # interleave into 2 blocks
            block2 = reed_solomon(
                block2, nd, nc
            )  # generate and append the Reed-Solomon codewords

            blocks = block1
            blocks.extend(block2)

            blocks = combine_interleaved(blocks, 10, nd, nc, True)

            data_blocks[i] = blocks[0]

    return data_blocks


# Takes a codeword stream and splits up into "inter" blocks.
# eg interleave( [1,2,3,4,5,6], 2 ) -> [1,3,5], [2,4,6]
def interleave(blocks, inter):
    if inter == 1:  # if we don't have to interleave, just return the blocks
        return blocks
    else:
        result = []
        for block in blocks:  # for each codeword block in the stream
            block_length = int(len(block) / inter)  # length of each interleaved block
            inter_blocks = [
                [0] * block_length for i in range(inter)
            ]  # the interleaved blocks

            for i in range(block_length):  # for each element in the interleaved blocks
                for j in range(inter):  # for each interleaved block
                    inter_blocks[j][i] = block[i * inter + j]

            result.extend(inter_blocks)  # add the interleaved blocks to the output

        return result


# Combine interleaved blocks into the groups for the same datamatrix
#
# e.g combine_interleaved( [[d1, d3, d5, e1, e3, e5], [d2, d4, d6, e2, e4, e6]], 2, 3, 3 )
#   --> [[d1, d2, d3, d4, d5, d6, e1, e2, e3, e4, e5, e6]]
def combine_interleaved(blocks, inter, nd, nc, size144):
    if inter == 1:  # the blocks aren't interleaved
        return blocks
    else:
        result = []
        for i in range(
            len(blocks) // inter
        ):  # for each group of "inter" blocks -> one full datamatrix
            data_codewords = []  # interleaved data blocks

            if size144:
                nd_range = 1558  # 1558 = 156*8 + 155*2
                nc_range = 620  # 620 = 62*8 + 62*2
            else:
                nd_range = nd * inter
                nc_range = nc * inter

            for j in range(nd_range):  # for each codeword in the final list
                data_codewords.append(blocks[i * inter + j % inter][j // inter])

            for j in range(nc_range):  # for each block, add the ecc codewords
                data_codewords.append(blocks[i * inter + j % inter][nd + j // inter])

            result.append(data_codewords)
        return result


def encode_to_ascii(text):
    """Encode this text into chunks, ascii or digits"""
    i = 0
    while i < len(text):
        # check for double digits, if the next char is also a digit
        if text[i].isdigit() and (i < len(text) - 1) and text[i + 1].isdigit():
            yield int(text[i] + text[i + 1]) + 130
            i += 2  # move on 2 characters
        else:  # encode as a normal ascii,
            yield ord(text[i]) + 1  # codeword is ASCII value + 1 (ISO 16022:2006 5.2.3)
            i += 1  # next character


# partition data into blocks of the appropriate size to suit the
# Reed-Solomon block being used.
# e.g. partition_data([1,2,3,4,5], 3) -> [[1,2,3],[4,5,PAD]]
def partition_data(data, rs_data):
    PAD_VAL = 129  # PAD codeword (ISO 16022:2006 5.2.3)
    data_blocks = []
    i = 0
    while i < len(data):
        if len(data) >= i + rs_data:  # we have a whole block in our data
            data_blocks.append(data[i : i + rs_data])
            i = i + rs_data
        else:  # pad out with the pad codeword
            data_block = data[i : len(data)]  # add any remaining data
            pad_pos = len(data)
            padded = False
            while (
                len(data_block) < rs_data
            ):  # and then pad with randomised pad codewords
                if not padded:
                    data_block.append(PAD_VAL)  # add a normal pad codeword
                    padded = True
                else:
                    data_block.append(randomise_pad_253(PAD_VAL, pad_pos))
                pad_pos += 1
            data_blocks.append(data_block)
            break

    return data_blocks


# Pad character randomisation, to prevent regular patterns appearing
# in the data matrix
def randomise_pad_253(pad_value, pad_position):
    pseudo_random_number = ((149 * pad_position) % 253) + 1
    randomised = pad_value + pseudo_random_number
    if randomised <= 254:
        return randomised
    else:
        return randomised - 254


# REED-SOLOMON ENCODING ROUTINES =====================================

# "prod(x,y,log,alog,gf)" returns the product "x" times "y"
def prod(x, y, log, alog, gf):
    if x == 0 or y == 0:
        return 0
    else:
        result = alog[(log[x] + log[y]) % (gf - 1)]
        return result


# generate the log & antilog lists:
def gen_log_alog(gf, pp):
    log = [0] * gf
    alog = [0] * gf

    log[0] = 1 - gf
    alog[0] = 1

    for i in range(1, gf):
        alog[i] = alog[i - 1] * 2

        if alog[i] >= gf:
            alog[i] = alog[i] ^ pp

        log[alog[i]] = i

    return log, alog


# generate the generator polynomial coefficients:
def gen_poly_coeffs(nc, log, alog, gf):
    c = [0] * (nc + 1)
    c[0] = 1

    for i in range(1, nc + 1):
        c[i] = c[i - 1]

        j = i - 1
        while j >= 1:
            c[j] = c[j - 1] ^ prod(c[j], alog[i], log, alog, gf)
            j -= 1

        c[0] = prod(c[0], alog[i], log, alog, gf)

    return c


# "ReedSolomon(wd,nd,nc)" takes "nd" data codeword values in wd[]
# and adds on "nc" check codewords, all within GF(gf) where "gf" is a
# power of 2 and "pp" is the value of its prime modulus polynomial */
def reed_solomon(data, nd, nc):
    # parameters of the polynomial arithmetic
    gf = 256  # operating on 8-bit codewords -> Galois field = 2^8 = 256
    pp = 301  # prime modulus polynomial for ECC-200 is 0b100101101 = 301 (ISO 16022:2006 5.7.1)

    log, alog = gen_log_alog(gf, pp)
    c = gen_poly_coeffs(nc, log, alog, gf)

    for block in data:  # for each block of data codewords

        block.extend([0] * (nc + 1))  # extend to make space for the error codewords

        # generate "nc" checkwords in the list block
        for i in range(0, nd):
            k = block[nd] ^ block[i]

            for j in range(0, nc):
                block[nd + j] = block[nd + j + 1] ^ prod(
                    k, c[nc - j - 1], log, alog, gf
                )

        block.pop()

    return data


# MODULE PLACEMENT ROUTINES===========================================
#   These routines take a steam of codewords, and place them into the
#   DataMatrix in accordance with Annex F of BS ISO/IEC 16022:2006


def bit(byte, bit_ch):
    """bit() returns the bit'th bit of the byte"""
    # the MSB is bit 1, LSB is bit 8
    return (byte >> (8 - bit_ch)) % 2


def module(array, nrow, ncol, row, col, bit_ch):
    """place a given bit with appropriate wrapping within array"""
    if row < 0:
        row = row + nrow
        col = col + 4 - ((nrow + 4) % 8)

    if col < 0:
        col = col + ncol
        row = row + 4 - ((ncol + 4) % 8)

    array[row][col] = bit_ch


def place_square(case, array, nrow, ncol, row, col, char):
    """Populate corner cases (0-3) and utah case (-1)"""
    for i in range(8):
        x, y = [
            [
                (row - 1, 0),
                (row - 1, 1),
                (row - 1, 2),
                (0, col - 2),
                (0, col - 1),
                (1, col - 1),
                (2, col - 1),
                (3, col - 1),
            ],
            [
                (row - 3, 0),
                (row - 2, 0),
                (row - 1, 0),
                (0, col - 4),
                (0, col - 3),
                (0, col - 2),
                (0, col - 1),
                (1, col - 1),
            ],
            [
                (row - 3, 0),
                (row - 2, 0),
                (row - 1, 0),
                (0, col - 2),
                (0, col - 1),
                (1, col - 1),
                (2, col - 1),
                (3, col - 1),
            ],
            [
                (row - 1, 0),
                (row - 1, col - 1),
                (0, col - 3),
                (0, col - 2),
                (0, col - 1),
                (1, col - 3),
                (1, col - 2),
                (1, col - 1),
            ],
            # "utah" places the 8 bits of a utah-shaped symbol character in ECC200
            [
                (row - 2, col - 2),
                (row - 2, col - 1),
                (row - 1, col - 2),
                (row - 1, col - 1),
                (row - 1, col),
                (row, col - 2),
                (row, col - 1),
                (row, col),
            ],
        ][case][i]
        module(array, nrow, ncol, x, y, bit(char, i + 1))
    return 1


def place_bits(data, nrow, ncol):
    """fill an nrow x ncol array with the bits from the codewords in data."""
    # initialise and fill with -1's (invalid value)
    array = [[INVALID_BIT] * ncol for i in range(nrow)]
    # Starting in the correct location for character #1, bit 8,...
    char = 0
    row = 4
    col = 0
    while True:

        # first check for one of the special corner cases, then...
        if (row == nrow) and (col == 0):
            char += place_square(0, array, nrow, ncol, nrow, ncol, data[char])
        elif (row == nrow - 2) and (col == 0) and (ncol % 4):
            char += place_square(1, array, nrow, ncol, nrow, ncol, data[char])
        elif (row == nrow - 2) and (col == 0) and (ncol % 8 == 4):
            char += place_square(2, array, nrow, ncol, nrow, ncol, data[char])
        elif (row == nrow + 4) and (col == 2) and ((ncol % 8) == 0):
            char += place_square(3, array, nrow, ncol, nrow, ncol, data[char])

        # sweep upward diagonally, inserting successive characters,...
        while (row >= 0) and (col < ncol):
            if (row < nrow) and (col >= 0) and (array[row][col] == INVALID_BIT):
                char += place_square(-1, array, nrow, ncol, row, col, data[char])
            row -= 2
            col += 2

        row += 1
        col += 3

        # & then sweep downward diagonally, inserting successive characters,...
        while (row < nrow) and (col >= 0):
            if (row >= 0) and (col < ncol) and (array[row][col] == INVALID_BIT):
                char += place_square(-1, array, nrow, ncol, row, col, data[char])
            row += 2
            col -= 2

        row += 3
        col += 1

        # ... until the entire array is scanned
        if not ((row < nrow) or (col < ncol)):
            break

    # Lastly, if the lower righthand corner is untouched, fill in fixed pattern */
    if array[nrow - 1][ncol - 1] == INVALID_BIT:
        array[nrow - 1][ncol - 2] = 0
        array[nrow - 1][ncol - 1] = 1
        array[nrow - 2][ncol - 1] = 0
        array[nrow - 2][ncol - 2] = 1

    return array  # return the array of 1's and 0's


def add_finder_pattern(array, data_nrow, data_ncol, reg_row, reg_col):
    # get the total size of the datamatrix
    nrow = (data_nrow + 2) * reg_row
    ncol = (data_ncol + 2) * reg_col

    datamatrix = [[0] * ncol for i in range(nrow)]  # initialise and fill with 0's

    for i in range(reg_col):  # for each column of data regions
        for j in range(nrow):
            datamatrix[j][i * (data_ncol + 2)] = 1  # vertical black bar on left
            datamatrix[j][i * (data_ncol + 2) + data_ncol + 1] = (
                j % 2
            )  # alternating blocks

    for i in range(reg_row):  # for each row of data regions
        for j in range(ncol):
            datamatrix[i * (data_nrow + 2) + data_nrow + 1][
                j
            ] = 1  # horizontal black bar at bottom
            datamatrix[i * (data_nrow + 2)][j] = (j + 1) % 2  # alternating blocks

    for i in range(data_nrow * reg_row):
        for j in range(data_ncol * reg_col):
            # offset by 1, plus two for every addition block
            dest_col = j + 1 + 2 * (j // data_ncol)
            dest_row = i + 1 + 2 * (i // data_nrow)

            datamatrix[dest_row][dest_col] = array[i][
                j
            ]  # transfer from the plain bit array

    return datamatrix


class DataMatrix(inkex.GenerateExtension):
    container_label = "DataMatrix"

    def add_arguments(self, pars):
        pars.add_argument("--text", default="Inkscape")
        pars.add_argument("--symbol", type=self.arg_symbols, required=True)
        pars.add_argument("--size", type=int, default=4)

    @staticmethod
    def arg_symbols(value):
        """Turn a symbol key into matrix metrics"""
        try:
            return SYMBOLS[value]
        except KeyError:
            raise inkex.AbortExtension(_("Invalid symbol size."))

    def generate(self):
        size = str(self.options.size)
        style = inkex.Style({"stroke": "none", "stroke-width": "1", "fill": "#000000"})
        attribs = {"style": str(style), "height": size, "width": size}

        if not self.options.text:
            raise inkex.AbortExtension(_("Please enter an input string."))

        # create a 2d list corresponding to the 1's and 0s of the DataMatrix
        encoded = self.encode(self.options.text, *self.options.symbol)
        for x, y in self.render_data_matrix(encoded):
            attribs.update({"x": str(x), "y": str(y)})
            yield Rectangle(**attribs)

    def encode(
        self, text, nrow, ncol, data_nrow, data_ncol, reg_row, reg_col, nd, nc, inter
    ):
        """
        Take an input string and convert it to a sequence (or sequences)
        of codewords as specified in ISO/IEC 16022:2006 (section 5.2.3)
        """
        # generate the codewords including padding and ECC
        codewords = get_codewords(text, nd, nc, inter, nrow == 144)

        # break up into separate arrays if more than one DataMatrix is needed
        module_arrays = []
        for codeword_stream in codewords:  # for each datamatrix
            # place the codewords' bits across the array as modules
            bit_array = place_bits(
                codeword_stream, data_nrow * reg_row, data_ncol * reg_col
            )
            # add finder patterns around the modules
            module_arrays.append(
                add_finder_pattern(bit_array, data_nrow, data_ncol, reg_row, reg_col)
            )

        return module_arrays

    def render_data_matrix(self, module_arrays):
        """turn a 2D array of 1's and 0's into a set of black squares"""
        ncol = self.options.symbol[1]
        size = self.options.size
        spacing = ncol * size * 1.5
        for i, line in enumerate(module_arrays):
            height = len(line)
            width = len(line[0])

            for y in range(height):  # loop over all the modules in the datamatrix
                for x in range(width):
                    if line[y][x] == 1:  # A binary 1 is a filled square
                        yield (x * size + i * spacing, y * size)
                    elif line[y][x] == INVALID_BIT:  # we have an invalid bit value
                        inkex.errormsg(_("Invalid bit value, {}!").format(line[y][x]))


if __name__ == "__main__":
    DataMatrix().run()