lb

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 10, 2023 License: MIT Imports: 0 Imported by: 0

README

Line-breaking

This package provides a Go implementation of two line-breaking algorithms with support for bi-directional texts. However, it abstracts text elements (normally, words) with a Box interface that has a Width and Direction instead of plain strings, for more flexibility.

An example usage would be like the following:

package main

import (
    "strings"
    lb "github.com/aslrousta/line-breaking"
)

var paragraph = "Alice was beginning ..."

type Word string
func (w Word) Direction() lb.Direction { return lb.LeftToRight }
func (w Word) Width() float32 {
    // Compute the extent of the word in a desired font-face.
}

func main() {

    // Convert words in the paragraph into boxes.
    words := strings.Split(paragraph, " ")
    boxes := make([]lb.Box, 0, len(words))
    for _, w := range words {
        boxes = append(boxes, Word(w))
    }

    spaceWidth := /* Compute the extent of a `space` character */

    // Do the line-breaking using Knuth-Plass algorithm.
    lines := lb.KnuthPlass(boxes, &lb.Options{
        TextWidth:     60,
        TextDirection: lb.LeftToRight,
        GlueWidth:     spaceWidth,
        GlueShrink:    spaceWidth / 5, /* 20% shrink */
        GlueExpand:    spaceWidth / 3, /* 33% expand */
    })

    renderLines(lines)
    ...
}

See the example folder for a more practical sample code.

Algorithms

Two Greedy and Knuth-Plass line-breaking algorithms are provided. The greedy approach is a fast algorithm that tries to fit as boxes as possible within a line. On the other hand, the Knuth-Plass algorithm is relatively slow but gives better results

This package is distributed under the MIT license. See the LICENSE file for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Box

type Box interface {

	// Direction returns the writing direction of the box.
	Direction() Direction

	// Width returns the extent of the box within the text.
	Width() float32
}

Box is a solid text element (typically, a word).

type Direction

type Direction int

Direction is the data-type for word direction.

const (
	LeftToRight Direction = iota
	RightToLeft
)

List of word direction values.

type Line

type Line struct {

	// Boxes is the slice of boxes within the line.
	Boxes []Box

	// GlueWidth is the extent of the glues between the boxes.
	GlueWidth float32
}

Line is a series of boxes that fit in a line of text.

func Greedy

func Greedy(para []Box, opt *Options) (lines []*Line)

Greedy is a greedy but fast line-breaking algorithm that tries to fit as much boxes as possible in each and every line.

func KnuthPlass

func KnuthPlass(para []Box, opt *Options) (lines []*Line)

KnuthPlass is a relatively slow line-breaking algorithm but gives the best result aesthetically.

type Options

type Options struct {

	// TextWidth is the extent of a line of text.
	TextWidth float32

	// TextDirection is the writing direction of the text.
	TextDirection Direction

	// GlueWidth is the normal extent of the glues between the boxes.
	GlueWidth float32

	// GlueShrink is the extent that a glue can shrink to.
	GlueShrink float32

	// GlueExpand is the extent that a glue can expand to.
	GlueExpand float32
}

Options contains several options passed to the line-breaking algorithm.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL