Class: Prawn::Format::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/format/line.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instructions, hard_break) ⇒ Line

Returns a new instance of Line.



10
11
12
13
14
15
16
17
18
# File 'lib/prawn/format/line.rb', line 10

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

Instance Attribute Details

#instructionsObject (readonly)

Returns the value of attribute instructions.



8
9
10
# File 'lib/prawn/format/line.rb', line 8

def instructions
  @instructions
end

#sourceObject (readonly)

Returns the value of attribute source.



7
8
9
# File 'lib/prawn/format/line.rb', line 7

def source
  @source
end

Instance Method Details

#ascentObject

distance from top of line to baseline



47
48
49
# File 'lib/prawn/format/line.rb', line 47

def ascent
  instructions.map { |instruction| instruction.ascent }.max || 0
end

#descentObject

distance from bottom of line to baseline



52
53
54
# File 'lib/prawn/format/line.rb', line 52

def descent
  instructions.map { |instruction| instruction.descent }.min || 0
end

#draw_on(document, state, options = {}) ⇒ Object



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
# File 'lib/prawn/format/line.rb', line 60

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?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/prawn/format/line.rb', line 38

def hard_break?
  @hard_break
end

#height(include_blank = false) ⇒ Object



56
57
58
# File 'lib/prawn/format/line.rb', line 56

def height(include_blank=false)
  instructions.map { |instruction| instruction.height(include_blank) }.max
end

#spacesObject



31
32
33
34
35
36
# File 'lib/prawn/format/line.rb', line 31

def spaces
  @spaces ||= begin
    spaces = instructions.inject(0) { |sum, instruction| sum + instruction.spaces }
    [1, spaces].max
  end
end

#widthObject



42
43
44
# File 'lib/prawn/format/line.rb', line 42

def width
  instructions.inject(0) { |sum, instruction| sum + instruction.width }
end