folkertdev/elm-paragraph/1.0.0

Paragraph

This package picks (close to) optimal points to break a paragraph into lines.

Based on the paper Bridging the Algorithm Gap: A Linear-time Functional Program for Paragraph Formatting

type alias Options =
{ maximumWidth : Int
, optimalWidth : Int
, stringWidth : String -> Int
}

Formatting options

  • maximumWidth: the maximum width of a line
  • optimalWidth: the optimal width of a line. Often this is slightly smaller than the maximum width
  • stringWidth: function that calculates the width of a string. String.length usually gives good results, but other options (based on a font, or something creative) can be used as well.

Note: if a word is longer than the maximum width of a line it is left as-is. The line is broken after the word.

format : Options -> String -> String

Format a string using the given options

The output is one string where the lines are broken with newline \n characters.

lines : Options -> String -> List String

Format a string using the given options, returning the individual lines