trim only if more than one space

This commit is contained in:
Jörg Prante 2024-10-18 15:27:55 +02:00
parent e81e605887
commit aa08fa45f0
3 changed files with 20 additions and 10 deletions

View file

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

View file

@ -77,7 +77,7 @@ public class StyledText implements TextFragment {
if (rightMargin < 0) {
throw new IllegalArgumentException("rightMargin must be >= 0");
}
this.text = text;
this.text = trimToSingleSpace(text);
this.fontDescriptor = fontDescriptor;
this.color = color;
this.leftMargin = leftMargin;
@ -103,7 +103,7 @@ public class StyledText implements TextFragment {
@Override
public float getWidth() {
if (width == null) {
width = getWidth(getFontDescriptor(), getText());
width = getWidth(getFontDescriptor());
width += leftMargin;
width += rightMargin;
}
@ -123,10 +123,10 @@ public class StyledText implements TextFragment {
if (text == null) {
return 0f;
}
if (text.trim().isEmpty()) {
if (text.isEmpty()) {
return 0f;
}
return getFontDescriptor().getSize() * getFontDescriptor().getSelectedFont(text.trim()).getFontDescriptor().getAscent() / 1000;
return getFontDescriptor().getSize() * getFontDescriptor().getSelectedFont(text).getFontDescriptor().getAscent() / 1000;
}
public float getBaselineOffset() {
@ -180,21 +180,31 @@ public class StyledText implements TextFragment {
return new StyledText(text, getFontDescriptor(), getColor(), getBaselineOffset(), leftMargin, rightMargin, getRotation());
}
private static float getWidth(FontDescriptor fontDescriptor, String text) {
private float getWidth(FontDescriptor fontDescriptor) {
if (text == null) {
return 0f;
}
if (text.trim().isEmpty()) {
if (text.isEmpty()) {
return 0f;
}
try {
return fontDescriptor.getSize() * fontDescriptor.getSelectedFont(text.trim()).getStringWidth(text.trim()) / 1000;
return fontDescriptor.getSize() * fontDescriptor.getSelectedFont(text).getStringWidth(text) / 1000;
} catch (Exception e) {
logger.log(Level.WARNING, "text = '" + text.trim() + "' " + e.getMessage(), e);
logger.log(Level.WARNING, "text = '" + text + "' " + e.getMessage(), e);
return 0f;
}
}
private static String trimToSingleSpace(String string) {
if (string == null) {
return null;
}
if (!string.contains(" ")) {
return string;
}
return string.replaceAll("\\s+", " ");
}
@Override
public String toString() {
return "StyledText [text=" + text + ", fontDescriptor="

View file

@ -211,7 +211,7 @@ public class TextLine implements TextSequence {
matrix = matrix.multiply(transform.getMatrix());
}
// always trim to prevent leading spaces to allow getSelectedFont() to work
String text = styledText.getText().trim();
String text = styledText.getText();
if (!text.isEmpty()) {
contentStream.beginText();
beginText = true;