Class: Prawn::Text::Box
- Inherits:
-
Object
- Object
- Prawn::Text::Box
- Defined in:
- lib/prawn/text/box.rb
Overview
Generally, one would use the text_box convenience method. However, using Text::Box.new in conjunction with render() enables one to do look-ahead calculations prior to placing text on the page, or to determine how much vertical space was consumed by the printed text
Instance Attribute Summary collapse
-
#ascender ⇒ Object
readonly
The height of the ascender of the last line printed.
-
#at ⇒ Object
readonly
The upper left corner of the text box.
-
#descender ⇒ Object
readonly
The height of the descender of the last line printed.
-
#leading ⇒ Object
readonly
The leading used during printing.
-
#line_height ⇒ Object
readonly
The line height of the last line printed.
-
#text ⇒ Object
readonly
The text that was successfully printed (or, if
dry_run
was used, the test that would have been successfully printed).
Instance Method Summary collapse
-
#height ⇒ Object
The height actually used during the previous
render
. -
#initialize(text, options = {}) ⇒ Box
constructor
See Prawn::Text#text_box for valid options.
-
#render(flags = {}) ⇒ Object
Render text to the document based on the settings defined in initialize.
Constructor Details
#initialize(text, options = {}) ⇒ Box
See Prawn::Text#text_box for valid options
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/prawn/text/box.rb', line 103 def initialize(text, ={}) @inked = false Prawn.(, ) = .dup @overflow = [:overflow] || :truncate # we'll be messing with the strings encoding, don't change the user's # original string @text_to_print = text.dup @text = nil @document = [:document] @at = [:at] || [@document.bounds.left, @document.bounds.top] @width = [:width] || @document.bounds.right - @at[0] @height = [:height] || @at[1] - @document.bounds.bottom @center = [@at[0] + @width * 0.5, @at[1] + @height * 0.5] @align = [:align] || :left @vertical_align = [:valign] || :top @leading = [:leading] || 0 if @overflow == :expand # if set to expand, then we simply set the bottom # as the bottom of the document bounds, since that # is the maximum we should expand to @height = @at[1] - @document.bounds.bottom @overflow = :truncate end @min_font_size = [:min_font_size] || 5 @wrap_block = [:wrap_block] || default_wrap_block @options = @document..merge(:kerning => [:kerning], :size => [:size], :style => [:style]) end |
Instance Attribute Details
#ascender ⇒ Object (readonly)
The height of the ascender of the last line printed
95 96 97 |
# File 'lib/prawn/text/box.rb', line 95 def ascender @ascender end |
#at ⇒ Object (readonly)
The upper left corner of the text box
91 92 93 |
# File 'lib/prawn/text/box.rb', line 91 def at @at end |
#descender ⇒ Object (readonly)
The height of the descender of the last line printed
97 98 99 |
# File 'lib/prawn/text/box.rb', line 97 def descender @descender end |
#leading ⇒ Object (readonly)
The leading used during printing
99 100 101 |
# File 'lib/prawn/text/box.rb', line 99 def leading @leading end |
#line_height ⇒ Object (readonly)
The line height of the last line printed
93 94 95 |
# File 'lib/prawn/text/box.rb', line 93 def line_height @line_height end |
#text ⇒ Object (readonly)
The text that was successfully printed (or, if dry_run
was used, the test that would have been successfully printed)
89 90 91 |
# File 'lib/prawn/text/box.rb', line 89 def text @text end |
Instance Method Details
#height ⇒ Object
The height actually used during the previous render
171 172 173 174 175 176 177 178 |
# File 'lib/prawn/text/box.rb', line 171 def height return 0 if @baseline_y.nil? || @descender.nil? # baseline is already pushed down one line below the current # line, so we need to subtract line line_height and leading, # but we need to add in the descender since baseline is # above the descender @baseline_y.abs + @descender - @line_height - @leading end |
#render(flags = {}) ⇒ Object
Render text to the document based on the settings defined in initialize.
In order to facilitate look-ahead calculations, render
accepts a :dry_run => true
option. If provided then everything is executed as if rendering, with the exception that nothing is drawn on the page. Useful for look-ahead computations of height, unprinted text, etc.
Returns any text that did not print under the current settings
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/prawn/text/box.rb', line 149 def render(flags={}) unprinted_text = '' @document.save_font do unless @document.skip_encoding @document.font.normalize_encoding!(@text_to_print) end @document.font_size(@font_size) do shrink_to_fit if @overflow == :shrink_to_fit process_vertical_alignment @inked = true unless flags[:dry_run] unprinted_text = _render(@text_to_print) @inked = false end end unprinted_text end |