(What-is) The best way to get the last line of a paragraph #1316
-
Hello, I would like to know the best way to retrieve the last line of any given paragraph, in order to access its text and length before writing it to the PDF document (for the purpose of handling les "lignes de voleurs" — I don't know the english term, but it's not widow or orphan). Thanks for any help, and sorry for bad english. Phil Phil |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
The closes thing to what you want that Prawn has is probably |
Beta Was this translation helpful? Give feedback.
-
Maybe a best/easier way? my_string = "bla bla bla" * 50
my_options = {
font: ..., at: ...., width: ...,
inline_format: true,
dry_run: true
}
pdf.update do
exceeded_text, my_box = text_box(my_string, **my_options)
# @note
# I overwrote #text_box method to deal with :dry_run
# (more convenient than box.render, see below)
last_printed_line = my_box.instance_variable_get('@printed_lines').last
# => last line text content
end The overwritten #text_box method for my needs: def text_box(string, options = {})
options = options.dup
options[:document] = self
# @Added
dry_run = options.delete(:dry_run)
box =
if options[:inline_format]
p = options.delete(:inline_format)
p = [] unless p.is_a?(Array)
array = text_formatter.format(string, *p)
Text::Formatted::Box.new(array, options)
else
Text::Box.new(string, options)
end
if dry_run
exceed = box.render(dry_run: dry_run)
[exceed, box]
else
[box.render, nil]
end
end |
Beta Was this translation helpful? Give feedback.
The closes thing to what you want that Prawn has is probably
Prawn::Text::Formatted::Arranger
but keep in mind that it's not a public API and is not designed to be ergonomic.