Skip to content

Commit 6d3b6b9

Browse files
committed
#420 #421 Test, divide by 0 check and refactoring around justification.
+ Further test for justified text with nested non-justified text. + Defence against divide by zero error. + Remove unneeded casts in justify method. + Add comments around #421 + Fixes #420
1 parent 19887e0 commit 6d3b6b9

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed

openhtmltopdf-core/src/main/java/com/openhtmltopdf/render/InlineText.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,10 @@ public float calcTotalAdjustment(JustificationInfo info) {
314314
}
315315

316316
if (_counts == null) {
317+
// This will only happen for non-justifiable text nested inside
318+
// justifiable text (eg. white-space: pre).
319+
// Therefore the correct answer is 0.
320+
// See InlineLayoutBox#countJustifiableChars.
317321
return 0f;
318322
}
319323

openhtmltopdf-core/src/main/java/com/openhtmltopdf/render/LineBox.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import com.openhtmltopdf.layout.Layer;
4343
import com.openhtmltopdf.layout.LayoutContext;
4444
import com.openhtmltopdf.layout.PaintingInfo;
45-
import com.openhtmltopdf.render.FlowingColumnContainerBox.ColumnBreakStore;
4645
import com.openhtmltopdf.util.XRRuntimeException;
4746

4847
/**
@@ -217,7 +216,6 @@ public void align(boolean dynamic, CssContext c) {
217216
}
218217

219218
public void justify(CssContext c) {
220-
221219
if (getParent().getStyle().hasLetterSpacing()) {
222220
// Do nothing, letter-spacing turns off text justification.
223221
} else if (! isLastLineWithContent()) {
@@ -239,19 +237,22 @@ public void justify(CssContext c) {
239237

240238
if (counts.getSpaceCount() > 0) {
241239
if (counts.getNonSpaceCount() > 1) {
242-
info.setNonSpaceAdjust(Math.min((float)toAdd * JUSTIFY_NON_SPACE_SHARE / (counts.getNonSpaceCount()-1), maxInterChar));
240+
info.setNonSpaceAdjust(Math.min(toAdd * JUSTIFY_NON_SPACE_SHARE / (counts.getNonSpaceCount() - 1), maxInterChar));
243241
} else {
244242
info.setNonSpaceAdjust(0.0f);
245243
}
246244

247245
if (counts.getSpaceCount() > 0) {
248-
info.setSpaceAdjust(Math.min((float)toAdd * JUSTIFY_SPACE_SHARE / counts.getSpaceCount(), maxInterWord));
246+
info.setSpaceAdjust(Math.min(toAdd * JUSTIFY_SPACE_SHARE / counts.getSpaceCount(), maxInterWord));
249247
} else {
250248
info.setSpaceAdjust(0.0f);
251249
}
252-
} else {
250+
} else if (counts.getNonSpaceCount() > 1) {
253251
info.setSpaceAdjust(0f);
254252
info.setNonSpaceAdjust(Math.min((float) toAdd / (counts.getNonSpaceCount() - 1), maxInterChar));
253+
} else {
254+
info.setSpaceAdjust(0f);
255+
info.setNonSpaceAdjust(0f);
255256
}
256257

257258
adjustChildren(info);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<html>
2+
<head>
3+
<style>
4+
@page {
5+
size: 100px 200px;
6+
margin: 0;
7+
}
8+
body {
9+
margin: 0;
10+
padding: 0;
11+
}
12+
</style>
13+
</head>
14+
<body>
15+
<p style="text-align: justify;">
16+
This is some text with a <br/><span style="white-space: pre;">white</span> space pre in the middle. Will it justify correctly?
17+
</p>
18+
</body>
19+
</html>

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,10 +924,24 @@ public void testIssue399TableHeaderFooterWithNoRows() throws IOException {
924924
}
925925

926926

927+
/**
928+
* Tests that justified text with non-justified content (br) nested inside it
929+
* will not throw a NPE.
930+
* https://github.com/danfickle/openhtmltopdf/issues/420
931+
*/
927932
@Test
928933
public void testIssue420JustifyTextNullPointerException() throws IOException {
929934
assertTrue(vt.runTest("issue-420-justify-text-null-pointer-exception"));
930935
}
936+
937+
/**
938+
* Tests that justified text with non-justified content nested inside it
939+
* correctly justifies.
940+
*/
941+
@Test
942+
public void testIssue420JustifyTextWhiteSpacePre() throws IOException {
943+
assertTrue(vt.runTest("issue-420-justify-text-white-space-pre"));
944+
}
931945

932946
// TODO:
933947
// + Elements that appear just on generated overflow pages.

0 commit comments

Comments
 (0)