Class: PDF::Impose::Form

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/impose/form.rb

Overview

A Form represents a collection of pages that will be printed together on a single sheet of paper, in a particular (grid) layout.

Form subclasses define the layout of individual pages via the ‘recto` and `verso` methods (`recto` == front, `verso` == back).

class Quarto < Form
  per_signature 4

  recto %w(3* *3),
        %w(0. .0)

  verso %w(1* *1),
        %w(2. .2)
end

Each row of the recto/verso configuration is a series of cells, representing individual pages. They take the following format:

*n OR .n OR n* OR n.

‘n’ is which pair from the signature is to be set in this position. If the dot or asterisk comes before the number, then the first element of the pair is used (the lower page number). If the dot or asterisk comes after, then the last element of the pair is used (the higher page number).

A ‘.’ means the page is set without rotation. A ‘*’ means the page is rotated 180 degrees to turn it upside down.

Defined Under Namespace

Classes: LayoutElement

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pages) ⇒ Form

Returns a new instance of Form.



139
140
141
# File 'lib/pdf/impose/form.rb', line 139

def initialize(pages)
  @pages = pages
end

Class Attribute Details

.columns_per_formObject (readonly)

Returns the value of attribute columns_per_form.



36
37
38
# File 'lib/pdf/impose/form.rb', line 36

def columns_per_form
  @columns_per_form
end

.rows_per_formObject (readonly)

Returns the value of attribute rows_per_form.



36
37
38
# File 'lib/pdf/impose/form.rb', line 36

def rows_per_form
  @rows_per_form
end

Instance Attribute Details

#pagesObject (readonly)

Returns the value of attribute pages.



137
138
139
# File 'lib/pdf/impose/form.rb', line 137

def pages
  @pages
end

Class Method Details

._parse_configuration(rows) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/pdf/impose/form.rb', line 109

def _parse_configuration(rows)
  @rows_per_form = rows.length
  @columns_per_form = rows[0].length

  [].tap do |config|
    rows.each.with_index do |columns, row|
      columns.each.with_index do |cell, column|
        if cell !~ /^\s*([*.])?(\d+)([*.])?\s*$/
          raise "invalid form specification: #{cell.inspect}"
        end

        first = $1
        ofs   = $2.to_i
        last  = $3

        if first.nil? && last.nil?
          raise "invalid form specification: #{cell.inspect}"
        end

        flip = (first || last) == '*'
        config << LayoutElement.new(column, row, ofs,
                                    first ? :first : :last, flip)
      end
    end
  end
end

.cut_col(*cols) ⇒ Object



50
51
52
# File 'lib/pdf/impose/form.rb', line 50

def cut_col(*cols)
  @cut_col = cols.any? ? cols : @cut_col
end

.cut_row(*rows) ⇒ Object



46
47
48
# File 'lib/pdf/impose/form.rb', line 46

def cut_row(*rows)
  @cut_row = rows.any? ? rows : @cut_row
end

.layout(method = nil) ⇒ Object

method can be :pages or :signatures (the default), and determines what the numbers in the recto/verso layouts represent.



56
57
58
# File 'lib/pdf/impose/form.rb', line 56

def layout(method = nil)
  @layout = method || @layout
end

.layout_signatures(signatures) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pdf/impose/form.rb', line 76

def layout_signatures(signatures)
  l = layout || :signatures
  signatures.each do |signature|
    offset = 0
    while offset < signature.pairs.length
      [recto, verso].compact.each do |side|
        pages = []

        side.each do |element|
          n = if l == :signatures
                idx = offset + element.offset
                signature.pairs[idx].send(element.which)
              else
                signature.pairs[offset].first + element.offset - 1
              end

          pages << PDF::Impose::Page.new(element.column, element.row,
                                         n, element.flip)
        end

        yield self.new(pages)
      end

      offset += pages_per_form
    end
  end

  self
end

.pages_per_formObject



42
43
44
# File 'lib/pdf/impose/form.rb', line 42

def pages_per_form
  rows_per_form * columns_per_form
end

.per_signature(n = nil) ⇒ Object



38
39
40
# File 'lib/pdf/impose/form.rb', line 38

def per_signature(n = nil)
  @per_signature = n || @per_signature
end

.recto(*rows) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/pdf/impose/form.rb', line 60

def recto(*rows)
  if rows.any?
    @recto = _parse_configuration(rows)
  else
    @recto
  end
end

.verso(*rows) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/pdf/impose/form.rb', line 68

def verso(*rows)
  if rows.any?
    @verso = _parse_configuration(rows)
  else
    @verso
  end
end

Instance Method Details

#each_pageObject



143
144
145
146
# File 'lib/pdf/impose/form.rb', line 143

def each_page
  @pages.each { |page| yield page }
  self
end