Class: BrotherEscp::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/brother_escp/document.rb

Overview

Main class to manage document Instanciate one for each printing

Instance Method Summary collapse

Constructor Details

#initialize(log_level: Logger::INFO) ⇒ Document

Returns a new instance of Document.

Parameters:

  • log_level (defaults to: Logger::INFO)

    set log level, default to info



12
13
14
15
16
17
18
# File 'lib/brother_escp/document.rb', line 12

def initialize(log_level: Logger::INFO)
  @data = String.new('')
  sequence(:HW_SET_ESCP_MODE)
  sequence(:HW_INIT)

  BrotherEscp.logger.level = log_level
end

Instance Method Details

#image(file_name:, density: :single_density) ⇒ Object

Add an image to the document This will only work for PNG files, the alpha layer will be ignored

Parameters:

  • file_name (String)

    file name of the image file

  • density (Symbol) (defaults to: :single_density)

    set the image density, default to single_density, possible values are: :single_density, :high_density, :higher_density



30
31
32
33
# File 'lib/brother_escp/document.rb', line 30

def image(file_name:, density: :single_density)
  img = BrotherEscp::Image.new(file_name: file_name, converter: density)
  write(img.convert.to_escp)
end

#inspectString

Inspect the data

Returns:

  • (String)

    the hexadecimal string representation of the data



55
56
57
# File 'lib/brother_escp/document.rb', line 55

def inspect
  to_escp.chars.map { |c| "0x#{c.unpack('H*').first}" }.join(' ')
end

#line_feed_size=(n) ⇒ Object

Helper method to set the line feed size

Parameters:

  • n (Integer)

    line feed size in dots



43
44
45
# File 'lib/brother_escp/document.rb', line 43

def line_feed_size=(n)
  write(BrotherEscp::Sequence.line_feed_size(n))
end

#page_length=(n) ⇒ Object

Helper method to set the page length

Parameters:

  • n (Integer)

    page length size in dots



49
50
51
# File 'lib/brother_escp/document.rb', line 49

def page_length=(n)
  write(BrotherEscp::Sequence.page_length(n))
end

#sequence(value) ⇒ Object

Add a pre-defined sequence to the document

See Also:

  • Sequence#sequence


37
38
39
# File 'lib/brother_escp/document.rb', line 37

def sequence(value)
  write(BrotherEscp::Sequence.sequence(value))
end

#to_base64String

Return base64 encoded data

Returns:

  • (String)

    the base64 encoded data



67
68
69
# File 'lib/brother_escp/document.rb', line 67

def to_base64
  Base64.strict_encode64 to_escp
end

#to_escpString

Return the raw data

Returns:

  • (String)

    the raw data



61
62
63
# File 'lib/brother_escp/document.rb', line 61

def to_escp
  @data
end

#write(data) ⇒ Object

Write data to the document

Parameters:

  • data (String)

    raw data to send to the printer



22
23
24
# File 'lib/brother_escp/document.rb', line 22

def write(data)
  @data << data
end