Class: Prawn::Text::Formatted::Box

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/text/formatted/box.rb

Overview

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

Direct Known Subclasses

Box

Experimental API (collapse)

Experimental API (collapse)

Extension API (collapse)

Constructor Details

- (Box) initialize(formatted_text, options = {})

See Prawn::Text#text_box for valid options



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/prawn/text/formatted/box.rb', line 139

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
  @disable_wrap_by_char = options[:disable_wrap_by_char]

  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]
  @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

Instance Attribute Details

- (Object) ascender (readonly)

The height of the ascender of the last line printed



127
128
129
# File 'lib/prawn/text/formatted/box.rb', line 127

def ascender
  @ascender
end

- (Object) at (readonly)

The upper left corner of the text box



123
124
125
# File 'lib/prawn/text/formatted/box.rb', line 123

def at
  @at
end

- (Object) descender (readonly)

The height of the descender of the last line printed



129
130
131
# File 'lib/prawn/text/formatted/box.rb', line 129

def descender
  @descender
end

- (Object) leading (readonly)

The leading used during printing



131
132
133
# File 'lib/prawn/text/formatted/box.rb', line 131

def leading
  @leading
end

- (Object) line_height (readonly)

The line height of the last line printed



125
126
127
# File 'lib/prawn/text/formatted/box.rb', line 125

def line_height
  @line_height
end

- (Object) text (readonly)

The text that was successfully printed (or, if dry_run was used, the text that would have been successfully printed)



108
109
110
# File 'lib/prawn/text/formatted/box.rb', line 108

def text
  @text
end

Class Method Details

+ (Object) extensions

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"?')


322
323
324
# File 'lib/prawn/text/formatted/box.rb', line 322

def self.extensions
  @extensions ||= []
end

Instance Method Details

- (Object) available_width

The width available at this point in the box



238
239
240
# File 'lib/prawn/text/formatted/box.rb', line 238

def available_width
  @width
end

- (Object) draw_fragment(fragment, accumulated_width = 0, line_width = 0, word_spacing = 0)

fragment is a Prawn::Text::Formatted::Fragment object



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
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/prawn/text/formatted/box.rb', line 251

def draw_fragment(fragment, accumulated_width = 0, line_width = 0, word_spacing = 0) #:nodoc:
  case(@align)
  when :left
    x = @at[0]
  when :center
    x = @at[0] + @width * 0.5 - line_width * 0.5
  when :right
    x = @at[0] + @width - line_width
  when :justify
    if @direction == :ltr
      x = @at[0]
    else
      x = @at[0] + @width - line_width
    end
  end

  x += accumulated_width

  y = @at[1] + @baseline_y

  y += fragment.y_offset

  fragment.left = x
  fragment.baseline = y

  if @inked
    draw_fragment_underlays(fragment)

    @document.word_spacing(word_spacing) {
      if @draw_text_callback
        @draw_text_callback.call(fragment.text, :at => [x, y],
                                                :kerning => @kerning)
      else
        @document.draw_text!(fragment.text, :at => [x, y],
                                            :kerning => @kerning)
      end
    }

    draw_fragment_overlays(fragment)
  end
end

- (Boolean) everything_printed?

True if everything printed (or, if dry_run was used, everything would have been successfully printed)

Returns:

  • (Boolean)


118
119
120
# File 'lib/prawn/text/formatted/box.rb', line 118

def everything_printed?
  @everything_printed
end

- (Object) height

The height actually used during the previous render



244
245
246
247
# File 'lib/prawn/text/formatted/box.rb', line 244

def height
  return 0 if @baseline_y.nil? || @descender.nil?
  (@baseline_y - @descender).abs
end

- (Object) line_gap



133
134
135
# File 'lib/prawn/text/formatted/box.rb', line 133

def line_gap
  line_height - (ascender + descender)
end

- (Boolean) nothing_printed?

True if nothing printed (or, if dry_run was used, nothing would have been successfully printed)

Returns:

  • (Boolean)


112
113
114
# File 'lib/prawn/text/formatted/box.rb', line 112

def nothing_printed?
  @nothing_printed
end

- (Object) render(flags = {})

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



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
232
233
234
# File 'lib/prawn/text/formatted/box.rb', line 206

def render(flags = {})
  unprinted_text = []

  @document.save_font do
    @document.character_spacing(@character_spacing) do
      @document.text_rendering_mode(@mode) do
        process_options

        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]
          if @rotate != 0 && @inked
            unprinted_text = render_rotated(text)
          else
            unprinted_text = 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

- (Object) valid_options



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/prawn/text/formatted/box.rb', line 331

def valid_options
  PDF::Core::Text::VALID_OPTIONS + [
    :at,
    :height, :width,
    :align, :valign,
    :rotate, :rotate_around,
    :overflow, :min_font_size,
    :disable_wrap_by_char,
    :leading, :character_spacing,
    :mode, :single_line,
    :document,
    :direction,
    :fallback_fonts,
    :draw_text_callback
  ]
end