Skip to content

WordBreak

Ralf Stuckert edited this page Feb 19, 2017 · 2 revisions

Word Break

In versions before 0.8.5, word wrapping has been stupid simple by splitting the text by blanks. If a word did not fit into the line, it has been put to the next line. If it was not fitting a full line, it has been left as it is...resulting in an overflow:

legacy long word breaking

Since 0.8.5 a new strategy has been implemented: If a word is to long to fit into a full line, it will be hard cut into pieces:

long word breaking

Also, a lot of people wanted to break words at certain characters if necessary (and not only at blanks). Let's see the following example text:

legacy word breaking

The new strategy currently breaks words after a hyphen, slash, comma and period. This eases up things, if there is e.g. no blank after a comma or period. Also hyphen is a perfect place to break.

soft word breaking

Legacy behavior

Hey, I don't need no stinkin' word breaking. I want it to work as it was! No problem, you can configure it to use the old strategy if you want. Just set the legacy strategy by setting a system property:

System.setProperty(WordBreakerFactory.WORD_BREAKER_CLASS_PROPERTY, 
	                WordBreakerFactory.LEGACY_WORD_BREAKER_CLASS_NAME);

Custom Word Breaking Strategy

Word breaking has been implemented as a strategy, so you might alter the existing one. Or write it completely on your own to implement a hyphenation-algorithm. Or...whatever you want. Just implement the interface WordBreaker...

public class MySuperDuperWordBreaker implements WordBreaker {

  @Override
  Pair<String> breakWord(final String word,
                          final FontDescriptor fontDescriptor, final float maxWidth,
                          final boolean breakHardIfNecessary) throws IOException;
        // TODO implement

  }

...and register your implementation by setting the following system property:

 System.setProperty(WordBreakerFactory.WORD_BREAKER_CLASS_PROPERTY, 
                   MySuperDuperWordBreaker.class.getName());