Class: JekyllPandocMultipleFormats::Imposition

Inherits:
Printer
  • Object
show all
Defined in:
lib/jekyll-pandoc-multiple-formats/imposition.rb

Constant Summary

Constants inherited from Printer

Printer::SHEET_SIZES, Printer::TEMPLATE

Instance Attribute Summary collapse

Attributes inherited from Printer

#extra_options, #nup, #original_file, #output_file, #pages, #papersize, #relative_path, #sheetsize, #template

Instance Method Summary collapse

Methods inherited from Printer

#is_landscape?, #render_template, #round_to_nearest, #write

Constructor Details

#initialize(file, papersize = nil, sheetsize = nil, signature = nil, extra_options = nil) ⇒ Imposition

Returns a new instance of Imposition.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jekyll-pandoc-multiple-formats/imposition.rb', line 29

def initialize(file, papersize = nil, sheetsize = nil, signature = nil, extra_options = nil)
  super(file, papersize, sheetsize, extra_options)
  @output_file   = file.gsub(/\.pdf\Z/, '-imposed.pdf')
  # Total pages must be modulo 4
  @rounded_pages = round_to_nearest(@pages, 4)
  @blank_pages   = @rounded_pages - @pages
  # If we don't use a signature, make a single fold
  @signature     = signature || @rounded_pages
  # Also if we specified 0
  @signature     = @rounded_pages if signature == 0

  render_template
  self
end

Instance Attribute Details

#blank_pagesObject

Returns the value of attribute blank_pages.



27
28
29
# File 'lib/jekyll-pandoc-multiple-formats/imposition.rb', line 27

def blank_pages
  @blank_pages
end

#rounded_pagesObject

Returns the value of attribute rounded_pages.



27
28
29
# File 'lib/jekyll-pandoc-multiple-formats/imposition.rb', line 27

def rounded_pages
  @rounded_pages
end

#signatureObject

Returns the value of attribute signature.



27
28
29
# File 'lib/jekyll-pandoc-multiple-formats/imposition.rb', line 27

def signature
  @signature
end

Instance Method Details

#to_nupObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jekyll-pandoc-multiple-formats/imposition.rb', line 44

def to_nup
  # 14 pages example:
  # [ {}, 1, 2, {}, 14, 3, 4, 13, 12, 5, 6, 11, 10, 7, 8, 9 ]
  #
  # * Add {} to missing pages
  #   [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, {}, {}]
  # * Take first half [ 1, 2, 3, 4, 5, 6, 7, 8 ]
  # * Reverse second half [ {}, {}, 14, 13, 12, 11, 10, 9 ]
  # * Intercalate the first half into the second half by two
  #   elements
  #   [ {}, 1, 2, {}, 14, 3, 4, 13, 12, 5, 6, 11, 10, 7, 8, 9 ]
  #
  # An array of numbered pages padded with blank pages ('{}')
  #
  # [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ] + [ '{}', '{}' ]
  padded = @pages.times.map{|i|i+1} + Array.new(@blank_pages, '{}')

  # If we have a signature, we have to split in groups up to the
  # amount of pages per signature, and then continue with the rest
  #
  # If we have no signature, we assume it's equal to the total
  # amount of pages, so you only have one fold
  signed = padded.each_slice(@signature).to_a
  folds = []
  signed.each do |fold|
    #
    # Split in halves
    # [ [ 1, 2, 3, 4, 5, 6, 7, 8 ],
    #   [ 9, 10, 11, 12, 13, 14, '{}', '{}' ] ]
    halved = fold.each_slice(fold.size / 2).to_a
    # Add a nil as last page.  When we reverse it and intercalate by
    # two pages, we'll have [nil, last_page] instead of
    # [last_page,second_to_last_page]
    #
    # [ [ 1, 2, 3, 4, 5, 6, 7, 8 ],
    #   [ 9, 10, 11, 12, 13, 14, '{}', '{}', nil ] ]
    halved.last << nil
    # Reverse the second half and intercalate by two pages into the
    # first one.  Then remove nil elements and flatten the array.
    #
    # [ [ 1, 2, 3, 4, 5, 6, 7, 8 ],
    #   [ nil, '{}', '{}', 14, 13, 12, 11, 10 ] ]
    #
    # [ {}, 1, 2, {}, 14, 3, 4, 13, 12, 5, 6, 11, 10, 7, 8, 9 ]
    folds << halved.last.reverse.each_slice(2).zip(halved.first.each_slice(2).to_a).flatten.compact
  end

  # Create the matrix of pages (N-Up) per fold
  #
  # ["{}", 1, "{}", 1, 2, "{}", 2, "{}", 14, 3, 14, 3, 4, 13, 4, 13, 12, 5, 12, 5, 6, 11, 6, 11, 10, 7, 10, 7, 8, 9, 8, 9]
  folds.map { |o| o.each_slice(2).map{ |i| a=[]; (@nup/2).times { a = a+i }; a }}.flatten
end