Class: Prawn::Format::LayoutBuilder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, text, options = {}) ⇒ LayoutBuilder

Returns a new instance of LayoutBuilder.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/prawn/format/layout_builder.rb', line 11

def initialize(document, text, options={})
  @document = document
  @options  = options
  @tags     = document.tags.merge(options[:tags] || {})
  @styles   = document.styles.merge(options[:styles] || {})
  style     = document.default_style.merge(options[:default_style] || {})

  translate_prawn_options(style, options)

  @parser   = Parser.new(@document, text,
                :tags => @tags, :styles => @styles, :style => style)

  @state    = {}
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



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

def document
  @document
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#done?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/prawn/format/layout_builder.rb', line 26

def done?
  @parser.eos?
end

#fill(x, y, width, fill_options = {}, &block) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/prawn/format/layout_builder.rb', line 55

def fill(x, y, width, fill_options={}, &block)
  lines = word_wrap(width, fill_options, &block)
  draw_options = options.merge(fill_options).merge(:state => @state)
  @state = document.draw_lines(x, y, width, lines, draw_options)
  @state.delete(:cookies)
  return @state[:dy] + y
end

#next(line_width = nil) ⇒ Object



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
90
91
# File 'lib/prawn/format/layout_builder.rb', line 63

def next(line_width=nil)
  line = []
  width = 0
  break_at = nil

  while (instruction = @parser.next)
    next if !@parser.verbatim? && line.empty? && instruction.discardable? # ignore discardables at line start
    line.push(instruction)

    if instruction.break?
      width += instruction.width(:nondiscardable)
      break_at = line.length if line_width && width <= line_width
      width += instruction.width(:discardable)
    else
      width += instruction.width
    end

    if instruction.force_break? || line_width && width >= line_width
      break_at ||= line.length

      @parser.push(line.pop) while line.length > break_at
      hard_break = instruction.force_break? || @parser.eos?

      return Line.new(line, hard_break)
    end
  end

  Line.new(line, true) if line.any?
end

#translate_prawn_options(style, options) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/prawn/format/layout_builder.rb', line 97

def translate_prawn_options(style, options)
  style[:kerning] = options[:kerning] if options.key?(:kerning)
  style[:font_size] = options[:size] if options.key?(:size)

  case options[:style]
  when :bold then
    style[:font_weight] = :bold
  when :italic then
    style[:font_style] = :italic
  when :bold_italic then
    style[:font_weight] = :bold
    style[:font_style] = :italic
  end
end

#unget(line) ⇒ Object



93
94
95
# File 'lib/prawn/format/layout_builder.rb', line 93

def unget(line)
  line.source.reverse_each { |instruction| @parser.push(instruction) }
end

#word_wrap(width, options = {}, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/prawn/format/layout_builder.rb', line 30

def word_wrap(width, options={}, &block)
  if options[:height] && block
    raise ArgumentError, "cannot specify both height and a block"
  elsif options[:height]
    block = Proc.new { |l, h| h > options[:height] }
  elsif block.nil?
    block = Proc.new { |l, h| false }
  end

  lines = []
  total_height = 0

  while (line = self.next(width))
    if block[line, total_height + line.height]
      unget(line)
      break
    end

    total_height += line.height
    lines.push(line)
  end

  return lines
end