From 5585a8f37fcd2e1aeeab70b2b862759e03ca374d Mon Sep 17 00:00:00 2001 From: Max Kasperowski Date: Mon, 21 Oct 2024 17:59:52 +0200 Subject: [PATCH] Fix bug when last word of current line is reached in wrapping --- .../management/SoftWrappingLabelManager.java | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/plugins/de.cau.cs.kieler.klighd/src/de/cau/cs/kieler/klighd/labels/management/SoftWrappingLabelManager.java b/plugins/de.cau.cs.kieler.klighd/src/de/cau/cs/kieler/klighd/labels/management/SoftWrappingLabelManager.java index 8f5b55ad1..7a4e091d2 100644 --- a/plugins/de.cau.cs.kieler.klighd/src/de/cau/cs/kieler/klighd/labels/management/SoftWrappingLabelManager.java +++ b/plugins/de.cau.cs.kieler.klighd/src/de/cau/cs/kieler/klighd/labels/management/SoftWrappingLabelManager.java @@ -85,22 +85,24 @@ public Result doResizeLabel(final ElkLabel label, final double targetWidth) { // Check whether next line would be below the fuzzy threshold int previewWordIndex = currWordIndex; - String previewLineText = words[previewWordIndex]; - testText = previewLineText; - do { - previewLineText = testText; - if (previewWordIndex < words.length - 1) { - testText = previewLineText + " " + words[++previewWordIndex]; - } else { - testText = " "; - previewWordIndex++; + if (previewWordIndex < words.length) { + String previewLineText = words[previewWordIndex]; + testText = previewLineText; + do { + previewLineText = testText; + if (previewWordIndex < words.length - 1) { + testText = previewLineText + " " + words[++previewWordIndex]; + } else { + testText = " "; + previewWordIndex++; + } + lineWidth = PlacementUtil.estimateTextSize(font, testText).getWidth(); + } while (lineWidth < effectiveTargetWidth && previewWordIndex < words.length); + if (lineWidth < effectiveTargetWidth * getFuzzyness(label)) { + // next line would contain too much whitespace so append it to this line + currWordIndex = previewWordIndex; + currentLineText += " " + previewLineText; } - lineWidth = PlacementUtil.estimateTextSize(font, testText).getWidth(); - } while (lineWidth < effectiveTargetWidth && previewWordIndex < words.length); - if (lineWidth < effectiveTargetWidth * getFuzzyness(label)) { - // next line would contain too much whitespace so append it to this line - currWordIndex = previewWordIndex; - currentLineText += " " + previewLineText; } // No more words fit so the line is added to the result