render_barcode_datamatrix.py revision 107e00c8104649437b9520d0ba298dba659e7cd7
# -*- coding: UTF-8 -*-
'''
Copyright (C) 2009 John Beard john.j.beard@gmail.com
######DESCRIPTION######
This extension renders a DataMatrix 2D barcode, as specified in
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
######LICENCE#######
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.
######VERSION HISTORY#####
Ver. Date Notes
0.50 2009-10-25 Full functionality, up to 144x144.
ASCII and compressed digit encoding only.
'''
# local library
import inkex
import simplestyle
from simpletransform import computePointInNode
symbols = {
'sq10': (10, 10),
'sq12': (12, 12),
'sq14': (14, 14),
'sq16': (16, 16),
'sq18': (18, 18),
'sq20': (20, 20),
'sq22': (22, 22),
'sq24': (24, 24),
'sq26': (26, 26),
'sq32': (32, 32),
'sq36': (36, 36),
'sq40': (40, 40),
'sq44': (44, 44),
'sq48': (48, 48),
'sq52': (52, 52),
'sq64': (64, 64),
'sq72': (72, 72),
'sq80': (80, 80),
'sq88': (88, 88),
'sq96': (96, 96),
'sq104': (104, 104),
'sq120': (120, 120),
'sq132': (132, 132),
'sq144': (144, 144),
'rect8x18': (8, 18),
'rect8x32': (8, 32),
'rect12x26': (12, 26),
'rect12x36': (12, 36),
'rect16x36': (16, 36),
'rect16x48': (16, 48),
}
#ENCODING ROUTINES ===================================================
# Take an input string and convert it to a sequence (or sequences)
#=====================================================================
#create a 2d list corresponding to the 1's and 0s of the DataMatrix
#retreive the parameters of this size of DataMatrix
else: #special handling will be required by get_codewords()
#generate the codewords including padding and ECC
# break up into separate arrays if more than one DataMatrix is needed
module_arrays = []
bit_array = place_bits(codeword_stream, (data_nrow*reg_row, data_ncol*reg_col)) #place the codewords' bits across the array as modules
module_arrays.append(add_finder_pattern( bit_array, data_nrow, data_ncol, reg_row, reg_col )) #add finder patterns around the modules
return module_arrays
#return parameters for the selected datamatrix size
# data_nrow number of rows in each data region
# data_ncol 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
#SQUARE SYMBOLS
return 8, 8, 1, 1, 3, 5, 1
return 10, 10, 1, 1, 5, 7, 1
return 12, 12, 1, 1, 8, 10, 1
return 14, 14, 1, 1, 12, 12, 1
return 16, 16, 1, 1, 18, 14, 1
return 18, 18, 1, 1, 22, 18, 1
return 20, 20, 1, 1, 30, 20, 1
return 22, 22, 1, 1, 36, 24, 1
return 24, 24, 1, 1, 44, 28, 1
return 14, 14, 2, 2, 62, 36, 1
return 16, 16, 2, 2, 86, 42, 1
return 18, 18, 2, 2, 114, 48, 1
return 20, 20, 2, 2, 144, 56, 1
return 22, 22, 2, 2, 174, 68, 1
return 24, 24, 2, 2, 102, 42, 2
return 16, 16, 4, 4, 140, 56, 2
return 16, 16, 4, 4, 92, 36, 4
return 18, 18, 4, 4, 114, 48, 4
return 20, 20, 4, 4, 144, 56, 4
return 22, 22, 4, 4, 174, 68, 4
return 24, 24, 4, 4, 136, 56, 6
return 18, 18, 6, 6, 175, 68, 6
return 20, 20, 6, 6, 163, 62, 8
return 22, 22, 6, 6, 0, 0, 0 #there are two separate sections of the data matrix with
#different interleaving and reed-solomon parameters.
#this will be handled separately
#RECTANGULAR SYMBOLS
return 6, 16, 1, 1, 5, 7, 1
return 6, 14, 1, 2, 10, 11, 1
return 10, 24, 1, 1, 16, 14, 1
return 10, 16, 1, 2, 22, 18, 1
return 14, 16, 1, 2, 32, 24, 1
return 14, 22, 1, 2, 49, 28, 1
#RETURN ERROR
else:
exit(0)
return None
# CODEWORD STREAM GENERATION =========================================
#take the text input and return the codewords,
#including the Reed-Solomon error-correcting codes.
#=====================================================================
#convert the data to the codewords
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 = 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 )
inter = 8
nd = 156
nc = 62
inter = 2
nd = 155
nc = 62
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]
return blocks
else:
result = []
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]]
return blocks
else:
result = []
data_codewords = [] #interleaved data blocks
if size144:
else:
return result
#checks if an ASCII character is a digit from 0 - 9
return True
else:
return False
def encode_to_ascii( text):
ascii = []
i = 0
#check for double digits
if is_digit( text[i] ) and ( i < len(text)-1) and is_digit( text[i+1] ): #if the next char is also a digit
i = i + 2 #move on 2 characters
else: #encode as a normal ascii,
i = i + 1 #next character
return ascii
#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]]
data_blocks = []
i = 0
i = i + rs_data
else: #pad out with the pad codeword
if not padded:
else:
break
return data_blocks
#Pad character randomisation, to prevent regular patterns appearing
#in the data matrix
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"
if ( x==0 or y==0):
return 0
else:
return result
# generate the log & antilog lists:
# generate the generator polynomial coefficients:
c[0] = 1
c[i] = c[i-1]
j = i-1
while j >= 1:
j = j - 1
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 */
#parameters of the polynomial arithmetic
#generate "nc" checkwords in the list block
return data
#MODULE PLACEMENT ROUTINES===========================================
# These routines take a steam of codewords, and place them into the
# bit() returns the bit'th bit of the byte
#the MSB is bit 1, LSB is bit 8
# "module" places a given bit with appropriate wrapping within array
if (row < 0) :
if (col < 0):
#"utah" places the 8 bits of a utah-shaped symbol character in ECC200
#"place_bits" fills an nrow x ncol array with the bits from the
# codewords in data.
# First, fill the array[] with invalid entries */
INVALID = 2
# 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...
#sweep upward diagonally, inserting successive characters,...
while True:
break
# & then sweep downward diagonally, inserting successive characters,...
while True:
break
#... until the entire array is scanned
break
# Lastly, if the lower righthand corner is untouched, fill in fixed pattern */
return array #return the array of 1's and 0's
#get the total size of the datamatrix
return datamatrix
#RENDERING ROUTINES ==================================================
# Take the array of 1's and 0's and render as a series of black
# squares. A binary 1 is a filled square
#=====================================================================
#SVG element generation routine
def draw_SVG_square((w,h), (x,y), parent):
'stroke-width' : '1',
'fill' : '#000000'
}
attribs = {
'height' : str(h),
'width' : str(w),
'x' : str(x),
'y' : str(y)
}
#turn a 2D array of 1's and 0's into a set of black squares
#PARSE OPTIONS
else:
#INKSCAPE GROUP TO CONTAIN EVERYTHING
centre = tuple(computePointInNode(list(self.view_center), self.current_layer)) #Put in in the centre of the current view
grp_name = 'DataMatrix'
'transform':grp_transform }
#GENERATE THE DATAMATRIX
if __name__ == '__main__':
e = DataMatrix()
e.affect()
# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99