Class: Pdfrb::Layout::PageStyle

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/layout/page_style.rb

Overview

A named page template. A PageStyle knows:

  • page dimensions (size + orientation)
  • margin
  • the next page style (chains)
  • a block that draws page chrome (header/footer) each time a new page with this style is created.

Constant Summary collapse

PAGE_SIZES =
{
  A4: [595.28, 841.89],
  A3: [841.89, 1190.55],
  A5: [419.53, 595.28],
  Letter: [612.0, 792.0],
  Legal: [612.0, 1008.0],
  Tabloid: [792.0, 1224.0],
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, page_size: :A4, orientation: :portrait, margin: 36, next_style: nil, &block) ⇒ PageStyle

Returns a new instance of PageStyle.



23
24
25
26
27
28
29
30
31
# File 'lib/pdfrb/layout/page_style.rb', line 23

def initialize(name:, page_size: :A4, orientation: :portrait,
               margin: 36, next_style: nil, &block)
  @name = name
  @page_size = page_size
  @orientation = orientation
  @margin = margin
  @next_style = next_style || name
  @block = block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



12
13
14
# File 'lib/pdfrb/layout/page_style.rb', line 12

def block
  @block
end

#marginObject (readonly)

Returns the value of attribute margin.



12
13
14
# File 'lib/pdfrb/layout/page_style.rb', line 12

def margin
  @margin
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/pdfrb/layout/page_style.rb', line 12

def name
  @name
end

#next_styleObject (readonly)

Returns the value of attribute next_style.



12
13
14
# File 'lib/pdfrb/layout/page_style.rb', line 12

def next_style
  @next_style
end

#orientationObject (readonly)

Returns the value of attribute orientation.



12
13
14
# File 'lib/pdfrb/layout/page_style.rb', line 12

def orientation
  @orientation
end

#page_sizeObject (readonly)

Returns the value of attribute page_size.



12
13
14
# File 'lib/pdfrb/layout/page_style.rb', line 12

def page_size
  @page_size
end

Instance Method Details

#dimensionsObject



33
34
35
36
37
38
39
40
# File 'lib/pdfrb/layout/page_style.rb', line 33

def dimensions
  base = PAGE_SIZES[@page_size] || [612.0, 792.0]
  if @orientation == :landscape
    [base[1], base[0]]
  else
    base
  end
end

#draw_chrome(canvas) ⇒ Object



60
61
62
# File 'lib/pdfrb/layout/page_style.rb', line 60

def draw_chrome(canvas)
  @block&.call(canvas)
end

#frameObject



50
51
52
53
54
55
56
57
58
# File 'lib/pdfrb/layout/page_style.rb', line 50

def frame
  m = @margin.is_a?(Array) ? @margin : [@margin] * 4
  Frame.new(
    left: m[3],
    bottom: m[2],
    width: width - m[1] - m[3],
    height: height - m[0] - m[2]
  )
end

#heightObject



46
47
48
# File 'lib/pdfrb/layout/page_style.rb', line 46

def height
  dimensions[1]
end

#widthObject



42
43
44
# File 'lib/pdfrb/layout/page_style.rb', line 42

def width
  dimensions[0]
end