Skip to content

Commit d6413a8

Browse files
committed
Allow font-size 0, handle different initial font-size values
DEVSIX-9614, DEVSIX-3591
1 parent 0c4794c commit d6413a8

21 files changed

+102
-6
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,9 @@ private FontStyleApplierUtil() {
8989
*/
9090
public static void applyFontStyles(Map<String, String> cssProps, ProcessorContext context,
9191
IStylesContainer stylesContainer, IPropertyContainer element) {
92-
float em = CssDimensionParsingUtils.parseAbsoluteLength(cssProps.get(CssConstants.FONT_SIZE));
93-
float rem = context.getCssContext().getRootFontSize();
94-
if (em != 0) {
95-
element.setProperty(Property.FONT_SIZE, UnitValue.createPointValue(em));
96-
}
92+
final float em = CssDimensionParsingUtils.parseAbsoluteLength(cssProps.get(CssConstants.FONT_SIZE));
93+
final float rem = context.getCssContext().getRootFontSize();
94+
element.setProperty(Property.FONT_SIZE, UnitValue.createPointValue(em));
9795

9896
if (cssProps.get(CssConstants.FONT_FAMILY) != null) {
9997
// TODO DEVSIX-2534

src/main/java/com/itextpdf/html2pdf/css/resolve/DefaultCssResolver.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,19 @@ private Map<String, String> resolveStyles(INode element, CssContext context) {
224224
// Format to 4 decimal places to prevent differences between Java and C#
225225
elementStyles.put(CssConstants.FONT_SIZE, DecimalFormatUtil.formatNumber(absoluteFontSize, "0.####") + CssConstants.PT);
226226
} else {
227-
elementStyles.put(CssConstants.FONT_SIZE, Float.toString(CssDimensionParsingUtils.parseAbsoluteFontSize(elementFontSize)) + CssConstants.PT);
227+
float fontSize;
228+
if (CssTypesValidationUtils.isInitialOrInheritOrUnset(elementFontSize)) {
229+
// font-size is inheritable property so we can always process initial/inherit/unset identically
230+
if (parentFontSizeStr == null) {
231+
fontSize = CssDimensionParsingUtils.parseAbsoluteFontSize(
232+
CssDefaults.getDefaultValue(CssConstants.FONT_SIZE));
233+
} else {
234+
fontSize = CssDimensionParsingUtils.parseAbsoluteLength(parentFontSizeStr);
235+
}
236+
} else {
237+
fontSize = CssDimensionParsingUtils.parseAbsoluteFontSize(elementFontSize);
238+
}
239+
elementStyles.put(CssConstants.FONT_SIZE, Float.toString(fontSize) + CssConstants.PT);
228240
}
229241

230242
// Update root font size

src/test/java/com/itextpdf/html2pdf/HtmlConverterPdfUA1UA2Test.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,16 @@ public void emptyTableDataCellTest(PdfUAConformance conformance) throws IOExcept
371371
convertToUaAndCheckCompliance(conformance, sourceHtml, destinationPdf, cmpPdf, null, true, null);
372372
}
373373

374+
@ParameterizedTest
375+
@MethodSource("conformanceLevels")
376+
public void zeroFontSizeTest(PdfUAConformance conformance) throws IOException, InterruptedException {
377+
String sourceHtml = SOURCE_FOLDER + "zeroFontSize.html";
378+
379+
String cmpPdf = SOURCE_FOLDER + "cmp_zeroFontSizeUa" + conformance.getPart() + ".pdf";
380+
String destinationPdf = DESTINATION_FOLDER + "zeroFontSizeUa" + conformance.getPart() + ".pdf";
381+
convertToUaAndCheckCompliance(conformance, sourceHtml, destinationPdf, cmpPdf, null, true, null);
382+
}
383+
374384
@Test
375385
public void duplicateConformanceLevelAAndUAThrows() {
376386
ConverterProperties converterProperties = new ConverterProperties();

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,25 @@ public void spacesInFontSizeValueTest() throws IOException, InterruptedException
7272
public void defaultFontDiffFontSizeSpanTest() throws IOException, InterruptedException {
7373
convertToPdfAndCompare("defaultFontDiffFontSizeSpan", sourceFolder, destinationFolder);
7474
}
75+
76+
@Test
77+
@LogMessages(messages = {
78+
@LogMessage(messageTemplate = StyledXmlParserLogMessageConstant.INVALID_CSS_PROPERTY_DECLARATION)
79+
})
80+
public void inheritedFontSizeTest() throws IOException, InterruptedException {
81+
convertToPdfAndCompare("inheritedFontSize", sourceFolder, destinationFolder);
82+
}
83+
84+
@Test
85+
public void nestingTagsFontSizeTest() throws IOException, InterruptedException {
86+
convertToPdfAndCompare("nestingTagsFontSize", sourceFolder, destinationFolder);
87+
}
88+
89+
@Test
90+
@LogMessages(messages = {
91+
@LogMessage(messageTemplate = StyledXmlParserLogMessageConstant.INVALID_CSS_PROPERTY_DECLARATION, count = 2)
92+
})
93+
public void garbageFontSizeTest() throws IOException, InterruptedException {
94+
convertToPdfAndCompare("garbageFontSize", sourceFolder, destinationFolder);
95+
}
7596
}
Binary file not shown.
Binary file not shown.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head>
3+
<meta charset="utf-8"/>
4+
<body>
5+
<p style="font-size: 9pt;font-family: Arial; line-height: 14pt;">Some Text.</p>
6+
<p style="font-size: 0px;font-family: Arial;">text2</p>
7+
</body>
8+
</html>
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)