' Gambas module file

PUBLIC SUB PrintBarcode(bcode AS String, posX AS Integer, posY AS Integer, bcHeight AS Integer, barThickness AS Integer, SorP AS Boolean)
  DIM structure AS String[10]
  'DIM enc AS String[10, 3]. I did it the following way 'cos I get not get the prg to read a multi-dimension array!
  DIM enc1 AS String[10]
  DIM enc2 AS String[10]
  DIM enc3 AS String[10]
  DIM fontSizeScreen AS Integer[10]
  DIM fontSizePrinter AS Integer[10]
  
  DIM first6 AS String ' encoding for first 6: l-code(1), g-code(2)
  DIM j AS Integer
  DIM l AS Integer
  DIM k AS Integer
  DIM frst AS Integer 'the first digit 
  DIM num AS Integer 'each digit in turn
  DIM bars AS String ' the bars for the digit
  DIM thkns AS Integer 'thickness of each bar
  DIM LinePos AS Integer 'used to horizontaly locate the next bar
  
  '
  structure = ["111111", "112122", "112212", "112221", "121122", "122112", "122211", "121212", "121221", "122121"] 
  enc1 = ["0001101", "0011001", "0010011", "0111101", "0100011", "0110001", "0101111", "0111011", "0110111", "0001011"]
  enc2 = ["0100111", "0110011", "0011011", "0100001", "0011101", "0111001", "0000101", "0010001", "0001001", "0010111"]
  enc3 = ["1110010", "1100110", "1101100", "1000010", "1011100", "1001110", "1010000", "1000100", "1001000", "1110100"]
  fontSizeScreen = [6, 9, 12, 16, 20, 24, 26, 28, 28, 30]
  fontSizePrinter = [2, 2, 2, 4, 4, 6, 6, 8, 8, 10]
  
  frst = Val(Left(bcode, 1))
  first6 = structure[frst]
  
  'draw 1st 2 deep bars-----------------------------------------------
  FOR l = 1 TO barThickness
    Draw.Line(l + posX, 1 + posY, l + posX, bcHeight * 1.2 + posY)
  NEXT
  LinePos = LinePos + barThickness * 2 'allows for following gap    
  FOR l = 1 TO barThickness
    Draw.Line(LinePos + l + posX, 1 + posY, LinePos + l + posX, bcHeight * 1.2 + posY)
  NEXT
  LinePos = LinePos + barThickness
  '---------------------------------------------------------------  
  FOR j = 2 TO 13 'loop through each of the 12 digits
    num = Val(Mid$(bcode, j, 1)) 'find the individual number IN the first 6 
    IF j < 8 THEN 'different for first 6
      IF Val(Mid(first6, j - 1, 1)) = 1 THEN 'find the bars making up that number (L,G or R)
        bars = enc1[num] 
      ELSE 
        bars = enc2[num]
      ENDIF 
    ELSE 
      bars = enc3[num]
    ENDIF
    
    'put the 2 middle deep bars----------------------------------------
    IF j = 8 THEN 
      LinePos = LinePos + barThickness
      FOR l = 1 TO barThickness
        Draw.Line(linepos + l + posX, 1 + posY, linepos + l + posX, bcHeight * 1.2 + posY)
      NEXT
      LinePos = LinePos + barThickness * 2 'allows for following gap    
      FOR l = 1 TO barThickness
        Draw.Line(LinePos + l + posX, 1 + posY, LinePos + l + posX, bcHeight * 1.2 + posY)
      NEXT
      LinePos = LinePos + barThickness * 2 'allows for following gap   
    ENDIF 
     '---------------------------------------------------------------   
    FOR k = 1 TO 7 'draw bars for single digit
     IF Mid(bars, k, 1) = "1" THEN
        FOR l = 1 TO barThickness
          Draw.Line(linepos + l + posX, 1 + posY, LinePos + l + posX, bcHeight + posY)
        NEXT 
     ENDIF 
     linepos = LinePos + barThickness
    NEXT
  NEXT   
    
  'draw last 2 deep bars------------------------------------------------
  FOR l = 1 TO barThickness
    Draw.Line(linepos + l + posX, 1 + posY, linepos + l + posX, bcHeight * 1.2 + posY)
  NEXT
  LinePos = LinePos + barThickness * 2 'allows for following gap    
  FOR l = 1 TO barThickness
    Draw.Line(LinePos + l + posX, 1 + posY, LinePos + l + posX, bcHeight * 1.2 + posY)
  NEXT
  '--------------------------------------------------------------- 
  'write the barcode text
  IF SorP THEN 
    Draw.Font.Size = fontSizeScreen[barThickness - 1]
  ELSE 
    Draw.Font.Size = fontSizePrinter[barThickness - 1]
  ENDIF 
  Draw.Text(Left(bcode, 1), posX - barThickness * 6, posY + bcHeight * 1.02)
  Draw.Text(Mid(bcode, 2, 6), posX + barThickness * 9, posY + bcHeight * 1.02)
  Draw.Text(Right(bcode, 6), posX + barThickness * 58, posY + bcHeight * 1.02)
  
  
END
