Google Groups Home
Help | Sign in
Recent pages and files
Encoder Code Commentary    

 

An Encoder

The procedure labelled code2of5 is a simple example of an encoder, which we will now consider. Its purpose is to accept as input a string containing the barcode contents and a string containing a list of options, and to process these in a way that is specific to this encoder, and finally to output an instance of the dictionary-based data structure described in section 2.1 that represents the barcode contents in the Code 2 of 5 symbology.

As with all of the encoders, the input string is assumed to be valid for the corresponding symbology, otherwise the behaviour is undefined.

The variables that we use in this procedure are confined to local scope by declaring the procedure as follows:

/code2of5 {

0 begin

...

end

} bind def
/code2of5 load 0 1 dict put

We start by immediately reading the contents strings that are passed as arguments to this procedure by the user. We duplicate the options string because it is later passed unamended to the renderer.

     /options exch def
/useropts options def
/barcode exch def

We initialise a few default variables. Those variables corresponding to options that can be enabled with the options argument are initially set to false.

     /includetext false def
/textfont /Courier def
/textsize 10 def
/textpos -7 def
/height 1 def

The options string is tokenised with each successive token defining either a name value pair which we instantiate or a lone variable that we define as true, allowing us to override the given default variables given above.

     options {
token false eq {exit} if dup length string cvs (=) search
true eq {cvlit exch pop exch def} {cvlit true def} ifelse
} loop

Since any user given options create variables that are strings we need to convert them back to their intended types.

     /textfont textfont cvlit def
/textsize textsize cvr def
/textpos textpos cvr def
/height height cvr def

We then create an array of string encodings for each of the available characters which we then declare in another string. This information can be derived from careful reading of the relevant specification, although this is often surprisingly difficult to obtain.

     /encs
[ (1111313111) (3111111131) (1131111131) (3131111111)
(1111311131) (3111311111) (1131311111) (1111113131)
(3111113111) (1131113111) (313111) (311131)
] def

/barchars (0123456789) def

We now store the length of the content string and calculate the total number of bars and spaces in the resulting barcode. We initialise a string of size dependant on this length into which we will build the space bar succession. Similarly, we create an array into which we will add the human readable text information.

     /barlen barcode length def
/sbs barlen 10 mul 12 add string def
/txt barlen array def

We now begin to populate the space bar succession by adding the encoding of the start character to the beginning.

     sbs 0 encs 10 get putinterval

We now enter the main loop which iterates over the content string from start to finish, looking up the encoding for each character, adding this to the space bar succession.

It is important to understand how the encoding for a given character is derived. Firstly, given a character, we find its position in the string of all available characters. We then use this position to index the array of character encodings to obtain the encoding for the given character, which is added to the space/bar succession. Likewise, the character is added to the array of human readable text along with positioning and font information.

    0 1 barlen 1 sub {
/i exch def
barcode i 1 getinterval barchars exch search
pop
length /indx exch def
pop pop
/enc encs indx get def
sbs i 10 mul 6 add enc putinterval
txt i [barcode i 1 getinterval i 14 mul 10 add -7
textfont textsize] put
} for

The encoding for the end character is obtained and added to the end of the space bar succession.

    sbs barlen 10 mul 6 add encs 11 get putinterval

Finally we prepare to push a dictionary containing the space bar succession (and any additional information defined in section 2.1) that will be passed to the renderer.

     /retval 1 dict def
retval (sbs) [sbs {48 sub} forall] put
retval (bhs) [sbs length 1 add 2 idiv {height} repeat] put
retval (bbs) [sbs length 1 add 2 idiv {0} repeat] put
includetext {
retval (txt) txt put
} if
retval (opt) useropts put
retval
Version: 
Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google