pdfbox-layout
pdfbox-layout copied to clipboard
Text wrapping runs forever when the constraints are impossible to fullfill.
I found an issue in the word breaker logic in the project.
Given a word that will never fit in the given constraints, the word breaker runs forever.
It sprung out when using the Paragraph
class, but I figured dove into it and found the bug in the WordBreakers
class logic.
The tests below show the error at hand.
@Test(timeout = 5_000L)
public void wordBreakerTerminatesWhenAttemptingImpossibleWrap() throws Exception {
Paragraph paragraph = new Paragraph();
paragraph.add(new StyledText("N", 12, PDType1Font.COURIER));
List<TextLine> lines = TextSequenceUtil.wordWrapToLines(paragraph, 2);
Assert.assertEquals(1, lines.size());
TextLine line = lines.get(0);
Assert.assertEquals("N", line.getStyledTexts().get(0).getText());
}
The above runs forever because the wrap function returns a pair Pair("", "N")
.
I wrote another test verifying this closer to the word breaking logic:
@Test
public void wordBreakerHasEmptyStringAsTheSecondPairWhenAttemptingImpossibleWrap() throws Exception {
FontDescriptor fontDescriptor = new FontDescriptor(PDType1Font.COURIER, 5);
WordBreaker workBreaker = WordBreakerFactory.getWorkBreaker();
Pair<String> broken = workBreaker.breakWord("H", fontDescriptor, 1, true);
Assert.assertEquals("The first entry should be 'H'", "H", broken.getFirst());
Assert.assertEquals("The second entry should be the empty string", "", broken.getSecond());
}
@ripdajacker Is the issue now closed?
It's still an issue. I ended up using a different library in my project.
The pull request I've sent should give you an indication of what the cause may be.
@ripdajacker Which library? Thanks
OpenPDF
Also ran into this issue