class Elegant::Header

Public Class Methods

new(document, options = {}) click to toggle source
# File lib/elegant/header.rb, line 6
def initialize(document, options = {})
  @document = document
  @text, @logo = options.values_at :text, :logo
  @logo_width = @logo.fetch(:width, 50) if @logo
  @logo_height = @logo.fetch(:height, 50) if @logo
end

Public Instance Methods

render() click to toggle source

Draws in the header of each page a watermark logo (provided via the configuration), a horizontal line, a title and an image for the content.

# File lib/elegant/header.rb, line 15
def render
  repeat(:all) do
    render_watermark
    stroke_horizontal_rule
    render_logo
    render_heading
  end
end
title_padding() click to toggle source

Sets the right padding for title based on whether the page has a logo or not.

# File lib/elegant/header.rb, line 26
def title_padding
  @logo_width ? @logo_width + (margin = 7) : 0
end

Private Instance Methods

heading_options(options = {}) click to toggle source
# File lib/elegant/header.rb, line 84
def heading_options(options = {})
  left = 25
  width = (@logo_width || 0) + 2 * line_width + (margin = 5)
  options[:valign] = :center
  options[:align] = :right
  options[:size] = 17
  options[:overflow] = :shrink_to_fit
  options[:width] = bounds.width - width - left
  options[:height] = document.header_height / 2.0
  options[:at] = [left, bounds.top + options[:height]]
  options
end
render_heading() click to toggle source

Writes the heading for the document in the top-right corner of each page, to the left of the logo. The heading must be provided when initializing the document.

# File lib/elegant/header.rb, line 78
def render_heading
  transparent(0.25) do
    font('Sans Serif', style: :bold) {text_box @text, heading_options}
  end if @text
end
render_logo_frame(options = {}) click to toggle source

Renders a frame around the logo in the top-right corner.

# File lib/elegant/header.rb, line 52
def render_logo_frame(options = {})
  width = @logo_width + line_width
  height = @logo_height + line_width
  left = bounds.right - width - 0.5 * line_width
  top = bounds.top + 0.5 * height
  bounding_box([left, top], width: width, height: height) do
    old_color = self.fill_color
    fill_color 'FFFFFF'
    fill_rectangle [0, height], width, height
    fill_color old_color
    stroke_bounds
  end
end
render_logo_image() click to toggle source

Renders the actual image as the logo in the top-right corner.

# File lib/elegant/header.rb, line 67
def render_logo_image
  left = bounds.right - @logo_width - line_width
  top = bounds.top + @logo_height / 2
  options = {width: @logo_width, height: @logo_height, at: [left, top]}
  image open(@logo[:url]), options
rescue OpenURI::HTTPError, OpenSSL::SSL::SSLError, SocketError, Prawn::Errors::UnsupportedImageType
end
render_watermark() click to toggle source

Renders a watermark at the top-left corner of each page. The watermark must be provided via configuration.

# File lib/elegant/header.rb, line 34
def render_watermark
  image = Elegant.configuration.watermark
  height = (document.header_height * 0.25).ceil
  y = bounds.top + (document.header_height * 0.375).floor
  image image, at: [0, y], height: height.ceil
end