Class: Bread::Basket::Poster::HeaderMaker

Inherits:
Object
  • Object
show all
Defined in:
lib/bread/basket/poster/header_maker.rb

Constant Summary collapse

HEADER_DEFAULTS =
{ 'font-size' => 36,
  'color' => '000000',
  'margin-top' => 0,
  'margin-bottom' => 0,
  'radius' => 0,
  'background-color' => 'ffffff',
  'font-family' => 'Helvetica'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pdf, layout) ⇒ HeaderMaker



17
18
19
20
# File 'lib/bread/basket/poster/header_maker.rb', line 17

def initialize(pdf, layout)
  @pdf = pdf
  @layout = layout
end

Instance Attribute Details

#layoutObject (readonly)

Returns the value of attribute layout.



5
6
7
# File 'lib/bread/basket/poster/header_maker.rb', line 5

def layout
  @layout
end

#pdfObject (readonly)

Returns the value of attribute pdf.



5
6
7
# File 'lib/bread/basket/poster/header_maker.rb', line 5

def pdf
  @pdf
end

#stylesObject (readonly)

Returns the value of attribute styles.



5
6
7
# File 'lib/bread/basket/poster/header_maker.rb', line 5

def styles
  @styles
end

Instance Method Details

#bottom_marginObject



68
69
70
# File 'lib/bread/basket/poster/header_maker.rb', line 68

def bottom_margin
  styles['margin-bottom'] + styles['height']
end

#create_header(text, styles) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bread/basket/poster/header_maker.rb', line 22

def create_header(text, styles)
  @text = text
  @styles = init_styles(styles)
  @header_callback = Poster::HeaderCallback.new(pdf, @styles)

  pdf.move_down top_margin
  pdf.formatted_text_box text_box_arr, text_box_opts
  pdf.move_down bottom_margin

  text # don't return a Numeric, Redcarpet hates that!
end

#init_styles(styles) ⇒ Object



34
35
36
37
38
39
# File 'lib/bread/basket/poster/header_maker.rb', line 34

def init_styles(styles)
  styles = HEADER_DEFAULTS.merge layout_styles
  styles['width'] = layout.columns[0].width unless styles.key? 'width'
  styles['height'] = styles['font-size'] * 2 unless styles.key? 'height'
  styles
end

#layout_stylesObject



41
42
43
44
45
46
47
# File 'lib/bread/basket/poster/header_maker.rb', line 41

def layout_styles
  if layout.respond_to? :header
    layout.header
  else
    {}
  end
end

#text_box_arrObject



49
50
51
52
53
54
55
56
# File 'lib/bread/basket/poster/header_maker.rb', line 49

def text_box_arr
  [{ text: @text,
     callback: @header_callback,
     color: styles['color'].sub('#', ''),
     font:  styles['font-family'],
     size: styles['font-size']
    }]
end

#text_box_optsObject



58
59
60
61
62
# File 'lib/bread/basket/poster/header_maker.rb', line 58

def text_box_opts
  { at: [pdf.bounds.left, pdf.cursor],
    width: layout.columns[0].width,
    align: :center }
end

#top_marginObject



64
65
66
# File 'lib/bread/basket/poster/header_maker.rb', line 64

def top_margin
  styles['margin-top'] + (styles['height'] - styles['font-size']) / 2
end