class RubyEscPos

Constants

BARCODE_CODE39
BARCODE_EAN13
BARCODE_EAN8
BARCODE_FONT_A
BARCODE_FONT_B
BARCODE_HEIGHT
BARCODE_ITF
BARCODE_NW7
BARCODE_TXT_ABV
BARCODE_TXT_BLW
BARCODE_TXT_BTH
BARCODE_TXT_OFF

Barcode format

BARCODE_UPC_A
BARCODE_UPC_E
BARCODE_WIDTH
CD_KICK_2

Cash Drawer

CD_KICK_5
CTL_CR
CTL_FF
CTL_HT
CTL_LF

Feed control sequences

CTL_VT
FONT_TYPE_BOLD

Font types

FONT_TYPE_BOLD_UNDERLINE
FONT_TYPE_BOLD_UNDERLINE_2
FONT_TYPE_NORMAL
FONT_TYPE_UNDERLINE
FONT_TYPE_UNDERLINE_2
HW_INIT

Printer hardware

HW_RESET
HW_SELECT
PAPER_FULL_CUT

Paper

PAPER_PART_CUT
STYLE_HEADER

Styles

STYLE_NORMAL
STYLE_SUBHEADER
TXT_2HEIGHT
TXT_2WIDTH
TXT_4SQUARE
TXT_ALIGN_CT
TXT_ALIGN_LT
TXT_ALIGN_RT
TXT_BOLD_OFF
TXT_BOLD_ON
TXT_FONT_A
TXT_FONT_B
TXT_NORMAL

Text format

TXT_UNDERL2_ON
TXT_UNDERL_OFF
TXT_UNDERL_ON

Attributes

buffer[RW]

Public Class Methods

new() click to toggle source
# File lib/ruby-escpos.rb, line 4
def initialize
  @buffer = ''
end

Public Instance Methods

barcode(code, width = 64, height = 64, bc = BARCODE_UPC_A, pos = BARCODE_TXT_OFF, font = nil) click to toggle source
# File lib/ruby-escpos.rb, line 80
def barcode(code, width = 64, height = 64, bc = BARCODE_UPC_A, pos = BARCODE_TXT_OFF, font = nil)
  write TXT_ALIGN_CT

  if height >= 2 && height <= 6
    write BARCODE_HEIGHT
  end

  if width >= 1 && width <=255
    write BARCODE_WIDTH
  end

  write font
  write pos

  write bc
  write code
  new_line
end
cut(cut_type = PAPER_FULL_CUT) click to toggle source
# File lib/ruby-escpos.rb, line 107
def cut(cut_type = PAPER_FULL_CUT)
  write "\n\n\n\n\n\n"
  write cut_type
end
new_line(lines = 1) click to toggle source
# File lib/ruby-escpos.rb, line 8
def new_line(lines = 1)
  text nil, lines
end
set(align = TXT_ALIGN_LT, font = TXT_FONT_A, font_type = FONT_TYPE_NORMAL, width = 1, height = 1, density = nil) click to toggle source
# File lib/ruby-escpos.rb, line 40
def set(align = TXT_ALIGN_LT, font = TXT_FONT_A, font_type = FONT_TYPE_NORMAL, width = 1, height = 1, density = nil)
  write align

  if height == 2 && width == 2
    write TXT_NORMAL
    write TXT_4SQUARE
  elsif height == 2 && width != 2
    write TXT_NORMAL
    write TXT_2HEIGHT
  elsif width == 2 && height != 2
    write TXT_NORMAL
    write TXT_2WIDTH
  else
    write TXT_NORMAL
  end

  if font_type == FONT_TYPE_BOLD
    write TXT_BOLD_ON
    write TXT_UNDERL_OFF
  elsif font_type == FONT_TYPE_UNDERLINE
    write TXT_BOLD_OFF
    write TXT_UNDERL_ON
  elsif font_type == FONT_TYPE_UNDERLINE_2
    write TXT_BOLD_OFF
    write TXT_UNDERL2_ON
  elsif font_type == FONT_TYPE_BOLD_UNDERLINE
    write TXT_BOLD_ON
    write TXT_UNDERL_ON
  elsif font_type == FONT_TYPE_BOLD_UNDERLINE_2
    write TXT_BOLD_ON
    write TXT_UNDERL2_ON
  else
    write TXT_BOLD_OFF
    write TXT_UNDERL_OFF
  end

  write font
  write density if density
end
style(style, align) click to toggle source
# File lib/ruby-escpos.rb, line 12
def style(style, align)
  if style == :header
    style = STYLE_HEADER
  elsif style == :subheader
    style = STYLE_SUBHEADER
  elsif style == :normal
    style = STYLE_NORMAL
  elsif style == :footer
    style = STYLE_FOOTER
  end

  if align == :left
    align = TXT_ALIGN_LT
  elsif align == :center
    align = TXT_ALIGN_CT
  elsif align == :right
    align = TXT_ALIGN_RT
  end

  set(
    align,
    style[:font],
    style[:font_type],
    style[:width],
    style[:height]
  )
end
table(rows, new_lines = 1, main_column_index = 1, max_width = 42) click to toggle source
# File lib/ruby-escpos.rb, line 112
def table(rows, new_lines = 1, main_column_index = 1, max_width = 42)
  max_widths = Array.new(rows[0].count) { |i| 0 }

  rows.each_with_index do |row, i|
    row.each_with_index do |cell, y|
      if y < row.length - 1
        rows[i][y][:text] += ' '
      end

      if max_widths[y] < cell[:text].length
        max_widths[y] = cell[:text].length
      end

      if max_widths[y] < cell[:width].to_i
        max_widths[y] = cell[:width]
      end

      if cell[:text].length < cell[:width].to_i
        cell[:text] = (' ' * (cell[:width] - cell[:text].length)) + cell[:text]
      end
    end
  end

  rows.each_with_index do |row, i|
    row.each_with_index do |cell, y|
      padding = ''
      if y == main_column_index
        padding = (' ' * (max_width - row.map { |x| x[:text].length }.inject(0, :+)))
      elsif y < row.length - 1
        padding = (' ' * (max_widths[y] - cell[:text].length))
      end

      if cell[:align]
        write(cell[:text] = padding + cell[:text])
      else
        write(cell[:text] = cell[:text] + padding)
      end
    end
    new_line
  end

  new_line new_lines - 1
end
text(txt, new_lines = 1) click to toggle source
# File lib/ruby-escpos.rb, line 99
def text(txt, new_lines = 1)
  @buffer += txt if txt

  (1..new_lines).each do
    @buffer += "\n"
  end
end

Private Instance Methods

write(data) click to toggle source
# File lib/ruby-escpos.rb, line 158
def write(data)
  if data.is_a? String
    @buffer += data
  elsif data.is_a? Array
    @buffer += data.pack('U*')
  end
end