Class: Quadtone::Printer

Inherits:
Object
  • Object
show all
Defined in:
lib/quadtone/printer.rb

Constant Summary collapse

ImportantOptions =
%i{
  MediaType
  Resolution
  ripBlack
  ripSpeed
  stpDither
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Printer

Returns a new instance of Printer.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/quadtone/printer.rb', line 18

def initialize(name)
  @name = name
  @cups_ppd = CupsPPD.new(@name, nil)
  @options = Hash[
    @cups_ppd.options.map { |o| [o.delete(:keyword).to_sym, HashStruct.new(o)] }
  ]
  @attributes = Hash[
    @cups_ppd.attributes.map { |a| [a.delete(:name).to_sym, HashStruct.new(a)] }
  ]
  @cups_printer = CupsPrinter.new(@name)
  get_inks
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



15
16
17
# File 'lib/quadtone/printer.rb', line 15

def attributes
  @attributes
end

#inksObject

Returns the value of attribute inks.



16
17
18
# File 'lib/quadtone/printer.rb', line 16

def inks
  @inks
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/quadtone/printer.rb', line 13

def name
  @name
end

#optionsObject

Returns the value of attribute options.



14
15
16
# File 'lib/quadtone/printer.rb', line 14

def options
  @options
end

Instance Method Details

#default_optionsObject



58
59
60
61
62
63
64
# File 'lib/quadtone/printer.rb', line 58

def default_options
  Hash[
    @options.map do |name, option|
      [name, option.default_choice]
    end
  ]
end

#get_inksObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/quadtone/printer.rb', line 35

def get_inks
  # FIXME: It would be nice to get this path programmatically.
  ppd_file = Path.new("/etc/cups/ppd/#{@name}.ppd")
  ppd_file.readlines.each do |line|
    if line =~ /^\*%Inks\s*(.*?)\s*$/
      @inks = $1.split(/,/).map(&:downcase).map(&:to_sym)
      break
    end
  end
end

#page_size(name = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/quadtone/printer.rb', line 46

def page_size(name=nil)
  name ||= @cups_ppd.attribute('DefaultPageSize').first[:value]
  page_size = @cups_ppd.page_size(name) or raise "Can't determine page size #{name.inspect}"
  size = HashStruct.new(page_size)
  # change 'length' to 'height', or else there are problems with Hash#length
  size[:height] = size.delete(:length)
  size = HashStruct.new(size)
  size.imageable_width = size.margin.right - size.margin.left
  size.imageable_height = size.margin.top - size.margin.bottom
  size
end


95
96
97
98
99
100
101
102
# File 'lib/quadtone/printer.rb', line 95

def print_file(path, options)
  warn "Printing #{path}"
  warn "Options:"
  options.each do |key, value|
    warn "\t" + "%10s: %s" % [key, value]
  end
  @cups_printer.print_file(path, options)
end

#quadtone?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/quadtone/printer.rb', line 31

def quadtone?
  @attributes[:Manufacturer].value == 'QuadToneRIP'
end

#show_attributesObject



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/quadtone/printer.rb', line 66

def show_attributes
  puts "Attributes:"
  max_field_length = @attributes.keys.map(&:length).max
  @attributes.sort_by { |name, info| name }.each do |name, attribute|
    puts "\t" + "%#{max_field_length}s: %s%s" % [
      name,
      attribute.value.inspect,
      attribute.spec.empty? ? '' : " [#{attribute.spec.inspect}]"
    ]
  end
end

#show_inksObject



90
91
92
93
# File 'lib/quadtone/printer.rb', line 90

def show_inks
  puts "Inks:"
  puts "\t" + @inks.map { |ink| ink.to_s.upcase }.join(', ')
end

#show_optionsObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/quadtone/printer.rb', line 78

def show_options
  puts "Options:"
  max_field_length = @options.keys.map(&:length).max
  @options.sort_by { |name, option| name }.each do |name, option|
    puts "\t" + "%#{max_field_length}s: %s [%s]" % [
      name,
      option.default_choice.inspect,
      (option.choices.map { |o| o.choice } - [option.default_choice]).map { |o| o.inspect }.join(', ')
    ]
  end
end