Skip to content

Commit b885384

Browse files
committed
#482 #491 #483 Fix infinite loop bug with soft-hypen plus inserted hyphen overflowing line.
The core problem seems to be the under-reporting of width when we have a soft hyphen that is found to be unbreakable.
1 parent ba8dc5c commit b885384

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
lines changed

openhtmltopdf-core/src/main/java/com/openhtmltopdf/layout/Breaker.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,9 @@ public static LineBreakResult breakText(LayoutContext c,
173173
context.setEnd(savedEnd);
174174
continue LOOP;
175175
} else {
176-
if (context.getWidth() == 0) {
177-
String calculatedSubstring = context.getCalculatedSubstring();
178-
if (calculatedSubstring.chars().allMatch(ch -> ch == SOFT_HYPHEN)) {
179-
// Consists only of soft hypen, we have to break here and skip all togheter. We do not
180-
// need to try breaking on the charater level, this will not work.
181-
tryToBreakAnywhere = true;
182-
}
183-
}
184176
// Else, retry it on a new line.
177+
// FIXME: This is very dangerous and has led to infinite
178+
// loops. Needs review.
185179
context.setEnd(savedEnd);
186180
break LOOP;
187181
}
@@ -501,15 +495,16 @@ public static LineBreakResult doBreakText(
501495
context.setEnd(context.getStart() + current.left);
502496
context.setUnbreakable(true);
503497

504-
if (current.left == currentString.length()) {
498+
if (current.isSoftHyphenBreak) {
499+
context.setWidth(current.withHyphenGraphicsLength);
500+
} else if (current.left == currentString.length()) {
505501
String text = context.getCalculatedSubstring();
506502
float extraSpacing = text.length() * letterSpacing;
507503
context.setWidth((int) (c.getTextRenderer().getWidth(
508504
c.getFontContext(), font, text) + extraSpacing));
509505
} else {
510506
context.setWidth(current.graphicsLength);
511507
}
512-
513508
return LineBreakResult.WORD_BREAKING_UNBREAKABLE;
514509
}
515510
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<html>
22
<head>
33
<style>
4-
* {
5-
font-family: 'Liberation Sans', sans-serif;
6-
margin: 0;
7-
padding: 0;
8-
}
9-
4+
@page {
5+
size: 30px 40px;
6+
margin: 0;
7+
padding: 0;
8+
}
109
.content {
10+
font-family: 'Liberation Sans', sans-serif;
1111
word-wrap: break-word;
12-
white-space:pre-wrap;
12+
width: 10px;
1313
}
1414
</style>
1515
</head>
1616
<body>
17-
<table class="content"><tbody><tr><td width="10">­</td></tr></tbody></table>
17+
<div class="content">­</div>
1818
</body>
19-
</html>
19+
</html>

openhtmltopdf-examples/src/test/java/com/openhtmltopdf/visualregressiontests/VisualRegressionTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1154,8 +1154,12 @@ public Reader getReader() {
11541154
})));
11551155
}
11561156

1157+
/**
1158+
* Tests another endless loop bug when the font reports a non-zero width
1159+
* for soft hyphen and this overflows the width of the box in break-word
1160+
* mode.
1161+
*/
11571162
@Test
1158-
@Ignore
11591163
public void testIssue482InfiniteLoopTable() throws IOException {
11601164
assertTrue(vt.runTest("issue-482-infinite-loop-table", builder -> {
11611165
builder.useFont(() -> VisualRegressionTest.class.getClassLoader().getResourceAsStream("org/apache/pdfbox/resources/ttf/LiberationSans-Regular.ttf"),

0 commit comments

Comments
 (0)