mirror of
https://github.com/Sollace/Unicopia.git
synced 2025-02-01 11:36:43 +01:00
Add newline character support and fix some text wrapping incorrectly
This commit is contained in:
parent
85bb51ed78
commit
af385ac671
1 changed files with 14 additions and 2 deletions
|
@ -32,15 +32,24 @@ class ParagraphWrappingVisitor implements StyledVisitor<Object> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<Object> accept(Style style, String s) {
|
public Optional<Object> accept(Style style, String s) {
|
||||||
|
|
||||||
|
int remainingLength = (int)(pageWidth - currentLineCollectedLength);
|
||||||
|
|
||||||
while (!s.isEmpty()) {
|
while (!s.isEmpty()) {
|
||||||
int trimmedLength = handler.getTrimmedLength(s, pageWidth, style);
|
int trimmedLength = handler.getTrimmedLength(s, remainingLength, style);
|
||||||
|
int newline = s.indexOf('\n');
|
||||||
|
|
||||||
|
if (newline >= 0 && newline < trimmedLength) {
|
||||||
|
trimmedLength = newline;
|
||||||
|
} else {
|
||||||
|
newline = -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (trimmedLength == 0) {
|
if (trimmedLength == 0) {
|
||||||
trimmedLength = s.length();
|
trimmedLength = s.length();
|
||||||
}
|
}
|
||||||
|
|
||||||
String wrappedFragment = s.substring(0, trimmedLength);
|
String wrappedFragment = s.substring(0, trimmedLength);
|
||||||
|
|
||||||
int lastSpace = wrappedFragment.lastIndexOf(' ');
|
int lastSpace = wrappedFragment.lastIndexOf(' ');
|
||||||
trimmedLength = lastSpace > 0 ? Math.min(lastSpace, trimmedLength) : trimmedLength;
|
trimmedLength = lastSpace > 0 ? Math.min(lastSpace, trimmedLength) : trimmedLength;
|
||||||
|
|
||||||
|
@ -53,6 +62,7 @@ class ParagraphWrappingVisitor implements StyledVisitor<Object> {
|
||||||
|
|
||||||
if (currentLineCollectedLength > 0) {
|
if (currentLineCollectedLength > 0) {
|
||||||
currentLine.append(" ");
|
currentLine.append(" ");
|
||||||
|
currentLineCollectedLength += handler.getWidth(" ");
|
||||||
}
|
}
|
||||||
currentLine.append(fragment);
|
currentLine.append(fragment);
|
||||||
currentLineCollectedLength += grabbedWidth;
|
currentLineCollectedLength += grabbedWidth;
|
||||||
|
@ -60,6 +70,8 @@ class ParagraphWrappingVisitor implements StyledVisitor<Object> {
|
||||||
if (trimmedLength <= s.length()) {
|
if (trimmedLength <= s.length()) {
|
||||||
s = s.substring(trimmedLength, s.length());
|
s = s.substring(trimmedLength, s.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
remainingLength = pageWidth;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
|
|
Loading…
Reference in a new issue