pdfbox-layout icon indicating copy to clipboard operation
pdfbox-layout copied to clipboard

Text wrapping runs forever when the constraints are impossible to fullfill.

Open ripdajacker opened this issue 7 years ago • 5 comments

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 avatar Jul 13 '17 09:07 ripdajacker

@ripdajacker Is the issue now closed?

Kaylen-Travis-Pillay avatar Nov 18 '17 06:11 Kaylen-Travis-Pillay

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 avatar Nov 22 '17 12:11 ripdajacker

@ripdajacker Which library? Thanks

literakl avatar Dec 27 '17 11:12 literakl

OpenPDF

ripdajacker avatar Dec 27 '17 11:12 ripdajacker

Also ran into this issue

mcantrell avatar Feb 28 '19 15:02 mcantrell