From b130e1c95c3affdd959a4eb5eb50afd5bef9be0c Mon Sep 17 00:00:00 2001 From: Radek Wikturna Date: Fri, 12 Sep 2025 14:44:49 +0200 Subject: [PATCH] fixed --- .../html/simpleparser/FactoryProperties.java | 4 -- .../com/lowagie/text/html/StylesTest.java | 40 +++++++++++++++++++ .../stylesTest/lineHeightDefault.html | 15 +++++++ .../stylesTest/lineHeightPercentage.html | 15 +++++++ 4 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 openpdf/src/test/resources/stylesTest/lineHeightDefault.html create mode 100644 openpdf/src/test/resources/stylesTest/lineHeightPercentage.html diff --git a/openpdf/src/main/java/com/lowagie/text/html/simpleparser/FactoryProperties.java b/openpdf/src/main/java/com/lowagie/text/html/simpleparser/FactoryProperties.java index d2ad98795..6d59bbcbb 100755 --- a/openpdf/src/main/java/com/lowagie/text/html/simpleparser/FactoryProperties.java +++ b/openpdf/src/main/java/com/lowagie/text/html/simpleparser/FactoryProperties.java @@ -380,10 +380,6 @@ public static void insertStyle(Map h, ChainedProperties cprops) actualFontSize = Markup.DEFAULT_FONT_SIZE; } float v = parseLength(prop.getProperty(key), actualFontSize); - if (ss.endsWith("%")) { - h.put("leading", "0," + (v / 100)); - return; - } if ("normal".equalsIgnoreCase(ss)) { h.put("leading", "0,1.5"); return; diff --git a/openpdf/src/test/java/com/lowagie/text/html/StylesTest.java b/openpdf/src/test/java/com/lowagie/text/html/StylesTest.java index 9ed68fff6..10109c4a0 100644 --- a/openpdf/src/test/java/com/lowagie/text/html/StylesTest.java +++ b/openpdf/src/test/java/com/lowagie/text/html/StylesTest.java @@ -3,6 +3,7 @@ import com.lowagie.text.Chunk; import com.lowagie.text.Document; import com.lowagie.text.Element; +import com.lowagie.text.Font; import com.lowagie.text.Paragraph; import com.lowagie.text.html.simpleparser.HTMLWorker; import com.lowagie.text.html.simpleparser.StyleSheet; @@ -126,4 +127,43 @@ void testNamedFontSize() throws Exception { Chunk chunk10 = (Chunk) paragraph.get(18); Assertions.assertEquals(FontSize.LARGER.getScale() * 20f, chunk10.getFont().getSize()); } + + @Test + void testLineHeightPercentage() throws Exception { + List elements = htmlToPdf("stylesTest/lineHeightPercentage.html", "target/Line Height Percentage.pdf"); + Paragraph paragraph = (Paragraph) elements.get(0); + Chunk chunk = (Chunk) paragraph.get(0); + float fontSize; + if (chunk.getFont() != null) { + fontSize = chunk.getFont().getSize(); + } else { + fontSize = Font.DEFAULTSIZE; + } + float expectedMultiplier = 1.15f; // derived from in lineHeightPercentage.html + float expectedTotalLeading = fontSize * expectedMultiplier; + float totalLeading = paragraph.getTotalLeading(); + + Assertions.assertEquals(expectedTotalLeading, totalLeading, 0.1f, + "Total leading should be ~fontSize * " + expectedMultiplier); + } + + @Test + void testDefaultLineHeight() throws Exception { + List elements = htmlToPdf("stylesTest/lineHeightDefault.html", "target/Line Height Default.pdf"); + Paragraph paragraph = (Paragraph) elements.get(0); + Chunk chunk = (Chunk) paragraph.get(0); + float fontSize; + if (chunk.getFont() != null) { + fontSize = chunk.getFont().getSize(); + } else { + fontSize = Font.DEFAULTSIZE; + } + float expectedMultiplier = 1.5f; + float expectedTotalLeading = fontSize * expectedMultiplier; + float totalLeading = paragraph.getTotalLeading(); + + Assertions.assertEquals(expectedTotalLeading, totalLeading, 0.1f, + "Total leading should be ~fontSize * " + expectedMultiplier); + } + } diff --git a/openpdf/src/test/resources/stylesTest/lineHeightDefault.html b/openpdf/src/test/resources/stylesTest/lineHeightDefault.html new file mode 100644 index 000000000..fe6fa0bde --- /dev/null +++ b/openpdf/src/test/resources/stylesTest/lineHeightDefault.html @@ -0,0 +1,15 @@ + + + + + + + +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eget hendrerit nulla. +Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae. +Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. +Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip. +Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat. + + + diff --git a/openpdf/src/test/resources/stylesTest/lineHeightPercentage.html b/openpdf/src/test/resources/stylesTest/lineHeightPercentage.html new file mode 100644 index 000000000..49b7fca56 --- /dev/null +++ b/openpdf/src/test/resources/stylesTest/lineHeightPercentage.html @@ -0,0 +1,15 @@ + + + + + + + +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam eget hendrerit nulla. +Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae. +Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. +Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip. +Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat. + + +