Class: Prawn::Format::LayoutBuilder
- Inherits:
-
Object
- Object
- Prawn::Format::LayoutBuilder
- Defined in:
- lib/prawn/format/layout_builder.rb
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #done? ⇒ Boolean
- #fill(x, y, width, fill_options = {}, &block) ⇒ Object
-
#initialize(document, text, options = {}) ⇒ LayoutBuilder
constructor
A new instance of LayoutBuilder.
- #next(line_width = nil) ⇒ Object
- #translate_prawn_options(style, options) ⇒ Object
- #unget(line) ⇒ Object
- #word_wrap(width, options = {}, &block) ⇒ Object
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, ={}) @document = document @options = @tags = document..merge([:tags] || {}) @styles = document.styles.merge([:styles] || {}) style = document.default_style.merge([:default_style] || {}) (style, ) @parser = Parser.new(@document, text, :tags => @tags, :styles => @styles, :style => style) @state = {} end |
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
9 10 11 |
# File 'lib/prawn/format/layout_builder.rb', line 9 def document @document end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/prawn/format/layout_builder.rb', line 9 def @options end |
Instance Method Details
#done? ⇒ 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, ={}, &block) lines = word_wrap(width, , &block) = .merge().merge(:state => @state) @state = document.draw_lines(x, y, width, lines, ) @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 (style, ) style[:kerning] = [:kerning] if .key?(:kerning) style[:font_size] = [:size] if .key?(:size) case [: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, ={}, &block) if [:height] && block raise ArgumentError, "cannot specify both height and a block" elsif [:height] block = Proc.new { |l, h| h > [: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 |