From 0564b2e934d5f413dccc75677045cf0f75c32aac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Mon, 18 Nov 2024 17:37:10 +0100 Subject: [PATCH] try to fix wrong space recognition in StyledText --- gradle.properties | 2 +- .../pdfbox/layout/text/StyledText.java | 7 +++-- .../graphics/pdfbox/layout/text/TextFlow.java | 8 +++--- .../pdfbox/layout/text/TextSequenceUtil.java | 28 ++++++------------- 4 files changed, 19 insertions(+), 26 deletions(-) diff --git a/gradle.properties b/gradle.properties index 269761c..0bdba54 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ group = org.xbib.graphics name = graphics -version = 5.5.4 +version = 5.5.5 diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/text/StyledText.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/text/StyledText.java index 1a6b3f5..d7e13ff 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/text/StyledText.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/text/StyledText.java @@ -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+"); } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/text/TextFlow.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/text/TextFlow.java index bb14896..04ed5bb 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/text/TextFlow.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/text/TextFlow.java @@ -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; } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/text/TextSequenceUtil.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/text/TextSequenceUtil.java index a7d6e90..e3613bb 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/text/TextSequenceUtil.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/text/TextSequenceUtil.java @@ -60,15 +60,13 @@ public class TextSequenceUtil { List 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 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(head, tail); + TextFragment head = deriveFromExisting(word, brokenWord.getFirst(), leftMargin, 0); + TextFragment tail = deriveFromExisting(word, brokenWord.getSecond(), 0, rightMargin); + return new Pair<>(head, tail); } /**