Layout:: TextLayouter:: SimpleLineWrapping
Implementation of a simple line wrapping algorithm.
The algorithm arranges the given items so that the maximum number is put onto each line, taking the differences of
Penalty items into account. It is not as advanced as say Knuth's line wrapping algorithm in that it doesn't optimize paragraphs.
Public Class Methods
Arranges the items into lines.
width_block argument has to be a callable object that returns the width of the line:
If the line width doesn't depend on the height or the vertical position of the line (i.e. fixed line width), the
width_blockshould have an arity of zero. However, this doesn't mean that the block is called only once; it is actually called before each new line (e.g. for varying line widths that don't depend on the line height; one common case is the indentation of the first line). This is the general case.
However, if lines should have varying widths (e.g. for flowing text around shapes), the
width_blockargument should be an object responding to call(line_like) where
line_likeis a Line-like object responding to y_min, y_max and height holding the values for the currently layed out line. The caller is responsible for tracking the height of the already layed out lines. This method involves more work and is therefore slower.
Regardless of whether varying line widths are used or not, each time a line is finished, it is yielded to the caller. The second argument
item is the item that caused the line break (e.g. a
Penalty). The return value should be truthy if line wrapping should continue, or falsy if it should stop. If the yielded line is empty and the yielded item is a box item, this single item didn't fit into the available width; the caller has to handle this situation, e.g. by stopping.
In case of varying widths, the
width_block may also return
nil in which case the algorithm should revert back to a stored item index and then start as if beginning a new line. Which index to use is told the algorithm through the special return value
:store_start_of_line of the yielded-to block. When this return value is used, the current start of the line index should be stored for later use.
After the algorithm is finished, it returns the unused items.
Creates a new line wrapping object that arranges the
items on lines with the given width.
Public Instance Methods
Peforms line wrapping with a fixed width per line, with line height playing no role.
Performs the line wrapping with variable widths.