Class: Escper::Asciifier

Inherits:
Object
  • Object
show all
Defined in:
lib/escper/asciifier.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(codepage = 0) ⇒ Asciifier

Returns a new instance of Asciifier.



3
4
5
6
# File 'lib/escper/asciifier.rb', line 3

def initialize(codepage=0)
  @codepage = codepage
  @codepage_lookup_yaml = YAML::load(File.read(Escper.codepage_file))
end

Class Method Details

.all_charsObject



8
9
10
11
12
13
# File 'lib/escper/asciifier.rb', line 8

def self.all_chars
  out = "\e@" # Initialize Printer
  out.encode!('ASCII-8BIT')
  33.upto(255) { |i| out += i.to_s(16) + i.chr + ' ' }
  return out
end

Instance Method Details

#codepage_lookup(char) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/escper/asciifier.rb', line 30

def codepage_lookup(char)
  return '*'.force_encoding('ASCII-8BIT') unless @codepage_lookup_yaml[@codepage]
  
  output = @codepage_lookup_yaml[@codepage][char]
  if output
    output = output.chr
  else
    output = '?'
  end
  return output.force_encoding('ASCII-8BIT')
end

#process(text) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/escper/asciifier.rb', line 15

def process(text)
  output = ''
  output.encode 'ASCII-8BIT'
  0.upto(text.length - 1) do |i|
    char_utf8 = text[i]
    char_ascii = text[i].force_encoding('ASCII-8BIT')
    if char_ascii.length == 1
      output += char_ascii
    else
      output += codepage_lookup(char_utf8)
    end      
  end
  return output
end