| Class | Prawn::Format::LayoutBuilder |
| In: |
lib/prawn/format/layout_builder.rb
lib/prawn/format/layout_builder.rb |
| Parent: | Object |
| document | [R] | |
| document | [R] | |
| options | [R] | |
| options | [R] |
# File lib/prawn/format/layout_builder.rb, line 11
11: def initialize(document, text, options={})
12: @document = document
13: @options = options
14: @tags = document.tags.merge(options[:tags] || {})
15: @styles = document.styles.merge(options[:styles] || {})
16: style = document.default_style.merge(options[:default_style] || {})
17:
18: translate_prawn_options(style, options)
19:
20: @parser = Parser.new(@document, text,
21: :tags => @tags, :styles => @styles, :style => style)
22:
23: @state = {}
24: end
# File lib/prawn/format/layout_builder.rb, line 11
11: def initialize(document, text, options={})
12: @document = document
13: @options = options
14: @tags = document.tags.merge(options[:tags] || {})
15: @styles = document.styles.merge(options[:styles] || {})
16: style = document.default_style.merge(options[:default_style] || {})
17:
18: translate_prawn_options(style, options)
19:
20: @parser = Parser.new(@document, text,
21: :tags => @tags, :styles => @styles, :style => style)
22:
23: @state = {}
24: end
# File lib/prawn/format/layout_builder.rb, line 55
55: def fill(x, y, width, fill_options={}, &block)
56: lines = word_wrap(width, fill_options, &block)
57: draw_options = options.merge(fill_options).merge(:state => @state)
58: @state = document.draw_lines(x, y, width, lines, draw_options)
59: @state.delete(:cookies)
60: return @state[:dy] + y
61: end
# File lib/prawn/format/layout_builder.rb, line 55
55: def fill(x, y, width, fill_options={}, &block)
56: lines = word_wrap(width, fill_options, &block)
57: draw_options = options.merge(fill_options).merge(:state => @state)
58: @state = document.draw_lines(x, y, width, lines, draw_options)
59: @state.delete(:cookies)
60: return @state[:dy] + y
61: end
# File lib/prawn/format/layout_builder.rb, line 63
63: def next(line_width=nil)
64: line = []
65: width = 0
66: break_at = nil
67:
68: while (instruction = @parser.next)
69: next if !@parser.verbatim? && line.empty? && instruction.discardable? # ignore discardables at line start
70: line.push(instruction)
71:
72: if instruction.break?
73: width += instruction.width(:nondiscardable)
74: break_at = line.length if line_width && width <= line_width
75: width += instruction.width(:discardable)
76: else
77: width += instruction.width
78: end
79:
80: if instruction.force_break? || line_width && width >= line_width
81: break_at ||= line.length
82:
83: @parser.push(line.pop) while line.length > break_at
84: hard_break = instruction.force_break? || @parser.eos?
85:
86: return Line.new(line, hard_break)
87: end
88: end
89:
90: Line.new(line, true) if line.any?
91: end
# File lib/prawn/format/layout_builder.rb, line 63
63: def next(line_width=nil)
64: line = []
65: width = 0
66: break_at = nil
67:
68: while (instruction = @parser.next)
69: next if !@parser.verbatim? && line.empty? && instruction.discardable? # ignore discardables at line start
70: line.push(instruction)
71:
72: if instruction.break?
73: width += instruction.width(:nondiscardable)
74: break_at = line.length if line_width && width <= line_width
75: width += instruction.width(:discardable)
76: else
77: width += instruction.width
78: end
79:
80: if instruction.force_break? || line_width && width >= line_width
81: break_at ||= line.length
82:
83: @parser.push(line.pop) while line.length > break_at
84: hard_break = instruction.force_break? || @parser.eos?
85:
86: return Line.new(line, hard_break)
87: end
88: end
89:
90: Line.new(line, true) if line.any?
91: end
# File lib/prawn/format/layout_builder.rb, line 97
97: def translate_prawn_options(style, options)
98: style[:kerning] = options[:kerning] if options.key?(:kerning)
99: style[:font_size] = options[:size] if options.key?(:size)
100:
101: case options[:style]
102: when :bold then
103: style[:font_weight] = :bold
104: when :italic then
105: style[:font_style] = :italic
106: when :bold_italic then
107: style[:font_weight] = :bold
108: style[:font_style] = :italic
109: end
110: end
# File lib/prawn/format/layout_builder.rb, line 97
97: def translate_prawn_options(style, options)
98: style[:kerning] = options[:kerning] if options.key?(:kerning)
99: style[:font_size] = options[:size] if options.key?(:size)
100:
101: case options[:style]
102: when :bold then
103: style[:font_weight] = :bold
104: when :italic then
105: style[:font_style] = :italic
106: when :bold_italic then
107: style[:font_weight] = :bold
108: style[:font_style] = :italic
109: end
110: end
# File lib/prawn/format/layout_builder.rb, line 93
93: def unget(line)
94: line.source.reverse_each { |instruction| @parser.push(instruction) }
95: end
# File lib/prawn/format/layout_builder.rb, line 93
93: def unget(line)
94: line.source.reverse_each { |instruction| @parser.push(instruction) }
95: end
# File lib/prawn/format/layout_builder.rb, line 30
30: def word_wrap(width, options={}, &block)
31: if options[:height] && block
32: raise ArgumentError, "cannot specify both height and a block"
33: elsif options[:height]
34: block = Proc.new { |l, h| h > options[:height] }
35: elsif block.nil?
36: block = Proc.new { |l, h| false }
37: end
38:
39: lines = []
40: total_height = 0
41:
42: while (line = self.next(width))
43: if block[line, total_height + line.height]
44: unget(line)
45: break
46: end
47:
48: total_height += line.height
49: lines.push(line)
50: end
51:
52: return lines
53: end
# File lib/prawn/format/layout_builder.rb, line 30
30: def word_wrap(width, options={}, &block)
31: if options[:height] && block
32: raise ArgumentError, "cannot specify both height and a block"
33: elsif options[:height]
34: block = Proc.new { |l, h| h > options[:height] }
35: elsif block.nil?
36: block = Proc.new { |l, h| false }
37: end
38:
39: lines = []
40: total_height = 0
41:
42: while (line = self.next(width))
43: if block[line, total_height + line.height]
44: unget(line)
45: break
46: end
47:
48: total_height += line.height
49: lines.push(line)
50: end
51:
52: return lines
53: end