try to fix wrong space recognition in StyledText
This commit is contained in:
parent
aa08fa45f0
commit
0564b2e934
4 changed files with 19 additions and 26 deletions
|
@ -1,3 +1,3 @@
|
||||||
group = org.xbib.graphics
|
group = org.xbib.graphics
|
||||||
name = graphics
|
name = graphics
|
||||||
version = 5.5.4
|
version = 5.5.5
|
||||||
|
|
|
@ -5,6 +5,7 @@ import java.awt.Color;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class representing drawable text styled with font, size, color etc.
|
* Base class representing drawable text styled with font, size, color etc.
|
||||||
|
@ -190,7 +191,7 @@ public class StyledText implements TextFragment {
|
||||||
try {
|
try {
|
||||||
return fontDescriptor.getSize() * fontDescriptor.getSelectedFont(text).getStringWidth(text) / 1000;
|
return fontDescriptor.getSize() * fontDescriptor.getSelectedFont(text).getStringWidth(text) / 1000;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.log(Level.WARNING, "text = '" + text + "' " + e.getMessage(), e);
|
logger.log(Level.SEVERE, "text = '" + text + "' " + e.getMessage(), e);
|
||||||
return 0f;
|
return 0f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -199,7 +200,7 @@ public class StyledText implements TextFragment {
|
||||||
if (string == null) {
|
if (string == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (!string.contains(" ")) {
|
if (!spaces.matcher(string).find()) {
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
return string.replaceAll("\\s+", " ");
|
return string.replaceAll("\\s+", " ");
|
||||||
|
@ -212,4 +213,6 @@ public class StyledText implements TextFragment {
|
||||||
+ ", leftMargin=" + leftMargin + ", rightMargin=" + rightMargin
|
+ ", leftMargin=" + leftMargin + ", rightMargin=" + rightMargin
|
||||||
+ ", baselineOffset=" + baselineOffset + ",rotation=" + rotation + "]";
|
+ ", baselineOffset=" + baselineOffset + ",rotation=" + rotation + "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final Pattern spaces = Pattern.compile("\\s+");
|
||||||
}
|
}
|
||||||
|
|
|
@ -123,9 +123,9 @@ public class TextFlow implements TextSequence, WidthRespecting {
|
||||||
* @return the removed fragment (if any).
|
* @return the removed fragment (if any).
|
||||||
*/
|
*/
|
||||||
public TextFragment removeLast() {
|
public TextFragment removeLast() {
|
||||||
if (text.size() > 0) {
|
if (!text.isEmpty()) {
|
||||||
clearCache();
|
clearCache();
|
||||||
return text.remove(text.size() - 1);
|
return text.removeLast();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -134,9 +134,9 @@ public class TextFlow implements TextSequence, WidthRespecting {
|
||||||
* @return the last added fragment (if any).
|
* @return the last added fragment (if any).
|
||||||
*/
|
*/
|
||||||
public TextFragment getLast() {
|
public TextFragment getLast() {
|
||||||
if (text.size() > 0) {
|
if (!text.isEmpty()) {
|
||||||
clearCache();
|
clearCache();
|
||||||
return text.get(text.size() - 1);
|
return text.getLast();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,15 +60,13 @@ public class TextSequenceUtil {
|
||||||
List<TextLine> lines = getLines(wrapped);
|
List<TextLine> lines = getLines(wrapped);
|
||||||
Paragraph first = new Paragraph();
|
Paragraph first = new Paragraph();
|
||||||
Paragraph tail = new Paragraph();
|
Paragraph tail = new Paragraph();
|
||||||
if (text instanceof TextFlow) {
|
if (text instanceof TextFlow flow) {
|
||||||
TextFlow flow = (TextFlow) text;
|
|
||||||
first.setMaxWidth(flow.getMaxWidth());
|
first.setMaxWidth(flow.getMaxWidth());
|
||||||
first.setLineSpacing(flow.getLineSpacing());
|
first.setLineSpacing(flow.getLineSpacing());
|
||||||
tail.setMaxWidth(flow.getMaxWidth());
|
tail.setMaxWidth(flow.getMaxWidth());
|
||||||
tail.setLineSpacing(flow.getLineSpacing());
|
tail.setLineSpacing(flow.getLineSpacing());
|
||||||
}
|
}
|
||||||
if (text instanceof Paragraph) {
|
if (text instanceof Paragraph paragraph) {
|
||||||
Paragraph paragraph = (Paragraph) text;
|
|
||||||
first.setAlignment(paragraph.getAlignment());
|
first.setAlignment(paragraph.getAlignment());
|
||||||
first.setApplyLineSpacingToFirstLine(paragraph.isApplyLineSpacingToFirstLine());
|
first.setApplyLineSpacingToFirstLine(paragraph.isApplyLineSpacingToFirstLine());
|
||||||
tail.setAlignment(paragraph.getAlignment());
|
tail.setAlignment(paragraph.getAlignment());
|
||||||
|
@ -215,13 +213,11 @@ public class TextSequenceUtil {
|
||||||
} else {
|
} else {
|
||||||
ReplacedWhitespace whitespace = new ReplacedWhitespace(
|
ReplacedWhitespace whitespace = new ReplacedWhitespace(
|
||||||
text.substring(0, splitIndex), word.getFontDescriptor());
|
text.substring(0, splitIndex), word.getFontDescriptor());
|
||||||
StyledText newWord = null;
|
StyledText newWord;
|
||||||
if (word instanceof StyledText) {
|
if (word instanceof StyledText) {
|
||||||
newWord = ((StyledText) word).inheritAttributes(text
|
newWord = ((StyledText) word).inheritAttributes(text.substring(splitIndex));
|
||||||
.substring(splitIndex));
|
|
||||||
} else {
|
} else {
|
||||||
newWord = new StyledText(text.substring(splitIndex),
|
newWord = new StyledText(text.substring(splitIndex), word.getFontDescriptor(), word.getColor());
|
||||||
word.getFontDescriptor(), word.getColor());
|
|
||||||
}
|
}
|
||||||
return new TextFragment[]{newWord, whitespace};
|
return new TextFragment[]{newWord, whitespace};
|
||||||
}
|
}
|
||||||
|
@ -323,25 +319,19 @@ public class TextSequenceUtil {
|
||||||
|
|
||||||
float leftMargin = 0;
|
float leftMargin = 0;
|
||||||
float rightMargin = 0;
|
float rightMargin = 0;
|
||||||
if (word instanceof StyledText) {
|
if (word instanceof StyledText styledText) {
|
||||||
StyledText styledText = (StyledText) word;
|
|
||||||
leftMargin = styledText.getLeftMargin();
|
leftMargin = styledText.getLeftMargin();
|
||||||
rightMargin = styledText.getRightMargin();
|
rightMargin = styledText.getRightMargin();
|
||||||
}
|
}
|
||||||
|
|
||||||
Pair<String> brokenWord = WordBreakerFactory.getWorkBreaker().breakWord(word.getText(), word.getFontDescriptor(),
|
Pair<String> brokenWord = WordBreakerFactory.getWorkBreaker().breakWord(word.getText(), word.getFontDescriptor(),
|
||||||
remainingLineWidth - leftMargin, breakHard);
|
remainingLineWidth - leftMargin, breakHard);
|
||||||
if (brokenWord == null) {
|
if (brokenWord == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// break at calculated index
|
// break at calculated index
|
||||||
TextFragment head = deriveFromExisting(word,
|
TextFragment head = deriveFromExisting(word, brokenWord.getFirst(), leftMargin, 0);
|
||||||
brokenWord.getFirst(), leftMargin, 0);
|
TextFragment tail = deriveFromExisting(word, brokenWord.getSecond(), 0, rightMargin);
|
||||||
TextFragment tail = deriveFromExisting(word,
|
return new Pair<>(head, tail);
|
||||||
brokenWord.getSecond(), 0, rightMargin);
|
|
||||||
|
|
||||||
return new Pair<TextFragment>(head, tail);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue