try to fix wrong space recognition in StyledText

This commit is contained in:
Jörg Prante 2024-11-18 17:37:10 +01:00
parent aa08fa45f0
commit 0564b2e934
4 changed files with 19 additions and 26 deletions

View file

@ -1,3 +1,3 @@
group = org.xbib.graphics
name = graphics
version = 5.5.4
version = 5.5.5

View file

@ -5,6 +5,7 @@ import java.awt.Color;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
/**
* Base class representing drawable text styled with font, size, color etc.
@ -190,7 +191,7 @@ public class StyledText implements TextFragment {
try {
return fontDescriptor.getSize() * fontDescriptor.getSelectedFont(text).getStringWidth(text) / 1000;
} catch (Exception e) {
logger.log(Level.WARNING, "text = '" + text + "' " + e.getMessage(), e);
logger.log(Level.SEVERE, "text = '" + text + "' " + e.getMessage(), e);
return 0f;
}
}
@ -199,7 +200,7 @@ public class StyledText implements TextFragment {
if (string == null) {
return null;
}
if (!string.contains(" ")) {
if (!spaces.matcher(string).find()) {
return string;
}
return string.replaceAll("\\s+", " ");
@ -212,4 +213,6 @@ public class StyledText implements TextFragment {
+ ", leftMargin=" + leftMargin + ", rightMargin=" + rightMargin
+ ", baselineOffset=" + baselineOffset + ",rotation=" + rotation + "]";
}
private static final Pattern spaces = Pattern.compile("\\s+");
}

View file

@ -123,9 +123,9 @@ public class TextFlow implements TextSequence, WidthRespecting {
* @return the removed fragment (if any).
*/
public TextFragment removeLast() {
if (text.size() > 0) {
if (!text.isEmpty()) {
clearCache();
return text.remove(text.size() - 1);
return text.removeLast();
}
return null;
}
@ -134,9 +134,9 @@ public class TextFlow implements TextSequence, WidthRespecting {
* @return the last added fragment (if any).
*/
public TextFragment getLast() {
if (text.size() > 0) {
if (!text.isEmpty()) {
clearCache();
return text.get(text.size() - 1);
return text.getLast();
}
return null;
}

View file

@ -60,15 +60,13 @@ public class TextSequenceUtil {
List<TextLine> lines = getLines(wrapped);
Paragraph first = new Paragraph();
Paragraph tail = new Paragraph();
if (text instanceof TextFlow) {
TextFlow flow = (TextFlow) text;
if (text instanceof TextFlow flow) {
first.setMaxWidth(flow.getMaxWidth());
first.setLineSpacing(flow.getLineSpacing());
tail.setMaxWidth(flow.getMaxWidth());
tail.setLineSpacing(flow.getLineSpacing());
}
if (text instanceof Paragraph) {
Paragraph paragraph = (Paragraph) text;
if (text instanceof Paragraph paragraph) {
first.setAlignment(paragraph.getAlignment());
first.setApplyLineSpacingToFirstLine(paragraph.isApplyLineSpacingToFirstLine());
tail.setAlignment(paragraph.getAlignment());
@ -215,13 +213,11 @@ public class TextSequenceUtil {
} else {
ReplacedWhitespace whitespace = new ReplacedWhitespace(
text.substring(0, splitIndex), word.getFontDescriptor());
StyledText newWord = null;
StyledText newWord;
if (word instanceof StyledText) {
newWord = ((StyledText) word).inheritAttributes(text
.substring(splitIndex));
newWord = ((StyledText) word).inheritAttributes(text.substring(splitIndex));
} else {
newWord = new StyledText(text.substring(splitIndex),
word.getFontDescriptor(), word.getColor());
newWord = new StyledText(text.substring(splitIndex), word.getFontDescriptor(), word.getColor());
}
return new TextFragment[]{newWord, whitespace};
}
@ -323,25 +319,19 @@ public class TextSequenceUtil {
float leftMargin = 0;
float rightMargin = 0;
if (word instanceof StyledText) {
StyledText styledText = (StyledText) word;
if (word instanceof StyledText styledText) {
leftMargin = styledText.getLeftMargin();
rightMargin = styledText.getRightMargin();
}
Pair<String> brokenWord = WordBreakerFactory.getWorkBreaker().breakWord(word.getText(), word.getFontDescriptor(),
remainingLineWidth - leftMargin, breakHard);
if (brokenWord == null) {
return null;
}
// break at calculated index
TextFragment head = deriveFromExisting(word,
brokenWord.getFirst(), leftMargin, 0);
TextFragment tail = deriveFromExisting(word,
brokenWord.getSecond(), 0, rightMargin);
return new Pair<TextFragment>(head, tail);
TextFragment head = deriveFromExisting(word, brokenWord.getFirst(), leftMargin, 0);
TextFragment tail = deriveFromExisting(word, brokenWord.getSecond(), 0, rightMargin);
return new Pair<>(head, tail);
}
/**