Class: RubyEscPos

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-escpos.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRubyEscPos

Returns a new instance of RubyEscPos.



4
5
6
# File 'lib/ruby-escpos.rb', line 4

def initialize
  @buffer = ''
end

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer.



2
3
4
# File 'lib/ruby-escpos.rb', line 2

def buffer
  @buffer
end

Instance Method Details

#barcode(code, width = 64, height = 64, bc = BARCODE_UPC_A, pos = BARCODE_TXT_OFF, font = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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) ⇒ Object



107
108
109
110
# 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) ⇒ Object



8
9
10
# 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) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# 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) ⇒ Object



99
100
101
102
103
104
105
# 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