class Prawn::Format::Line

Attributes

instructions[R]
source[R]

Public Class Methods

new(instructions, hard_break) click to toggle source
# File lib/prawn/format/line.rb, line 9
def initialize(instructions, hard_break)
  # need to remember the "source" instructions, because lines can
  # pushed back onto the stack en masse when flowing into boxes,
  # if a line is discovered to not fit. Thus, a line must preserve
  # all instructions it was originally given.

  @source = instructions
  @hard_break = hard_break
end

Public Instance Methods

ascent() click to toggle source

distance from top of line to baseline

# File lib/prawn/format/line.rb, line 46
def ascent
  instructions.map { |instruction| instruction.ascent }.max || 0
end
descent() click to toggle source

distance from bottom of line to baseline

# File lib/prawn/format/line.rb, line 51
def descent
  instructions.map { |instruction| instruction.descent }.min || 0
end
draw_on(document, state, options={}) click to toggle source
# File lib/prawn/format/line.rb, line 59
def draw_on(document, state, options={})
  return if instructions.empty?

  format_state = instructions.first.state

  case(options[:align])
  when :left
    state[:dx] = 0
  when :center
    state[:dx] = (state[:width] - width) / 2.0
  when :right
    state[:dx] = state[:width] - width
  when :justify
    state[:dx] = 0
    state[:padding] = hard_break? ? 0 : (state[:width] - width) / spaces
    state[:text].word_space(state[:padding])
  end

  state[:dy] -= ascent

  state[:text].move_to(state[:dx], state[:dy])
  state[:line] = self

  document.save_font do
    instructions.each { |instruction| instruction.draw(document, state, options) }
    state[:pending_effects].each { |effect| effect.wrap(document, state) }
  end

  state[:dy] -= (options[:spacing] || 0) + (height - ascent)
end
hard_break?() click to toggle source
# File lib/prawn/format/line.rb, line 37
def hard_break?
  @hard_break
end
height(include_blank=false) click to toggle source
# File lib/prawn/format/line.rb, line 55
def height(include_blank=false)
  instructions.map { |instruction| instruction.height(include_blank) }.max
end
spaces() click to toggle source
# File lib/prawn/format/line.rb, line 30
def spaces
  @spaces ||= begin
    spaces = instructions.inject(0) { |sum, instruction| sum + instruction.spaces }
    [1, spaces].max
  end
end
width() click to toggle source
# File lib/prawn/format/line.rb, line 41
def width
  instructions.inject(0) { |sum, instruction| sum + instruction.width }
end

Private Instance Methods

consolidate(list) click to toggle source
# File lib/prawn/format/line.rb, line 92
def consolidate(list)
  list.inject([]) { |l,i| i.accumulate(l) }
end