Class: Prawn::Text::Formatted::Box
- Inherits:
-
Object
- Object
- Prawn::Text::Formatted::Box
- Defined in:
- lib/prawn/text/formatted/box.rb
Overview
Formatted text box.
Generally, one would use the #formatted_text_box convenience method. However, using ‘Text::Formatted::Box.new` in conjunction with `#render(dry_run: true)` enables one to do calculations prior to placing text on the page, or to determine how much vertical space was consumed by the printed text
Direct Known Subclasses
Experimental API collapse
-
#ascender ⇒ Number
readonly
The height of the ascender of the last line printed.
-
#at ⇒ Array(Number, Number)
readonly
The upper left corner of the text box.
-
#descender ⇒ Number
readonly
The height of the descender of the last line printed.
-
#leading ⇒ Number
readonly
The leading used during printing.
-
#line_height ⇒ Number
readonly
The line height of the last line printed.
-
#text ⇒ Array<Hash>
readonly
The text that was successfully printed (or, if ‘:dry_run` was used, the text that would have been successfully printed).
Experimental API collapse
-
#available_width ⇒ Number
The width available at this point in the box.
-
#everything_printed? ⇒ Boolean
True if everything printed (or, if ‘:dry_run` was used, everything would have been successfully printed).
-
#height ⇒ Number
The height actually used during the previous #render.
-
#initialize(formatted_text, options = {}) ⇒ Box
constructor
See Prawn::Text#text_box for valid options.
-
#line_gap ⇒ Number
Gap between adjacent lines of text.
-
#nothing_printed? ⇒ Boolean
True if nothing printed (or, if ‘:dry_run` was used, nothing would have been successfully printed).
-
#render(flags = {}) ⇒ Array<Hash>
Render text to the document based on the settings defined in constructor.
Extension API collapse
-
.extensions ⇒ Array<Module>
Text box extensions.
Constructor Details
#initialize(formatted_text, options = {}) ⇒ Box
See Prawn::Text#text_box for valid options
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/prawn/text/formatted/box.rb', line 173 def initialize(formatted_text, = {}) @inked = false Prawn.(, ) = .dup self.class.extensions.reverse_each { |e| extend(e) } @overflow = [:overflow] || :truncate @disable_wrap_by_char = [:disable_wrap_by_char] self.original_text = formatted_text @text = nil @document = [:document] @direction = [:direction] || @document.text_direction @fallback_fonts = [:fallback_fonts] || @document.fallback_fonts @at = ( [:at] || [@document.bounds.left, @document.bounds.top] ).dup @width = [:width] || (@document.bounds.right - @at[0]) @height = [:height] || default_height @align = [:align] || (@direction == :rtl ? :right : :left) @vertical_align = [:valign] || :top @leading = [:leading] || @document.default_leading @character_spacing = [:character_spacing] || @document.character_spacing @mode = [:mode] || @document.text_rendering_mode @rotate = [:rotate] || 0 @rotate_around = [:rotate_around] || :upper_left @single_line = [:single_line] @draw_text_callback = [:draw_text_callback] # if the text rendering mode is :unknown, force it back to :fill if @mode == :unknown @mode = :fill end 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 = default_height @overflow = :truncate end @min_font_size = [:min_font_size] || 5 if [:kerning].nil? [:kerning] = @document.default_kerning? end @options = { kerning: [:kerning], size: [:size], style: [:style], } super(formatted_text, ) end |
Instance Attribute Details
#ascender ⇒ Number (readonly)
The height of the ascender of the last line printed.
56 57 58 |
# File 'lib/prawn/text/formatted/box.rb', line 56 def ascender @ascender end |
#at ⇒ Array(Number, Number) (readonly)
The upper left corner of the text box.
48 49 50 |
# File 'lib/prawn/text/formatted/box.rb', line 48 def at @at end |
#descender ⇒ Number (readonly)
The height of the descender of the last line printed.
60 61 62 |
# File 'lib/prawn/text/formatted/box.rb', line 60 def descender @descender end |
#leading ⇒ Number (readonly)
The leading used during printing.
64 65 66 |
# File 'lib/prawn/text/formatted/box.rb', line 64 def leading @leading end |
#line_height ⇒ Number (readonly)
The line height of the last line printed.
52 53 54 |
# File 'lib/prawn/text/formatted/box.rb', line 52 def line_height @line_height end |
#text ⇒ Array<Hash> (readonly)
The text that was successfully printed (or, if ‘:dry_run` was used, the text that would have been successfully printed).
28 29 30 |
# File 'lib/prawn/text/formatted/box.rb', line 28 def text @text end |
Class Method Details
.extensions ⇒ Array<Module>
Text box extensions.
Example:
“‘ruby module MyWrap
def wrap(array)
initialize_wrap([{ text: 'all your base are belong to us' }])
@line_wrap.wrap_line(
document: @document,
kerning: @kerning,
width: 10000,
arranger: @arranger
)
fragment = @arranger.retrieve_fragment
format_and_draw_fragment(fragment, 0, @line_wrap.width, 0)
[]
end
end
Prawn::Text::Formatted::Box.extensions << MyWrap
box = Prawn::Text::Formatted::Box.new(‘hello world’) box.render(“why can’t I print anything other than” +
'"all your base are belong to us"?')
“‘
See Wrap for what is required of the wrap method if you want to override the default wrapping algorithm.
391 392 393 |
# File 'lib/prawn/text/formatted/box.rb', line 391 def self.extensions @extensions ||= [] end |
Instance Method Details
#available_width ⇒ Number
The width available at this point in the box.
285 286 287 |
# File 'lib/prawn/text/formatted/box.rb', line 285 def available_width @width end |
#everything_printed? ⇒ Boolean
True if everything printed (or, if ‘:dry_run` was used, everything would have been successfully printed).
42 43 44 |
# File 'lib/prawn/text/formatted/box.rb', line 42 def everything_printed? @everything_printed end |
#height ⇒ Number
The height actually used during the previous #render.
292 293 294 295 296 |
# File 'lib/prawn/text/formatted/box.rb', line 292 def height return 0 if @baseline_y.nil? || @descender.nil? (@baseline_y - @descender).abs end |
#line_gap ⇒ Number
Gap between adjacent lines of text.
69 70 71 |
# File 'lib/prawn/text/formatted/box.rb', line 69 def line_gap line_height - (ascender + descender) end |
#nothing_printed? ⇒ Boolean
True if nothing printed (or, if ‘:dry_run` was used, nothing would have been successfully printed).
34 35 36 |
# File 'lib/prawn/text/formatted/box.rb', line 34 def nothing_printed? @nothing_printed end |
#render(flags = {}) ⇒ Array<Hash>
Render text to the document based on the settings defined in constructor.
In order to facilitate look-ahead calculations, this method 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.
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/prawn/text/formatted/box.rb', line 251 def render(flags = {}) unprinted_text = [] @document.save_font do @document.character_spacing(@character_spacing) do @document.text_rendering_mode(@mode) do text = normalized_text(flags) @document.font_size(@font_size) do shrink_to_fit(text) if @overflow == :shrink_to_fit process_vertical_alignment(text) @inked = true unless flags[:dry_run] unprinted_text = if @rotate != 0 && @inked render_rotated(text) else wrap(text) end @inked = false end end end end unprinted_text.map do |e| e.merge(text: @document.font.to_utf8(e[:text])) end end |