Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FontUtils::splitLines() respects line breaks now #225

Open
wants to merge 1 commit into
base: GLES2-AnchorCenter
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 39 additions & 14 deletions src/org/andengine/opengl/font/FontUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,29 +131,54 @@ public static <L extends List<CharSequence>> L splitLines(final CharSequence pTe
}

/**
* Does not respect linebreaks!
* Wraps given {@link CharSequence} using provided {@link AutoWrap} policy. Respects existing line breaks.
*
* @param pFont
* @param pText
* @param pResult
* @param pAutoWrapWidth
* @return
*/
public static <L extends List<CharSequence>> L splitLines(final IFont pFont, final CharSequence pText, final L pResult, final AutoWrap pAutoWrap, final float pAutoWrapWidth) {
/**
* TODO In order to respect already existing linebreaks, {@link #split(CharSequence, List)} could be leveraged and than the following methods could be called for each line.
*/
public static <L extends List<CharSequence>> L splitLines(final IFont pFont, final CharSequence pText, L pResult, final AutoWrap pAutoWrap, final float pAutoWrapWidth) {

/* Split line into paragraphs */
splitLines(pText, pResult);

/* Wrap each paragraph one-by-one*/
int totalLines = pResult.size();
int paragraphIndex = 0;
while(paragraphIndex < totalLines) {
final List<CharSequence> paragraphLines = pResult.subList(paragraphIndex, paragraphIndex+1);
final CharSequence paragraphText = paragraphLines.get(0);
paragraphLines.clear();

wrapLine(pFont, paragraphText, paragraphLines, pAutoWrap, pAutoWrapWidth);

final int lines = paragraphLines.size();
paragraphIndex += lines;
totalLines += lines - 1;
}

return pResult;
}

private static <L extends List<CharSequence>> L wrapLine(final IFont pFont, final CharSequence pTextToWrap, final L pResult, final AutoWrap pAutoWrap, final float pAutoWrapWidth) {
switch (pAutoWrap) {
case LETTERS:
return FontUtils.splitLinesByLetters(pFont, pText, pResult, pAutoWrapWidth);
case WORDS:
return FontUtils.splitLinesByWords(pFont, pText, pResult, pAutoWrapWidth);
case CJK:
return FontUtils.splitLinesByCJK(pFont, pText, pResult, pAutoWrapWidth);
case NONE:
default:
throw new IllegalArgumentException("Unexpected " + AutoWrap.class.getSimpleName() + ": '" + pAutoWrap + "'.");
case LETTERS:
FontUtils.splitLinesByLetters(pFont, pTextToWrap, pResult, pAutoWrapWidth);
break;
case WORDS:
FontUtils.splitLinesByWords(pFont, pTextToWrap, pResult, pAutoWrapWidth);
break;
case CJK:
FontUtils.splitLinesByCJK(pFont, pTextToWrap, pResult, pAutoWrapWidth);
break;
case NONE:
default:
throw new IllegalArgumentException("Unexpected " + AutoWrap.class.getSimpleName() + ": '" + pAutoWrap + "'.");
}

return pResult;
}

private static <L extends List<CharSequence>> L splitLinesByLetters(final IFont pFont, final CharSequence pText, final L pResult, final float pAutoWrapWidth) {
Expand Down