Sha256: 7ebddb83a8af66c5a6fcbc608314e2211da699a7cd1116ed93484434fa7a97a5

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

module Rescpos
  module ReportUtil
    FONT_NORMAL = "\x00"
    FONT_BIG = "\x11"

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

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

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

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

    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 << txt if txt
    end
    
    def fontsize(size)
      "\x1d\x21" << size.to_s
    end

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

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

    def align(format)
      if format == 'C'
        return "\x1b\x61\x01"
      elsif format == 'L'
        return "\x1b\x61\x00"
      elsif format == 'R'
        return "\x1b\x61\x02"
      end
    end
    
    def table(positions)
      command = "\x1b\x44"
      for position in positions
        command << ascii(position)
      end
      command << "\x00"
    end

    def horizontal_tab
      "\x09"
    end
    
    def ascii(value)
      value.to_s.unpack('U')[0].to_s(16)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rescpos-0.0.2 lib/rescpos/report_util.rb
rescpos-0.0.1 lib/rescpos/report_util.rb