Good programming practices

Barcode printing with GLE

October 4th, 2011

GLE (Graphics Layout Engine) is an Open Source project and a graphics script language for generating vector graphics. I used it for printing labels which contain some information about a products including barcode. Generated EPS files (PostScript) are easy to print, especially when you want to automate the process.

Below is a template of simple label, which takes 3 arguments from command line: barcode in binary and decimal format and name of a product. If there are less than 3 agruments, an example data are used.

  1. ! define constants
  2. STEP = 0.04 ! width of one bar
  3. LHG = 1.2 ! height of one bar
  4.  
  5. ! control line
  6. sub ctrl
  7. rline 0 -0.2
  8. rmove 2*STEP 0.2
  9. end sub
  10.  
  11. ! barcode with digits
  12. sub barcode bin$ dec$
  13. set lwidth STEP
  14. bl = len(bin$)+1
  15. i = 0
  16. while i < bl
  17. bc$ = seg$(bin$, i, i)
  18. if bc$ = "1" then
  19. rline 0 -LHG
  20. rmove STEP LHG
  21. else
  22. rmove STEP 0
  23. end if
  24. i = i + 1
  25. next
  26.  
  27. rmove -95*STEP -LHG
  28.  
  29. ctrl
  30. ctrl
  31. rmove 42*STEP 0
  32. ctrl
  33. ctrl
  34. rmove 42*STEP 0
  35. ctrl
  36. ctrl
  37.  
  38. rmove -96*STEP-0.3 -0.35
  39. set font ttb hei 0.4 just left
  40. p$ = left$(dec$,1)
  41. write p$
  42. rmove 16*STEP 0
  43. p$ = seg$(dec$, 2, 7)
  44. write p$
  45. rmove 46*STEP 0
  46. p$ = seg$(dec$, 8, 13)
  47. write p$
  48. rmove 40*STEP 0
  49. write ">"
  50. end sub
  51.  
  52. ! label
  53. begin object label bin$ dec$ prodname$
  54. set font pshb hei 0.62 just cc
  55. write prodname$
  56.  
  57. rmove -2 -0.8
  58. barcode bin$ dec$
  59. end object
  60.  
  61. ! begin main program
  62. args = nargs() ! take number of command line arguments
  63. if args < 3 then ! use example data
  64. bin$ = "10100011010010011011001100011010100111001101101010111001011100101100110111001011100101110100101"
  65. dec$ = "1021002001009"
  66. draw label bin$ dec$ "Product"
  67. else ! use data from command line
  68. draw label arg$(1) arg$(2) arg$(3)
  69. end if

Save the above content as label.gle. Next run:

  1. gle label.gle

Linux command for printing generted file:

  1. lpr label.eps

EAN13 barcode generated with GLE

Advantages

  • Template doesn't mess your business logic code.
  • PostScript is the standard for printing.
  • Vector format is scalable and generate smaller files.

I think this is interesting example of how a mid-size Open Source project can be used in commercial solution.

Leave a Response