
    BPcR                        d Z ddlZddlmZ ddlmZ dZi dddd	d
dddddddddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5Zd6 Zd7 Z	d8 Z
d9 Zd: Zd; Zd< Zd= Zd> Zd? Zd@ ZdA ZdB ZdC ZdD Z G dE dFej                  ZedGk    r e                                             dS dS )Ha  
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
    N)	Rectangle)inkex_gettext   sq10)	
   r      r      r	         r	   sq12)	   r   r   r   r	   r	   r      r	   sq14)	   r   r   r   r	   r	   r   r   r	   sq16)	   r   r   r   r	   r	   r   r   r	   sq18)	   r   r   r   r	   r	   r   r   r	   sq20)	   r   r   r   r	   r	      r   r	   sq22)	r   r   r   r   r	   r	      r   r	   sq24)	   r   r   r   r	   r	   $   r   r	   sq26)	   r   r   r   r	   r	   ,      r	   sq32)	    r"   r   r   r   r   >   r   r	   sq36)	r   r   r   r   r   r   V   *   r	   sq40)	(   r(   r   r   r   r   r   0   r	   sq44)	r   r   r   r   r   r      8   r	   sq48)	r*   r*   r   r   r   r      D   r	   sq52)	4   r2   r   r   r   r   f   r&   r   sq64)	@   r5   r   r      r6      r-   r   sq72)	H   r9   r   r   r6   r6   \   r   r6   )	P   r;   r   r   r6   r6   r)   r*   r6   )	X   r<   r   r   r6   r6   r,   r-   r6   )	`   r=   r   r   r6   r6   r/   r0   r6   )	h   r>   r   r   r6   r6      r-      )	x   rA   r   r   r@   r@      r0   r@   )	   rC   r   r   r@   r@      r#   r   )	r,   r,   r   r   r@   r@   r   r   r   )	r   r   r@   r   r	   r	   r   r   r	   )	r   r"   r@   r   r	   r   r      r	   )	r   r   r   r   r	   r	   r   r   r	   )	r   r   r   r   r	   r   r   r   r	   )	r   r   r   r   r	   r   r"   r   r	   )	r   r*   r   r   r	   r   1   r    r	   )sq80sq88sq96sq104sq120sq132sq144rect8x18rect8x32	rect12x26	rect12x36	rect16x36	rect16x48c                 z   t          t          |                     }|sHt          |||z            }t          ||          }t	          |||          }t          ||||d          }nt          |d          }t          t          |                    D ]}d}d}d}||         dd         }t          |g|          }t	          |||          }d}d	}d}||         dd          }	t          |	g|          }	t	          |	||          }	|}
|
                    |	           t          |
d
||d          }
|
d         ||<   |S )NF  r      r#   r   i  r      r   T)	listencode_to_asciipartition_data
interleavereed_solomoncombine_interleavedrangelenextend)textndncintersize144datadata_blocksiblock1block2blockss              ;/usr/share/inkscape/extensions/render_barcode_datamatrix.pyget_codewordsrm   h   s   %%&&D /'$"u*
 
 !
 
 #R
 
 *B
 

 %$
 
 s;''(( 	' 	'AEBB ^AK0F%00F!B F EBB ^GII.F%00F!B F FMM&!!!(RTBBF#AYKNN    c                 :   |dk    r| S g }| D ]}t          t          |          |z            fdt          |          D             }t                    D ]+}t          |          D ]}|||z  |z            ||         |<   ,|                    |           |S )Nr	   c                     g | ]}d gz  	S r    ).0rh   block_lengths     rl   
<listcomp>zinterleave.<locals>.<listcomp>   s-       '(l"  rn   )intr_   r^   r`   )rk   rd   resultblockinter_blocksrh   jrt   s          @rl   r[   r[      s    zz 
	( 
	(Es5zzE122L   ,1%LL  L <(( > >u > >A).q5y1})=LOA&&> MM,''''rn   c                    |dk    r| S g }t          t          |           |z            D ]}g }|rd}d}	n
||z  }||z  }	t          |          D ]/}
|                    | ||z  |
|z  z            |
|z                      0t          |	          D ]2}
|                    | ||z  |
|z  z            ||
|z  z                       3|                    |           |S )Nr	   rU   il  )r^   r_   append)rk   rd   rb   rc   re   rw   rh   data_codewordsnd_rangenc_rangerz   s              rl   r]   r]      s   zzKK5 
 
 	* 	*A  N &::8__ Q Q%%fQYU-B&CAJ&OPPPP8__ V V%%fQYU-B&CBeO&TUUUUMM.))))rn   c              #     K   d}|t          |           k     r| |                                         r_|t          |           dz
  k     rI| |dz                                            r,t          | |         | |dz            z             dz   V  |dz  }nt          | |                   dz   V  |dz  }|t          |           k     dS dS )z-Encode this text into chunks, ascii or digitsr   r	      r   N)r_   isdigitrv   ord)ra   rh   s     rl   rY   rY      s      	A
c$ii--7?? 	!c$ii!m"3"3a!e9L9L9N9N"3d1gQU+,,s2222FAAd1g,,""""FA c$ii------rn   c                 "   d}g }d}|t          |           k     rt          |           ||z   k    r&|                    | |||z                       ||z   }n| |t          |                    }t          |           }d}t          |          |k     rU|s|                    |           d}n#|                    t          ||                     |dz  }t          |          |k     U|                    |           n|t          |           k     |S )N   r   FTr	   )r_   r|   randomise_pad_253)rf   rs_dataPAD_VALrg   rh   
data_blockpad_pospaddeds           rl   rZ   rZ      s'   GK	A
c$ii--t99G##tAGO4555GAAa#d))m,J$iiGFJ')) K%%g...!FF%%&7&I&IJJJ1 J')) z***% c$ii--( rn   c                 <    d|z  dz  dz   }| |z   }|dk    r|S |dz
  S )N      r	      rr   )	pad_valuepad_positionpseudo_random_number
randomiseds       rl   r   r     s>     </36!;11JSCrn   c                 \    | dk    s|dk    rdS |||          ||         z   |dz
  z           }|S Nr   r	   rr   )xylogaloggfrw   s         rl   prodr     s<    Avvaqs1vA2623rn   c                     dg| z  }dg| z  }d| z
  |d<   d|d<   t          d|           D ]8}||dz
           dz  ||<   ||         | k    r||         |z  ||<   ||||         <   9||fS )Nr   r	   r   r^   )r   ppr   r   rh   s        rl   gen_log_alogr     s    #(C38DVCFDG1b\\  q1u+/Q7b==1glDGDG9rn   c           	      6   dg| dz   z  }d|d<   t          d| dz             D ]v}||dz
           ||<   |dz
  }|dk    r9||dz
           t          ||         ||         |||          z  ||<   |dz  }|dk    9t          |d         ||         |||          |d<   w|S r   )r^   r   )rc   r   r   r   crh   rz   s          rl   gen_poly_coeffsr   *  s    	
rAvAAaD1b1f 2 2Qx!E1ffQU8d1Q4a#tR@@@AaDFA 1ff AaD$q'3b11!Hrn   c                    d}d}t          ||          \  }}t          ||||          }| D ]}|                    dg|dz   z             t          d|          D ]Z}	||         ||	         z  }
t          d|          D ]6}|||z   dz            t	          |
|||z
  dz
           |||          z  |||z   <   7[|                                 | S )N   i-  r   r	   )r   r   r`   r^   r   pop)rf   rb   rc   r   r   r   r   r   rx   rh   krz   s               rl   r\   r\   >  s    	B	BR$$ICCr**A  aSBF^$$$ q" 	 	Ab	E!H$A1b\\   %b1fqj 1Dqa!}c45 5 !b1f
 			Krn   c                     | d|z
  z	  dz  S )z(bit() returns the bit'th bit of the byter   r   rr   )bytebit_chs     rl   bitr   ]  s     QZ A%%rn   c                     |dk     r||z   }|dz   |dz   dz  z
  }|dk     r||z   }|dz   |dz   dz  z
  }|| |         |<   dS )z8place a given bit with appropriate wrapping within arrayr   r6   r   Nrr   )arraynrowncolrowcolr   s         rl   moduler   c  sc    
QwwDjAg$(a(
QwwDjAg$(a(E#JsOOOrn   c                    t          d          D ]:}|dz
  df|dz
  df|dz
  dfd|dz
  fd|dz
  fd|dz
  fd|dz
  fd|dz
  fg|dz
  df|dz
  df|dz
  dfd|dz
  fd|dz
  fd|dz
  fd|dz
  fd|dz
  fg|dz
  df|dz
  df|dz
  dfd|dz
  fd|dz
  fd|dz
  fd|dz
  fd|dz
  fg|dz
  df|dz
  |dz
  fd|dz
  fd|dz
  fd|dz
  fd|dz
  fd|dz
  fd|dz
  fg|dz
  |dz
  f|dz
  |dz
  f|dz
  |dz
  f|dz
  |dz
  f|dz
  |f||dz
  f||dz
  f||fgg|          |         \  }}	t          |||||	t          ||dz                        <dS )z.Populate corner cases (0-3) and utah case (-1)r   r	   r   r   r
   r6   )r^   r   r   )
caser   r   r   r   r   charrh   r   r   s
             rl   place_squarer   p  s\   1XX 6: 6: q!q!q!C!GC!GC!GC!GC!G	 q!q!q!C!GC!GC!GC!GC!G	 q!q!q!C!GC!GC!GC!GC!G	 q!q#'"C!GC!GC!GC!GC!GC!G	 q#'"q#'"q#'"q#'"q#cAgcAgc
	U4
h i4h i41j 	udD!QD!a%(8(899991rn   c                 v   fdt          |          D             }d}d}d}	 ||k    r%|dk    r|t          d|||| |                   z  }n||dz
  k    r*|dk    r$dz  r|t          d|||| |                   z  }nm||dz
  k    r.|dk    r(dz  dk    r|t          d|||| |                   z  }n6||dz   k    r-|dk    r'dz  dk    r|t          d|||| |                   z  }|dk    r]|k     rW||k     r;|dk    r5||         |         t          k    r|t          d	||||| |                   z  }|dz  }|dz  }|dk    r|k     W|dz  }|dz  }||k     r]|dk    rW|dk    r;|k     r5||         |         t          k    r|t          d	||||| |                   z  }|dz  }|dz  }||k     r|dk    W|dz  }|dz  }||k     s|k     sn||dz
           dz
           t          k    rDd||dz
           dz
  <   d||dz
           dz
  <   d||dz
           dz
  <   d||dz
           dz
  <   |S )
zCfill an nrow x ncol array with the bits from the codewords in data.c                 &    g | ]}t           gz  S rr   )INVALID_BITrs   rh   r   s     rl   ru   zplace_bits.<locals>.<listcomp>  s     777ak]T!777rn   r   r6   Tr   r	   r   r
   )r^   r   r   )rf   r   r   r   r   r   r   s     `    rl   
place_bitsr     s    87775;;777ED
C
C" 4KKcQhhLE4tT4:NNNDDTAXooC1HH4!8HLE4tT4:NNNDDTAXooC1HH4!8q==LE4tT4:NNNDDTAXooC1HHD1H??LE4tT4:NNND axxcDjjd

c
3;0N0NRdCd4jQQQ1HC1HC	 axxcDjj 	qq TzzqqsTzzc
3;0N0NRdCd4jQQQ1HC1HC	 Tzzq 	qq ttE"J TAXtax K//$%dQhq!$%dQhq!$%dQhq!$%dQhq!Lrn   c                 Z   |dz   |z  }|dz   |z  fdt          |          D             }t          |          D ]?}t          |          D ]-}d||         ||dz   z  <   |dz  ||         ||dz   z  |z   dz   <   .@t          |          D ]B}t                    D ]0}d|||dz   z  |z   dz            |<   |dz   dz  |||dz   z           |<   1Ct          ||z            D ]J}t          ||z            D ]5}|dz   d||z  z  z   }	|dz   d||z  z  z   }
| |         |         ||
         |	<   6K|S )Nr   c                     g | ]}d gz  	S rq   rr   r   s     rl   ru   z&add_finder_pattern.<locals>.<listcomp>  s    2221#*222rn   r	   r   )r   	data_nrow	data_ncolreg_rowreg_colr   
datamatrixrh   rz   dest_coldest_rowr   s              @rl   add_finder_patternr     s   MW$DMW$D2222eDkk222J7^^  t 	 	A12JqM!y1}-.A qM!y1}-	9A=>>	 7^^ = =t 	= 	=A  qIM*Y6:; 34a%1JqIM*+A..		= 9w&''  y7*++ 	 	A1uqAN33H1uqAN33H-21X.Jx **	 rn   c                   @    e Zd Zd Zd Zed             Zd Zd Zd Z	dS )
DataMatrixc                     |                     dd           |                     d| j        d           |                     dt          d	           d S )
Nz--textInkscape)defaultz--symbolT)typerequiredz--sizer6   )r   r   )add_argumentarg_symbolsrv   )selfparss     rl   add_argumentszDataMatrix.add_arguments  sZ    (J777*4+;dKKK(a88888rn   c                 |    	 t           |          S # t          $ r" t          j        t	          d                    w xY w)z%Turn a symbol key into matrix metricszInvalid symbol size.)SYMBOLSKeyErrorinkexAbortExtension_)values    rl   r   zDataMatrix.arg_symbols  sK    	B5>! 	B 	B 	B&q)?'@'@AAA	Bs    ,;c              #     K   t          | j        j                  }t          j        dddd          }t          |          ||d}| j        j        s!t          j        t          d                     | j        | j        j        g| j        j	        R  }| 
                    |          D ]E\  }}|                    t          |          t          |          d           t          di |V  Fd S )	Nnone1z#000000)strokezstroke-widthfill)styleheightwidthzPlease enter an input string.)r   r   rr   )stroptionssizer   Stylera   r   r   encodesymbolrender_data_matrixupdater   )r   r   r   attribsencodedr   r   s          rl   generatezDataMatrix.generate  s     4<$%%vsIVVWWJJ$FF|  	K&q)H'I'IJJJ $+dl/F$,2EFFF++G44 	' 	'DAqNNQc!ff55666&&g&&&&&&	' 	'rn   c           
          t          |||	|
|dk              }g }|D ]?}t          |||z  ||z            }|                    t          |||||                     @|S )z
        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)
        r,   )rm   r   r|   r   )r   ra   r   r   r   r   r   r   rb   rc   rd   	codewordsmodule_arrayscodeword_stream	bit_arrays                  rl   r   zDataMatrix.encode"  s     "$Bts{CC	 ( 	 	O"W!4i'6I I   "9iGWUU    rn   c           	   #     K   | j         j        d         }| j         j        }||z  dz  }t          |          D ]\  }}t	          |          }t	          |d                   }t          |          D ]}	t          |          D ]~}
||	         |
         dk    r|
|z  ||z  z   |	|z  fV  '||	         |
         t          k    r@t          j        t          d          
                    ||	         |
                              dS )z:turn a 2D array of 1's and 0's into a set of black squaresr	   g      ?r   zInvalid bit value, {}!N)r   r   r   	enumerater_   r^   r   r   errormsgr   format)r   r   r   r   spacingrh   liner   r   r   r   s              rl   r   zDataMatrix.render_data_matrix:  s$     |"1%| +# // 		W 		WGAtYYFQLLE6]] W Wu W WAAwqzQ 4x!g+5q4x@@@@@a{22q)A'B'B'I'I$q'RS*'U'UVVV	WW			W 		Wrn   N)
__name__
__module____qualname__container_labelr   staticmethodr   r   r   r   rr   rn   rl   r   r     sx        "O9 9 9
 B B \B' ' '  0W W W W Wrn   r   __main__)__doc__r   r   inkex.localizationr   r   r   r   rm   r[   r]   rY   rZ   r   r   r   r   r\   r   r   r   r   r   GenerateExtensionr   r   runrr   rn   rl   <module>r     s  ( 6        1 1 1 1 1 1"
)" +" ,	"
 -" -" -" -" -" -" -" -" ." ." ."  .!"" .#"$ -%"& /..111 /-/2222C" " "V5 5 5t  0  6
 
 
   :         &  (  >& & &
 
 
9 9 9x3 3 3l  DDW DW DW DW DW( DW DW DWN zJLL rn   