Class: CupsPPD

Inherits:
Object
  • Object
show all
Defined in:
lib/cupsffi/ppd.rb

Overview

The MIT License

Copyright © 2011 Nathan Ehresman

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Instance Method Summary collapse

Constructor Details

#initialize(printer_name, connection) ⇒ CupsPPD

Returns a new instance of CupsPPD.



24
25
26
27
28
29
30
31
32
# File 'lib/cupsffi/ppd.rb', line 24

def initialize(printer_name, connection)
  @file = CupsFFI::cupsGetPPD2(connection, printer_name)
  raise "No PPD found for #{printer_name}" if @file.nil?

  @pointer = CupsFFI::ppdOpenFile(@file)
  raise "Unable to open PPD #{file}" if @pointer.null?

  @ppd_file_s = CupsFFI::PPDFileS.new(@pointer)
end

Instance Method Details

#attribute(name, spec = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cupsffi/ppd.rb', line 84

def attribute(name, spec=nil)
  attributes = []
  attribute_pointer = CupsFFI::ppdFindAttr(@pointer, name, spec)
  while !attribute_pointer.null?
    attribute = CupsFFI::PPDAttrS.new(attribute_pointer)
    attributes.push({
      :name => String.new(attribute[:name]),
      :spec => String.new(attribute[:spec]),
      :text => String.new(attribute[:text]),
      :value => String.new(attribute[:value]),
    })

    attribute_pointer = CupsFFI::ppdFindNextAttr(@pointer, name, spec)
  end
  attributes
end

#attributesObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cupsffi/ppd.rb', line 67

def attributes
  attributes = []
  attr_ptr = @ppd_file_s[:attrs].read_pointer
  if !attr_ptr.null?
    @ppd_file_s[:attrs].get_array_of_pointer(0, @ppd_file_s[:num_attrs]).each do |attr_ptr|
      attribute = CupsFFI::PPDAttrS.new(attr_ptr)
      attributes.push({
        :name => String.new(attribute[:name]),
        :spec => String.new(attribute[:spec]),
        :text => String.new(attribute[:text]),
        :value => String.new(attribute[:value]),
      })
    end
  end
  attributes
end

#closeObject



34
35
36
37
# File 'lib/cupsffi/ppd.rb', line 34

def close
  CupsFFI::ppdClose(@pointer)
  File.unlink(@file)
end

#optionsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cupsffi/ppd.rb', line 39

def options
  options = []
  option_pointer = CupsFFI::ppdFirstOption(@pointer)
  while !option_pointer.null?
    option = CupsFFI::PPDOptionS.new(option_pointer)
    choices = []
    option[:num_choices].times do |i|
      choice = CupsFFI::PPDChoiceS.new(option[:choices] + (CupsFFI::PPDChoiceS.size * i))
      choices.push({
        :text => String.new(choice[:text]),
        :choice => String.new(choice[:choice])
      })
    end
    options.push({
      :keyword => String.new(option[:keyword]),
      :default_choice => String.new(option[:defchoice]),
      :text => String.new(option[:text]),
      :ui => String.new(option[:ui].to_s),
      :section => String.new(option[:section].to_s),
      :order => option[:order],
      :choices => choices
    })

    option_pointer = CupsFFI::ppdNextOption(@pointer)
  end
  options
end

#page_size(name = nil) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cupsffi/ppd.rb', line 101

def page_size(name=nil)
  size_ptr = CupsFFI::ppdPageSize(@pointer, name)
  size = CupsFFI::PPDSizeS.new(size_ptr)
  if size.null?
    nil
  else
    {
      :marked => (size[:marked] != 0),
      :name => String.new(size[:name]),
      :width => size[:width],
      :length => size[:length],
      :margin => {
        :left => size[:left],
        :bottom => size[:bottom],
        :right => size[:right],
        :top => size[:top]
      }
    }
  end
end