Documentation ¶
Overview ¶
Package firstfit implements a straightforward line-breaking algorithm where lines are broken at the first suitable breakpoint.
Wikipedia:
- | SpaceLeft := LineWidth
- | for each Word in Text
- | if (Width(Word) + SpaceWidth) > SpaceLeft
- | insert line break before Word in Text
- | SpaceLeft := LineWidth - Width(Word)
- | else
- | SpaceLeft := SpaceLeft - (Width(Word) + SpaceWidth)
With khipus, we have space before words, as a rule, not after them. Additionally, we break at penalty knots, not on whitespace. However, the implementation looks roughly like this:
- | SpaceUsed := 0; firstInLine := true
- | for each Knot in Khipu
- | if Knot.Type == Word
- | if (SpaceUsed + Width(Word)).MinWidth > LineWidth
- | insert line break before Word in Text
- | SpaceUsed := 0
- | firstInLine := true
- | else
- | SpaceUsed := SpaceUsed + (Width(Word) + SpaceWidth)
- | firstInLine := false
- | else if not firstInLine
- | SpaceUsed := SpaceUsed + Width(Knot)
One of the characteristics of the first-fit algorithm is that is decides about a breakpoint not until it's too late. It recognizes a breakpoint, when a word overshoots the line, and then has to move back to before that word.
What counts as a word is not so clear with international scripts. We rely on the khipukamayuq to insert appropriate penalties before line-breaking happens. Thus, we do not break automatically on spaces, but rather on penalties.
BSD License ¶
Copyright (c) 2017–20, Norbert Pillmayer ¶
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of this software nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BreakParagraph ¶
func BreakParagraph(cursor linebreak.Cursor, parshape linebreak.ParShape, params *linebreak.Parameters) ([]khipu.Mark, error)
BreakParagraph will find 'first fit' breakpoints for a paragraph of text. The method is similar to the one usually used by web browsers. It simply collects line material until the current line-length is exhausted, then continues on a new line.
The input khipu has to end with a penalty.
Types ¶
This section is empty.