Skip to content

Commit 7788e66

Browse files
LodrKumquatyulian-gaponenko
authored andcommitted
Support word-break property
DEVSIX-4422
1 parent 5315d19 commit 7788e66

File tree

9 files changed

+36
-21
lines changed

9 files changed

+36
-21
lines changed

src/main/java/com/itextpdf/html2pdf/css/apply/util/FontStyleApplierUtil.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@ This file is part of the iText (R) project.
6262
import com.itextpdf.layout.property.TransparentColor;
6363
import com.itextpdf.layout.property.Underline;
6464
import com.itextpdf.layout.property.UnitValue;
65+
import com.itextpdf.layout.splitting.DefaultSplitCharacters;
6566
import com.itextpdf.styledxmlparser.css.util.CssTypesValidationUtils;
6667
import com.itextpdf.styledxmlparser.css.util.CssDimensionParsingUtils;
68+
import com.itextpdf.layout.splitting.BreakAllSplitCharacters;
69+
import com.itextpdf.layout.splitting.KeepAllSplitCharacters;
6770
import com.itextpdf.styledxmlparser.node.IElementNode;
6871
import com.itextpdf.styledxmlparser.node.IStylesContainer;
6972

@@ -174,6 +177,23 @@ public static void applyFontStyles(Map<String, String> cssProps, ProcessorContex
174177
} else {
175178
element.setProperty(Property.OVERFLOW_WRAP, OverflowWrapPropertyValue.NORMAL);
176179
}
180+
181+
String wordBreak = cssProps.get(CssConstants.WORD_BREAK);
182+
if (CssConstants.BREAK_ALL.equals(wordBreak)) {
183+
element.setProperty(Property.SPLIT_CHARACTERS, new BreakAllSplitCharacters());
184+
} else if (CssConstants.KEEP_ALL.equals(wordBreak)) {
185+
element.setProperty(Property.SPLIT_CHARACTERS, new KeepAllSplitCharacters());
186+
} else if (CssConstants.BREAK_WORD.equals(wordBreak)) {
187+
// CSS specification cite that describes the reason for overflow-wrap overriding:
188+
// "For compatibility with legacy content, the word-break property also supports
189+
// a deprecated break-word keyword. When specified, this has the same effect
190+
// as word-break: normal and overflow-wrap: anywhere, regardless of the actual value
191+
// of the overflow-wrap property."
192+
element.setProperty(Property.OVERFLOW_WRAP, OverflowWrapPropertyValue.BREAK_WORD);
193+
element.setProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters());
194+
} else {
195+
element.setProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters());
196+
}
177197
}
178198

179199
float [] colors = new float[4];

src/test/java/com/itextpdf/html2pdf/css/WordBreakTest.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public static void beforeClass() {
5353
}
5454

5555
@Test
56-
// TODO: update cmp file after implementing DEVSIX-1438
5756
public void wordBreakCommonScenarioTest() throws IOException, InterruptedException {
5857
FontProvider fontProvider = new DefaultFontProvider();
5958
fontProvider.addFont(fontsFolder + "NotoSansCJKjp-Regular.otf");
@@ -68,7 +67,6 @@ public void wordBreakCommonScenarioTest() throws IOException, InterruptedExcepti
6867
}
6968

7069
@Test
71-
// TODO: update cmp file after implementing DEVSIX-1438
7270
public void overflowXWordBreakTest() throws IOException, InterruptedException {
7371
FontProvider fontProvider = new DefaultFontProvider();
7472
fontProvider.addFont(fontsFolder + "NotoSansCJKjp-Regular.otf");
@@ -83,7 +81,6 @@ public void overflowXWordBreakTest() throws IOException, InterruptedException {
8381
}
8482

8583
@Test
86-
// TODO: update cmp file after implementing DEVSIX-1438
8784
public void whiteSpaceAndWordBreakTest() throws IOException, InterruptedException {
8885
HtmlConverter.convertToPdf(new File(sourceFolder + "whiteSpaceAndWordBreak.html"),
8986
new File(destinationFolder + "whiteSpaceAndWordBreak.pdf"));
@@ -92,7 +89,6 @@ public void whiteSpaceAndWordBreakTest() throws IOException, InterruptedExceptio
9289
}
9390

9491
@Test
95-
// TODO: update cmp file after implementing DEVSIX-1438
9692
public void wordBreakMidNumbersTest() throws IOException, InterruptedException {
9793
FontProvider fontProvider = new DefaultFontProvider();
9894
fontProvider.addFont(fontsFolder + "NotoSansCJKjp-Regular.otf");
@@ -107,7 +103,6 @@ public void wordBreakMidNumbersTest() throws IOException, InterruptedException {
107103
}
108104

109105
@Test
110-
// TODO: update cmp file after implementing DEVSIX-1438
111106
public void wordBreakMidPunctuationTest() throws IOException, InterruptedException {
112107
HtmlConverter.convertToPdf(new File(sourceFolder + "wordBreakMidPunctuation.html"),
113108
new File(destinationFolder + "wordBreakMidPunctuation.pdf"));
@@ -116,7 +111,6 @@ public void wordBreakMidPunctuationTest() throws IOException, InterruptedExcepti
116111
}
117112

118113
@Test
119-
// TODO: update cmp file after implementing DEVSIX-1438
120114
public void wordBreakAllAndFloatTest() throws IOException, InterruptedException {
121115
HtmlConverter.convertToPdf(new File(sourceFolder + "wordBreakAllAndFloat.html"),
122116
new File(destinationFolder + "wordBreakAllAndFloat.pdf"));
@@ -127,7 +121,6 @@ public void wordBreakAllAndFloatTest() throws IOException, InterruptedException
127121
@Test
128122
@LogMessages(messages = {@LogMessage(messageTemplate = LogMessageConstant
129123
.TABLE_WIDTH_IS_MORE_THAN_EXPECTED_DUE_TO_MIN_WIDTH, count = 3)})
130-
// TODO: update cmp file after implementing DEVSIX-1438
131124
public void wordBreakTableScenarioTest() throws IOException, InterruptedException {
132125
HtmlConverter.convertToPdf(new File(sourceFolder + "wordBreakTableScenario.html"),
133126
new File(destinationFolder + "wordBreakTableScenario.pdf"));

src/test/resources/com/itextpdf/html2pdf/css/WordBreakTest/wordBreakMidNumbers.html

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,29 @@
2525
</head>
2626
<body>
2727
<h3>1. word-break: normal</h3>
28-
<p class="normal">Lorem 12345.678912345 -6789 ipsum dolor sit ametconsecteturadipiscingelitseddoeiusmodtempor incididunt ut labore et dolore magna aliqua</p>
28+
<p class="normal">Lorem 1234.6789.23 -6789 ipsum dolor sit ametconsecteturadipiscingelitseddoeiusmodtempor incididunt ut labore et dolore magna aliqua</p>
2929

3030
<h3>2. word-break: break-all</h3>
31-
<p class="breakAll">Lorem 12345.678912345 -6789 ipsum dolor sit ametconsecteturadipiscingelitseddoeiusmodtempor incididunt ut labore et dolore magna aliqua</p>
31+
<p class="breakAll">Lorem 1234.6789.23 -6789 ipsum dolor sit ametconsecteturadipiscingelitseddoeiusmodtempor incididunt ut labore et dolore magna aliqua</p>
3232

3333
<h3>3. word-break: keep-all</h3>
34-
<p class="keepAll">Lorem 12345.678912345 -6789 ipsum dolor sit ametconsecteturadipiscingelitseddoeiusmodtempor incididunt ut labore et dolore magna aliqua</p>
34+
<p class="keepAll">Lorem 1234.6789.23 -6789 ipsum dolor sit ametconsecteturadipiscingelitseddoeiusmodtempor incididunt ut labore et dolore magna aliqua</p>
3535

3636
<h3>4. word-break: break-word</h3>
37-
<p class="breakWord">Lorem 12345.678912345-6789 ipsum dolor sit ametconsecteturadipiscingelitseddoeiusmodtempor incididunt ut labore et dolore magna aliqua</p>
37+
<p class="breakWord">Lorem 1234.6789.23 -6789 ipsum dolor sit ametconsecteturadipiscingelitseddoeiusmodtempor incididunt ut labore et dolore magna aliqua</p>
3838

3939
<h3>Chinese script</h3>
40-
<h3>1. word-break: break-word</h3>
41-
<p class="breakWord">只不过是绝大多数人生来就有12345.678912345 -6789根手指这样一个解剖学事实的结果</p>
42-
<h3>1. word-break: keep-all</h3>
43-
<p class="keepAll">只不过是绝大多数人生来就有12345.678912345 -6789根手指这样一个解剖学事实的结果</p>
44-
<h3>1. word-break: break-all</h3>
45-
<p class="breakAll">只不过是绝大多数人生来就有12345.678912345 -6789根手指这样一个解剖学事实的结果</p>
4640
<h3>1. word-break: normal</h3>
4741
<p class="normal">只不过是绝大多数人生来就有12345.678912345 -6789根手指这样一个解剖学事实的结果</p>
4842

43+
<h3>1. word-break: break-all</h3>
44+
<p class="breakAll">只不过是绝大多数人生来就有12345.678912345 -6789根手指这样一个解剖学事实的结果</p>
45+
46+
<h3>1. word-break: keep-all</h3>
47+
<p class="keepAll">只不过是绝大多数人生来就有12345.678912345 -6789根手指这样一个解剖学事实的结果</p>
48+
49+
<h3>1. word-break: break-word</h3>
50+
<p class="breakWord">只不过是绝大多数人生来就有12345.678912345 -6789根手指这样一个解剖学事实的结果</p>
4951

5052
</body>
5153
</html>

src/test/resources/com/itextpdf/html2pdf/css/WordBreakTest/wordBreakMidPunctuation.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525
</head>
2626
<body>
2727
<h3>1. word-break: normal</h3>
28-
<p class="normal">Lorem ipsum dolor, sit ametconsecteturadipiscingelitseddoeiusmodtempor? Incididunt, - ut labore et dolore magna. Aliqua??????</p>
28+
<p class="normal">Lorem ipsum dolor, sit ametconse.....................cte$$$$$$$$$$dipiscingelitsed--------------eiusmodtempor?? Aliqua????????????</p>
2929

3030
<h3>2. word-break: break-all</h3>
31-
<p class="breakAll">Lorem ipsum dolor, sit ametconsecteturadipiscingelitseddoeiusmodtempor? Incididunt, - ut labore et dolore magna. Aliqua??????</p>
31+
<p class="breakAll">Lorem ipsum dolor, sit ametconse.....................cte$$$$$$$$$$dipiscingelitsed--------------eiusmodtempor?? Aliqua????????????</p>
3232

3333
<h3>3. word-break: keep-all</h3>
34-
<p class="keepAll">Lorem ipsum dolor, sit ametconsecteturadipiscingelitseddoeiusmodtempor? Incididunt, - ut labore et dolore magna. Aliqua??????</p>
34+
<p class="keepAll">Lorem ipsum dolor, sit ametconse.....................cte$$$$$$$$$$dipiscingelitsed--------------eiusmodtempor?? Aliqua????????????</p>
3535

3636
<h3>4. word-break: break-word</h3>
37-
<p class="breakWord">Lorem ipsum dolor, sit ametconsecteturadipiscingelitseddoeiusmodtempor? Incididunt, - ut labore et dolore magna. Aliqua??????</p>
37+
<p class="breakWord">Lorem ipsum dolor, sit ametconse.....................cte$$$$$$$$$$dipiscingelitsed--------------eiusmodtempor?? Aliqua????????????</p>
3838
</body>
3939
</html>

0 commit comments

Comments
 (0)