Method: Escpos::Helpers#barcode

Defined in:
lib/escpos/helpers.rb

#barcode(data, opts = {}) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/escpos/helpers.rb', line 147

def barcode(data, opts = {})
  text_position = opts.fetch(:text_position, Escpos::BARCODE_TXT_OFF)
  possible_text_positions = [
    Escpos::BARCODE_TXT_OFF,
    Escpos::BARCODE_TXT_ABV,
    Escpos::BARCODE_TXT_BLW,
    Escpos::BARCODE_TXT_BTH
  ]
  unless possible_text_positions.include?(text_position)
    raise ArgumentError("Wrong text position.")
  end
  height = opts.fetch(:height, 50)
  if height && (height < 1 || height > 255)
    raise ArgumentError("Height must be in range from 1 to 255.")
  end
  width = opts.fetch(:width, 3)
  if width && (width < 2 || width > 6)
    raise ArgumentError("Width must be in range from 2 to 6.")
  end
  [
    Escpos.sequence(text_position),
    Escpos.sequence(Escpos::BARCODE_WIDTH),
    Escpos.sequence([width]),
    Escpos.sequence(Escpos::BARCODE_HEIGHT),
    Escpos.sequence([height]),
    Escpos.sequence(opts.fetch(:format, Escpos::BARCODE_EAN13)),
    data
  ].join
end