Object
Generally, one would use the Prawn::Text::Formatted#formatted_text_box convenience method. However, using Text::Formatted::Box.new in conjunction with render(:dry_run => true) 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
Example (see Prawn::Text::Core::Formatted::Wrap for what is required of the wrap method if you want to override the default wrapping algorithm):
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"?')
# File lib/prawn/text/formatted/box.rb, line 323 def self.extensions @extensions ||= [] end
@private
# File lib/prawn/text/formatted/box.rb, line 328 def self.inherited(base) extensions.each { |e| base.extensions << e } end
See Prawn::Text#text_box for valid options
# File lib/prawn/text/formatted/box.rb, line 138 def initialize(formatted_text, options={}) @inked = false Prawn.verify_options(valid_options, options) options = options.dup self.class.extensions.reverse_each { |e| extend e } @overflow = options[:overflow] || :truncate self.original_text = formatted_text @text = nil @document = options[:document] @direction = options[:direction] || @document.text_direction @fallback_fonts = options[:fallback_fonts] || @document.fallback_fonts @at = (options[:at] || [@document.bounds.left, @document.bounds.top]).dup @width = options[:width] || @document.bounds.right - @at[0] @height = options[:height] || default_height @align = options[:align] || (@direction == :rtl ? :right : :left) @vertical_align = options[:valign] || :top @leading = options[:leading] || @document.default_leading @character_spacing = options[:character_spacing] || @document.character_spacing @mode = options[:mode] || @document.text_rendering_mode @rotate = options[:rotate] || 0 @rotate_around = options[:rotate_around] || :upper_left @single_line = options[:single_line] @skip_encoding = options[:skip_encoding] || @document.skip_encoding @draw_text_callback = options[: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 = options[:min_font_size] || 5 if options[:kerning].nil? then options[:kerning] = @document.default_kerning? end @options = { :kerning => options[:kerning], :size => options[:size], :style => options[:style] } super(formatted_text, options) end
The width available at this point in the box
# File lib/prawn/text/formatted/box.rb, line 239 def available_width @width end
True iff everything printed (or, if dry_run was used, everything would have been successfully printed)
# File lib/prawn/text/formatted/box.rb, line 117 def everything_printed? @everything_printed end
The height actually used during the previous render
# File lib/prawn/text/formatted/box.rb, line 245 def height return 0 if @baseline_y.nil? || @descender.nil? (@baseline_y - @descender).abs end
# File lib/prawn/text/formatted/box.rb, line 132 def line_gap line_height - (ascender + descender) end
True iff nothing printed (or, if dry_run was used, nothing would have been successfully printed)
# File lib/prawn/text/formatted/box.rb, line 111 def nothing_printed? @nothing_printed end
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
# File lib/prawn/text/formatted/box.rb, line 205 def render(flags={}) unprinted_text = [] @document.save_font do @document.character_spacing(@character_spacing) do @document.text_rendering_mode(@mode) do process_options if @skip_encoding text = original_text else text = normalize_encoding end @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] if @rotate != 0 && @inked unprinted_text = render_rotated(text) else unprinted_text = wrap(text) end @inked = false end end end end unprinted_text end
# File lib/prawn/text/formatted/box.rb, line 332 def valid_options PDF::Core::Text::VALID_OPTIONS + [:at, :height, :width, :align, :valign, :rotate, :rotate_around, :overflow, :min_font_size, :leading, :character_spacing, :mode, :single_line, :skip_encoding, :document, :direction, :fallback_fonts, :draw_text_callback] end
Generated with the Darkfish Rdoc Generator 2.