GithubHelp home page GithubHelp logo

aslrousta / line-breaking Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 12 KB

Go Line-breaking Algorithm width Bi-directional Support

License: MIT License

Go 100.00%
golang knuth-plass-breaking line-breaking

line-breaking's Introduction

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

Copyright

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

line-breaking's People

Contributors

aslrousta avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.