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
Source Code
173 | def initialize(formatted_text, options = {}) |
174 | @inked = false |
175 | Prawn.verify_options(valid_options, options) |
176 | options = options.dup |
177 | |
178 | self.class.extensions.reverse_each { |e| extend(e) } |
179 | |
180 | @overflow = options[:overflow] || :truncate |
181 | @disable_wrap_by_char = options[:disable_wrap_by_char] |
182 | |
183 | self.original_text = formatted_text |
184 | @text = nil |
185 | |
186 | @document = options[:document] |
187 | @direction = options[:direction] || @document.text_direction |
188 | @fallback_fonts = options[:fallback_fonts] || |
189 | @document.fallback_fonts |
190 | @at = ( |
191 | options[:at] || [@document.bounds.left, @document.bounds.top] |
192 | ).dup |
193 | @width = options[:width] || |
194 | (@document.bounds.right - @at[0]) |
195 | @height = options[:height] || default_height |
196 | @align = options[:align] || |
197 | (@direction == :rtl ? :right : :left) |
198 | @vertical_align = options[:valign] || :top |
199 | @leading = options[:leading] || @document.default_leading |
200 | @character_spacing = options[:character_spacing] || |
201 | @document.character_spacing |
202 | @mode = options[:mode] || @document.text_rendering_mode |
203 | @rotate = options[:rotate] || 0 |
204 | @rotate_around = options[:rotate_around] || :upper_left |
205 | @single_line = options[:single_line] |
206 | @draw_text_callback = options[:draw_text_callback] |
207 | |
208 | # if the text rendering mode is :unknown, force it back to :fill
|
209 | if @mode == :unknown |
210 | @mode = :fill |
211 | end
|
212 | |
213 | if @overflow == :expand |
214 | # if set to expand, then we simply set the bottom
|
215 | # as the bottom of the document bounds, since that
|
216 | # is the maximum we should expand to
|
217 | @height = default_height |
218 | @overflow = :truncate |
219 | end
|
220 | @min_font_size = options[:min_font_size] || 5 |
221 | if options[:kerning].nil? |
222 | options[:kerning] = @document.default_kerning? |
223 | end
|
224 | @options = { |
225 | kerning: options[:kerning], |
226 | size: options[:size], |
227 | style: options[:style], |
228 | }
|
229 | |
230 | super(formatted_text, options) |
231 | end
|
Instance Attribute Details
#ascender ⇒ Number (readonly)
The height of the ascender of the last line printed.
Source Code
56 | def ascender |
57 | @ascender
|
58 | end
|
#at ⇒ Array(Number, Number) (readonly)
The upper left corner of the text box.
Source Code
48 | def at |
49 | @at
|
50 | end
|
#descender ⇒ Number (readonly)
The height of the descender of the last line printed.
Source Code
60 | def descender |
61 | @descender
|
62 | end
|
#leading ⇒ Number (readonly)
The leading used during printing.
Source Code
64 | def leading |
65 | @leading
|
66 | end
|
#line_height ⇒ Number (readonly)
The line height of the last line printed.
Source Code
52 | def line_height |
53 | @line_height
|
54 | 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).
Source Code
28 | def text |
29 | @text
|
30 | end
|
Class Method Details
.extensions ⇒ Array<Module>
Text box extensions.
Example:
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.
Source Code
391 | def self.extensions |
392 | @extensions ||= [] |
393 | end
|
Instance Method Details
#available_width ⇒ Number
The width available at this point in the box.
Source Code
285 | def available_width |
286 | @width
|
287 | end
|
#everything_printed? ⇒ Boolean
True if everything printed (or, if :dry_run
was used, everything
would have been successfully printed).
Source Code
42 | def everything_printed? |
43 | @everything_printed
|
44 | end
|
#height ⇒ Number
The height actually used during the previous #render.
Source Code
292 | def height |
293 | return 0 if @baseline_y.nil? || @descender.nil? |
294 | |
295 | (@baseline_y - @descender).abs |
296 | end
|
#line_gap ⇒ Number
Gap between adjacent lines of text.
Source Code
69 | def line_gap |
70 | line_height - (ascender + descender) |
71 | end
|
#nothing_printed? ⇒ Boolean
True if nothing printed (or, if :dry_run
was used, nothing would
have been successfully printed).
Source Code
34 | def nothing_printed? |
35 | @nothing_printed
|
36 | 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.
Source Code
251 | def render(flags = {}) |
252 | unprinted_text = [] |
253 | |
254 | @document.save_font do |
255 | @document.character_spacing(@character_spacing) do |
256 | @document.text_rendering_mode(@mode) do |
257 | process_options
|
258 | |
259 | text = normalized_text(flags) |
260 | |
261 | @document.font_size(@font_size) do |
262 | shrink_to_fit(text) if @overflow == :shrink_to_fit |
263 | process_vertical_alignment(text) |
264 | @inked = true unless flags[:dry_run] |
265 | unprinted_text = |
266 | if @rotate != 0 && @inked |
267 | render_rotated(text) |
268 | else
|
269 | wrap(text) |
270 | end
|
271 | @inked = false |
272 | end
|
273 | end
|
274 | end
|
275 | end
|
276 | |
277 | unprinted_text.map do |e| |
278 | e.merge(text: @document.font.to_utf8(e[:text])) |
279 | end
|
280 | end
|