public class TextBlock { // currently working only with squares // raw working text variables String textBlock = ""; // the original text block String wrappedTextBlock = ""; float textBlockWidth = 0; float textBlockHeight = 0; float maxActualTextWidth = 0; // font info float fontLeading = 10.; // in pixels float fontSize = 25.; // in pixels PFont font = null; // the font in use /** * constructor * **/ public TextBlock(String _input, PFont _font, float _fontSize, float _fontLeading, float _textBlockWidth) { textBlock = _input; font = _font; textFont(font); fontSize = _fontSize; textSize(fontSize); fontLeading = _fontLeading; textLeading(fontLeading); textBlockWidth = _textBlockWidth; softWrapTextBlock(); } void softWrapTextBlock() { float currentLinePosition = 0; int currentLineNumber = 0; String[] tokens = textBlock.split(" "); for (int i = 0; i < tokens.length; i++) { currentLinePosition = currentLinePosition + textWidth(tokens[i] + " "); if(currentLinePosition > textBlockWidth) { wrappedTextBlock+="\n" + tokens[i] + " "; // insert a newline and add the word currentLineNumber++; // increment the line number currentLinePosition = textWidth(tokens[i] + " "); } else { maxActualTextWidth = max(currentLinePosition,maxActualTextWidth); wrappedTextBlock += tokens[i] + " "; // add the word } } textBlockHeight = fontLeading * (currentLineNumber + 1); } /** * Getters and Setters * **/ public void setFont(PFont _font) { font = _font; softWrapTextBlock(); // rewrap; } public PFont getFont() { return font; } public String getTextBlock() { return textBlock; } public String getWrappedTextBlock() { return wrappedTextBlock; } // width public float getTextBlockHeight() { return textBlockHeight; } // height public float getTextBlockWidth() { return textBlockWidth; } public float getMaxActualTextWidth() { return maxActualTextWidth; } }