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

View file

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