Class: Laser::Cutter::PageManager

Inherits:
Object
  • Object
show all
Defined in:
lib/laser/cutter/page_manager.rb

Overview

Lists the page sizes a PDF can take, in inches or millimetres.

Constant Summary collapse

SIZES =

The page sizes Prawn knows, in PDF points, by name.

PDF::Core::PageGeometry::SIZES.clone.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(units) ⇒ PageManager

Returns a new instance of PageManager.

Parameters:

  • units (String, Symbol) —

    'in' or 'mm'; the configuration's default is a Symbol



14
15
16
# File 'lib/laser/cutter/page_manager.rb', line 14

def initialize(units)
  @units = units.to_s
end

Instance Attribute Details

#units ⇒ String (readonly)

Returns "in" or "mm".

Returns:

  • (String) —

    "in" or "mm"



11
12
13
# File 'lib/laser/cutter/page_manager.rb', line 11

def units
  @units
end

Instance Method Details

#all_page_sizes ⇒ String

Returns one line for each page size, with its width and height.

Returns:

  • (String) —

    one line for each page size, with its width and height



19
20
21
22
23
24
25
# File 'lib/laser/cutter/page_manager.rb', line 19

def all_page_sizes
  output = +""
  page_size_values.each do |k|
    output << sprintf("\t%10s:\t%6.1f x %6.1f\n", *k)
  end
  output
end

#page_size_values ⇒ Array<Array(String, Float, Float)>

Returns each page size, sorted by name, with its width and height in #units.

Returns:

  • (Array<Array(String, Float, Float)>) —

    each page size, sorted by name, with its width and height in #units



52
53
54
55
56
57
# File 'lib/laser/cutter/page_manager.rb', line 52

def page_size_values
  h = Hashie::Mash.new(SIZES)
  h.keys.sort.map do |k|
    [k, value_from_units(h[k][0].to_f), value_from_units(h[k][1].to_f)]
  end
end

#value_from_units(value, from_units = nil) ⇒ Float

Converts a length into the units of this page manager.

Parameters:

  • value (Numeric) —

    the length

  • from_units (String, nil) (defaults to: nil) —

    "in" or "mm"; nil for PDF points, 72 to the inch, which is what Prawn measures in

Returns:

  • (Float)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/laser/cutter/page_manager.rb', line 33

def value_from_units(value, from_units = nil)
  multiplier = if from_units.nil?
                 if units.eql?('in')
                   1.0 / 72.0 # PDF units per inch
                 else
                   25.4 * 1.0 / 72.0
                 end
               elsif units.eql?(from_units)
                 1.0
               elsif units.eql?('in') && from_units.eql?('mm')
                 (1.0 / 25.4)
               else
                 25.4
               end
  value.to_f * multiplier
end