From 836712194cbb3593972a768124dbff524ebcfb78 Mon Sep 17 00:00:00 2001 From: "Hugh.C" Date: Tue, 9 Jun 2020 09:58:58 +0800 Subject: [PATCH 1/2] =?UTF-8?q?REPORT-33028=20=E6=97=A0=E6=8F=92=E4=BB=B6P?= =?UTF-8?q?DF=E5=AF=BC=E5=87=BA-HTML=E8=A7=A3=E6=9E=90=EF=BC=9A=E5=9D=97?= =?UTF-8?q?=E5=85=83=E7=B4=A0=E6=94=AF=E6=8C=81margin=E3=80=81padding=20?= =?UTF-8?q?=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fr/third/v2/lowagie/text/html/Markup.java | 41 ++++++++++++ .../html/simpleparser/FactoryProperties.java | 62 +++++++++++++++---- .../text/html/simpleparser/HtmlConstants.java | 10 +-- .../third/v2/lowagie/text/pdf/PdfChunk.java | 3 + 4 files changed, 100 insertions(+), 16 deletions(-) diff --git a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/Markup.java b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/Markup.java index 6137f1326..d49494bbb 100644 --- a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/Markup.java +++ b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/Markup.java @@ -53,7 +53,10 @@ package com.fr.third.v2.lowagie.text.html; import java.awt.Color; +import java.util.ArrayList; +import java.util.List; import java.util.Properties; +import java.util.StringTokenizer; /** * A class that contains all the possible tagnames and their attributes. @@ -510,4 +513,42 @@ public class Markup { return result.toString(); } + /** + * 解析类似于 margin: 10px 5px 15px 20px,中的值 + * NESW:北东南西(上右下左) + * + * @param attr 如10px 5px 15px 20px + * @return list: 按上左下右的顺序存放 ,要么四个方位的值都存在,要么都不存在(返回null) + */ + public static List parseNESW(String attr) { + if (null == attr) { + return null; + } + List list = new ArrayList(4); + StringTokenizer st = new StringTokenizer(attr); + while (st.hasMoreElements()) { + list.add(st.nextToken()); + } + switch (list.size()) { + case 1: + for (int i = 0; i < 3; i++) { + list.add(list.get(0)); + } + break; + case 2: + list.add(list.get(0)); + list.add(list.get(1)); + break; + case 3: + list.add(list.get(1)); + break; + case 4: + break; + default: + list = null; + break; + } + return list; + } + } \ No newline at end of file diff --git a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java index a8a89b93d..bcb8d1726 100644 --- a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java +++ b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java @@ -53,6 +53,7 @@ package com.fr.third.v2.lowagie.text.html.simpleparser; import java.awt.Color; import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Properties; import java.util.StringTokenizer; import com.fr.third.v2.lowagie.text.Chunk; @@ -143,18 +144,9 @@ public class FactoryProperties { if (props.hasProperty("noexist-attrid")) { p.setAttribute("noexist-attrid", props.getProperty("noexist-attrid")); } - for(String margin : HtmlConstants.MARGIN){ - if(props.hasPropertyInChain("div", margin)){ - String ss = props.getPropertyFromChain("div", margin); - p.setAttribute(margin, ss); - } - } - for(String padding : HtmlConstants.PADDING){ - if(props.hasPropertyInChain("div", padding)){ - String ss = props.getPropertyFromChain("div", padding); - p.setAttribute(padding, ss); - } - } + + parseMarginAttr( p, props); + parsePaddingAttr( p, props); if(props.hasPropertyInChain("div", "text-indent")){ String ss = props.getPropertyFromChain("div", "text-indent"); @@ -190,6 +182,52 @@ public class FactoryProperties { } } + /** + * 解析margin属性 + * + * @param p + * @param props + */ + private static void parseMarginAttr(Paragraph p, ChainedProperties props) { + if (props.hasPropertyInChain("div", Markup.CSS_KEY_MARGIN)) { + List list = Markup.parseNESW(props.getPropertyFromChain("div", Markup.CSS_KEY_MARGIN)); + if (null != list && 4 == list.size()) { + for (int i = 0; i < HtmlConstants.MARGIN.size() && i < list.size(); i++) { + p.setAttribute(HtmlConstants.MARGIN.get(i), list.get(i)); + } + } + } + for (String margin : HtmlConstants.MARGIN) { + if (props.hasPropertyInChain("div", margin)) { + String ss = props.getPropertyFromChain("div", margin); + p.setAttribute(margin, ss); + } + } + } + + /** + * 解析padding属性 + * + * @param p + * @param props + */ + private static void parsePaddingAttr(Paragraph p, ChainedProperties props) { + if (props.hasPropertyInChain("div", Markup.CSS_KEY_PADDING)) { + List list = Markup.parseNESW(props.getPropertyFromChain("div", Markup.CSS_KEY_PADDING)); + if (null != list) { + for (int i = 0; i < HtmlConstants.PADDING.size() && i < list.size(); i++) { + p.setAttribute(HtmlConstants.PADDING.get(i), list.get(i)); + } + } + } + for (String padding : HtmlConstants.PADDING) { + if (props.hasPropertyInChain("div", padding)) { + String ss = props.getPropertyFromChain("div", padding); + p.setAttribute(padding, ss); + } + } + } + public static Paragraph createParagraph(ChainedProperties props) { Paragraph p = new Paragraph(); createParagraph(p, props); diff --git a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/HtmlConstants.java b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/HtmlConstants.java index 7a94e1d51..a86cb9205 100644 --- a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/HtmlConstants.java +++ b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/HtmlConstants.java @@ -33,13 +33,15 @@ public class HtmlConstants { INLINE_ELEMENTS.add(HtmlTags.I); INLINE_ELEMENTS.add(HtmlTags.U); INLINE_ELEMENTS.add(HtmlTags.EM); - PADDING.add(Markup.CSS_KEY_PADDINGLEFT); - PADDING.add(Markup.CSS_KEY_PADDINGRIGHT); + PADDING.add(Markup.CSS_KEY_PADDINGTOP); + PADDING.add(Markup.CSS_KEY_PADDINGRIGHT); PADDING.add(Markup.CSS_KEY_PADDINGBOTTOM); - MARGIN.add(Markup.CSS_KEY_MARGINLEFT); - MARGIN.add(Markup.CSS_KEY_MARGINRIGHT); + PADDING.add(Markup.CSS_KEY_PADDINGLEFT); + MARGIN.add(Markup.CSS_KEY_MARGINTOP); + MARGIN.add(Markup.CSS_KEY_MARGINRIGHT); MARGIN.add(Markup.CSS_KEY_MARGINBOTTOM); + MARGIN.add(Markup.CSS_KEY_MARGINLEFT); } } diff --git a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java index 44a02083e..c666a1093 100644 --- a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java +++ b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java @@ -872,6 +872,9 @@ public class PdfChunk { if (font.isUnderlined()) { s.append("text-decoration :underline;"); } + if (font.isStrikethru()) { + s.append("text-decoration :line-through;"); + } } public String javaColor2JSColorWithAlpha(Color var0) { From 4e834caf0f6c9c6a332caaae6556a2c9e5863333 Mon Sep 17 00:00:00 2001 From: "Hugh.C" Date: Tue, 9 Jun 2020 11:42:12 +0800 Subject: [PATCH 2/2] =?UTF-8?q?REPORT-33028=20review=20=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/fr/third/v2/lowagie/text/html/Markup.java | 5 +++++ .../v2/lowagie/text/html/simpleparser/FactoryProperties.java | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/Markup.java b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/Markup.java index d49494bbb..a059dd05a 100644 --- a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/Markup.java +++ b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/Markup.java @@ -531,20 +531,25 @@ public class Markup { } switch (list.size()) { case 1: + //如 margin:1px ,代表上下左右 的margin值都是 1px for (int i = 0; i < 3; i++) { list.add(list.get(0)); } break; case 2: + //如 margin:1px 2px ,代表上下方位的margin值为1px, 左右方位的margin值为 2px list.add(list.get(0)); list.add(list.get(1)); break; case 3: + //如 margin:1px 2px 3px ,代表上下方位的margin值分别为1px、3px, 左右方位的margin值为 2px list.add(list.get(1)); break; case 4: + //如 margin:1px 2px 3px 4px,就不用管了 break; default: + //如 margin:1px 2px 3px 4px 5px,超了就违反了规则,在浏览器中该属性失效 list = null; break; } diff --git a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java index bcb8d1726..0b29f4d01 100644 --- a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java +++ b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java @@ -214,7 +214,7 @@ public class FactoryProperties { private static void parsePaddingAttr(Paragraph p, ChainedProperties props) { if (props.hasPropertyInChain("div", Markup.CSS_KEY_PADDING)) { List list = Markup.parseNESW(props.getPropertyFromChain("div", Markup.CSS_KEY_PADDING)); - if (null != list) { + if (null != list && 4 == list.size()) { for (int i = 0; i < HtmlConstants.PADDING.size() && i < list.size(); i++) { p.setAttribute(HtmlConstants.PADDING.get(i), list.get(i)); }