Module: Rescpos::ReportUtil

Included in:
Report
Defined in:
lib/rescpos/report_util.rb

Constant Summary collapse

FONT_NORMAL =
"\x00"
FONT_BIG =
"\x11"
ALIGN_C =
"\x01"
ALIGN_L =
"\x00"
ALIGN_R =
"\x02"

Instance Method Summary collapse

Instance Method Details

#align(type) ⇒ Object



47
48
49
# File 'lib/rescpos/report_util.rb', line 47

def align(type)
  "\x1b\x61" << type.to_s
end

#ascii(value) ⇒ Object



90
91
92
# File 'lib/rescpos/report_util.rb', line 90

def ascii(value)
  value.to_s.unpack('U')[0].to_s(16)
end

#chinese(chinese) ⇒ Object



21
22
23
24
# File 'lib/rescpos/report_util.rb', line 21

def chinese(chinese)
  text = Iconv.iconv("GBK//IGNORE","UTF-8//IGNORE",chinese)
  text[0]
end

#double_splitlineObject



13
14
15
# File 'lib/rescpos/report_util.rb', line 13

def double_splitline
  text("=" * 42, :font_size => FONT_NORMAL)
end

#fontsize(size) ⇒ Object



35
36
37
# File 'lib/rescpos/report_util.rb', line 35

def fontsize(size)
  "\x1d\x21" << size.to_s
end

#grayscale(value) ⇒ Object



39
40
41
# File 'lib/rescpos/report_util.rb', line 39

def grayscale(value)
  "\x1b\x6d" << ascii(value)
end

#horizontal_tabObject



86
87
88
# File 'lib/rescpos/report_util.rb', line 86

def horizontal_tab
  "\x09"
end

#key_value(label, value) ⇒ Object



43
44
45
# File 'lib/rescpos/report_util.rb', line 43

def key_value(label, value)
  "#{label}: #{value}"
end

#single_splitlineObject



9
10
11
# File 'lib/rescpos/report_util.rb', line 9

def single_splitline
  text("-" * 42, :font_size => FONT_NORMAL)
end

#table(data) {|table| ... } ⇒ Object

Yields:



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
79
80
81
82
83
84
# File 'lib/rescpos/report_util.rb', line 51

def table(data)
  table = Rescpos::Table.new(data)
  yield table
  # FIXME move the following logic into Rescpos::Table
  command = "\x1b\x44"
  table.positions.each do |position|
    command << position.chr
  end
  command << "\x00"
  if table.headers
    table.headers.each do |header|
      command << header << "\x09" 
    end
    command << "\n"
  end
  table.data.each do |item|
    if item.is_a? Array
      command = command + item.join("\x09") + (table.data.last == item ? "" : "\n")
    else
      table.keys.each do |key|
        if item.is_a? Hash
          command << "#{item[key]}\x09"
        else
          command << "#{item.send(key)}\x09"
        end
      end
      if table.data.last == item
        return command
      end
      command << "\n"
    end
  end
  command
end

#text(txt, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/rescpos/report_util.rb', line 26

def text(txt, options = {}) 
  font_size = options[:font_size] || FONT_NORMAL
  formatted_text = ''
  formatted_text << fontsize(font_size)
  formatted_text << grayscale(options[:gray]) if options[:gray]
  formatted_text << align(options[:align_type]) if options[:align_type]
  formatted_text << txt if txt
end

#underline(number) ⇒ Object



17
18
19
# File 'lib/rescpos/report_util.rb', line 17

def underline(number)
  text("_" * number, :font_size => FONT_NORMAL)
end