From 17c2807732b2dd7705d935d6cc01ab414081c123 Mon Sep 17 00:00:00 2001 From: kerry Date: Fri, 18 May 2018 12:01:18 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-6702=20=E6=B7=BB=E5=8A=A0margin=E5=92=8C?= =?UTF-8?q?padding=E5=B1=9E=E6=80=A7=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../v2/lowagie/text/html/IndentAttribute.java | 9 ++- .../text/html/ParseIndentAttrUtils.java | 77 +++++++++++++++++++ .../html/simpleparser/FactoryProperties.java | 49 +++++++----- .../text/html/simpleparser/HTMLWorker.java | 4 + .../text/html/simpleparser/HtmlConstants.java | 19 +++-- .../third/v2/lowagie/text/pdf/PdfChunk.java | 34 +++++--- .../fr/third/v2/lowagie/text/pdf/PdfLine.java | 8 +- 7 files changed, 160 insertions(+), 40 deletions(-) create mode 100644 fine-itext/src/com/fr/third/v2/lowagie/text/html/ParseIndentAttrUtils.java diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/html/IndentAttribute.java b/fine-itext/src/com/fr/third/v2/lowagie/text/html/IndentAttribute.java index 4f9ef628b..c254cca56 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/html/IndentAttribute.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/html/IndentAttribute.java @@ -42,7 +42,14 @@ public class IndentAttribute { this.right = right; } - public IndentAttribute(){ + public IndentAttribute(float top, float left, float bottom, float right){ + this.top = top; + this.left = left; + this.bottom = bottom; + this.right = right; } + public IndentAttribute(){ + + } } diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/html/ParseIndentAttrUtils.java b/fine-itext/src/com/fr/third/v2/lowagie/text/html/ParseIndentAttrUtils.java new file mode 100644 index 000000000..1eeafb1e4 --- /dev/null +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/html/ParseIndentAttrUtils.java @@ -0,0 +1,77 @@ +package com.fr.third.v2.lowagie.text.html; + +import java.util.Iterator; +import java.util.Map; + +/** + * @author kerry + * @date 2018/5/14 + */ +public class ParseIndentAttrUtils { + public static IndentAttribute parseSpace(String indent){ + String[] a = indent.split(" "); + if(a.length == 0){ + return new IndentAttribute(); + } + float indentTop = Markup.parseLength(a[0]); + if(a.length == 1){ + return new IndentAttribute(indentTop, indentTop, indentTop, indentTop); + } + float indentLeft = Markup.parseLength(a[1]); + if(a.length == 2){ + return new IndentAttribute(indentTop, indentLeft, indentTop, indentLeft); + } + float indentBottom = Markup.parseLength(a[2]); + if(a.length == 3){ + return new IndentAttribute(indentTop, indentLeft, indentBottom, indentLeft); + } + float indentRight = Markup.parseLength(a[3]); + return new IndentAttribute(indentTop, indentLeft, indentBottom, indentRight); + } + + public static IndentAttribute parsePaddingAttribute(Map map){ + IndentAttribute indentAttribute = new IndentAttribute(); + Iterator iter = map.entrySet().iterator(); + while (iter.hasNext()) { + Map.Entry entry = (Map.Entry) iter.next(); + String key = (String) entry.getKey(); + if (CSS.Property.PADDING_LEFT.equals(key)) { + indentAttribute.setLeft(Markup.parseLength((String) entry.getValue())); + } else if (CSS.Property.PADDING_RIGHT.equals(key)) { + indentAttribute.setRight(Markup.parseLength((String) entry.getValue())); + } else if (CSS.Property.PADDING_TOP.equals(key)) { + indentAttribute.setTop(Markup.parseLength((String) entry.getValue())); + } else if (CSS.Property.PADDING_BOTTOM.equals(key)) { + indentAttribute.setBottom(Markup.parseLength((String) entry.getValue())); + } + } + return indentAttribute; + } + + public static IndentAttribute parseMarginAttribute(Map map){ + IndentAttribute indentAttribute = new IndentAttribute(); + Iterator iter = map.entrySet().iterator(); + while (iter.hasNext()) { + Map.Entry entry = (Map.Entry) iter.next(); + String key = (String) entry.getKey(); + float val = Markup.parseLength((String) entry.getValue()); + if (CSS.Property.MARGIN_LEFT.equals(key)) { + indentAttribute.setLeft(val); + } else if (CSS.Property.MARGIN_RIGHT.equals(key)) { + indentAttribute.setRight(val); + } else if (CSS.Property.MARGIN_TOP.equals(key)) { + indentAttribute.setTop(val); + } else if (CSS.Property.MARGIN_BOTTOM.equals(key)) { + indentAttribute.setBottom(val); + } + } + return indentAttribute; + } + + public static void createIndent(StringBuffer stringBuffer, String attr, IndentAttribute indentAttribute){ + stringBuffer.append(attr).append("-").append("left").append(":").append(indentAttribute.getLeft()).append("px;") + .append(attr).append("-").append("right").append(":").append(indentAttribute.getRight()).append("px;") + .append(attr).append("-").append("top").append(":").append(indentAttribute.getTop()).append("px;") + .append(attr).append("-").append("bottom").append(":").append(indentAttribute.getBottom()).append("px;"); + } +} \ No newline at end of file diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java b/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java index b5137b14d..4a59fc066 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java @@ -53,8 +53,6 @@ package com.fr.third.v2.lowagie.text.html.simpleparser; import java.awt.Color; import java.util.HashMap; import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.Map; import java.util.Properties; import java.util.StringTokenizer; import com.fr.third.v2.lowagie.text.Chunk; @@ -66,7 +64,8 @@ import com.fr.third.v2.lowagie.text.FontFactoryImp; import com.fr.third.v2.lowagie.text.ListItem; import com.fr.third.v2.lowagie.text.Paragraph; import com.fr.third.v2.lowagie.text.html.HtmlTags; -import com.fr.third.v2.lowagie.text.pdf.BaseFont; +import com.fr.third.v2.lowagie.text.html.IndentAttribute; +import com.fr.third.v2.lowagie.text.html.ParseIndentAttrUtils; import com.fr.third.v2.lowagie.text.pdf.HyphenationAuto; import com.fr.third.v2.lowagie.text.pdf.HyphenationEvent; import com.fr.third.v2.lowagie.text.html.Markup; @@ -96,13 +95,11 @@ public class FactoryProperties { if(props.hasPropertyInChain("span", "background")){ ck.setBackground(props.getPropertyFromChain("span", "background")); } - if(props.hasPropertyInChain("span", "padding-left")){ - String ss = props.getPropertyFromChain("span", "padding-left"); - ck.setAttribute("padding-left", Markup.parseLength(ss)); - } - if(props.hasPropertyInChain("span", "padding-right")){ - String ss = props.getPropertyFromChain("span", "padding-right"); - ck.setAttribute("padding-right", Markup.parseLength(ss)); + for(String padding : HtmlConstants.PADDING){ + if(props.hasPropertyInChain("span", padding)){ + String ss = props.getPropertyFromChain("span", padding); + ck.setAttribute(padding, ss); + } } return ck; } @@ -140,26 +137,32 @@ public class FactoryProperties { if(props.hasPropertyInChain("div", "background")){ p.setBackground(props.getPropertyFromChain("div", "background")); } - if(props.hasPropertyInChain("div", "padding-left")){ - String ss = props.getPropertyFromChain("div", "padding-left"); - p.setIndentationLeft(Markup.parseLength(ss)); + for(String margin : HtmlConstants.MARGIN){ + if(props.hasPropertyInChain("div", margin)){ + String ss = props.getPropertyFromChain("div", margin); + p.setAttribute(margin, ss); + } } - if(props.hasPropertyInChain("div", "padding-right")){ - String ss = props.getPropertyFromChain("div", "padding-right"); - p.setIndentationRight(Markup.parseLength(ss)); + for(String padding : HtmlConstants.PADDING){ + if(props.hasPropertyInChain("div", padding)){ + String ss = props.getPropertyFromChain("div", padding); + p.setAttribute(padding, ss); + } } + if(props.hasPropertyInChain("div", "text-indent")){ String ss = props.getPropertyFromChain("div", "text-indent"); p.setFirstLineIndent(Markup.parseLength(ss)); } if(props.hasPropertyInChain("div", "width")){ String ss = props.getPropertyFromChain("div", "width"); - p.setAttribute("width", Markup.parseLength(ss)); + p.setAttribute("width", ss); } if(props.hasPropertyInChain("div", "height")){ String ss = props.getPropertyFromChain("div", "height"); - p.setAttribute("height", Markup.parseLength(ss)); + p.setAttribute("height", ss); } + p.setHyphenation(getHyphenation(props)); setParagraphLeading(p, props.getProperty("leading")); value = props.getProperty("before"); @@ -340,7 +343,15 @@ public class FactoryProperties { else { h.put("leading", v + ",0"); } - } else if (key.equals(Markup.CSS_KEY_TEXTALIGN)) { + } else if (key.equals(Markup.CSS_KEY_PADDING)) { + String ss = prop.getProperty(key).trim().toLowerCase(); + IndentAttribute indentAttribute = ParseIndentAttrUtils.parseSpace(ss); + h.put(Markup.CSS_KEY_PADDINGTOP, indentAttribute.getTop() + "px"); + h.put(Markup.CSS_KEY_PADDINGLEFT, indentAttribute.getLeft() + "px"); + h.put(Markup.CSS_KEY_PADDINGBOTTOM, indentAttribute.getBottom() + "px"); + h.put(Markup.CSS_KEY_PADDINGRIGHT, indentAttribute.getRight() + "px"); + } + else if (key.equals(Markup.CSS_KEY_TEXTALIGN)) { String ss = prop.getProperty(key).trim().toLowerCase(); h.put("align", ss); } diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java b/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java index 22d0a6853..dd0fcf43e 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java @@ -191,6 +191,10 @@ public class HTMLWorker implements SimpleXMLDocHandler, DocListener { return; try { style.applyStyle(tag, h); + if(tag.equals("p")){ + h.put(Markup.CSS_KEY_MARGINTOP, "16px"); + h.put(Markup.CSS_KEY_MARGINBOTTOM, "16px"); + } String follow = (String) FactoryProperties.followTags.get(tag); if (follow != null) { HashMap prop = new HashMap(); diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HtmlConstants.java b/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HtmlConstants.java index 1ed96f0d3..7a94e1d51 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HtmlConstants.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HtmlConstants.java @@ -1,8 +1,10 @@ package com.fr.third.v2.lowagie.text.html.simpleparser; import com.fr.third.v2.lowagie.text.html.HtmlTags; +import com.fr.third.v2.lowagie.text.html.Markup; import java.util.ArrayList; +import java.util.List; /** * @author kerry @@ -10,13 +12,12 @@ import java.util.ArrayList; */ public class HtmlConstants { //块级元素集合 - public static final ArrayList BLOCK_ELEMENTS = new ArrayList(); + public static final List BLOCK_ELEMENTS = new ArrayList(); //行内元素集合 - public static final ArrayList INLINE_ELEMENTS = new ArrayList(); + public static final List INLINE_ELEMENTS = new ArrayList(); //支持解析的属性 - - public static final String PADDING_LEFT = "padding-left"; - public static final String PADDING_RIGHT = "padding-right"; + public static final List PADDING = new ArrayList(); + public static final List MARGIN = new ArrayList(); static { BLOCK_ELEMENTS.add(HtmlTags.DIV); BLOCK_ELEMENTS.add(HtmlTags.UNORDEREDLIST); @@ -32,5 +33,13 @@ 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_PADDINGBOTTOM); + MARGIN.add(Markup.CSS_KEY_MARGINLEFT); + MARGIN.add(Markup.CSS_KEY_MARGINRIGHT); + MARGIN.add(Markup.CSS_KEY_MARGINTOP); + MARGIN.add(Markup.CSS_KEY_MARGINBOTTOM); } } diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java index d2c2552e6..f8523074e 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java @@ -61,7 +61,7 @@ import com.fr.third.v2.lowagie.text.Chunk; import com.fr.third.v2.lowagie.text.Font; import com.fr.third.v2.lowagie.text.Image; import com.fr.third.v2.lowagie.text.html.IndentAttribute; -import com.fr.third.v2.lowagie.text.html.simpleparser.HtmlConstants; +import com.fr.third.v2.lowagie.text.html.ParseIndentAttrUtils; import sun.font.FontDesignMetrics; /** @@ -115,8 +115,6 @@ public class PdfChunk { keysAttributes.put(Chunk.HSCALE, null); keysAttributes.put(Chunk.SEPARATOR, null); keysAttributes.put(Chunk.TAB, null); - keysAttributes.put(HtmlConstants.PADDING_LEFT, null); - keysAttributes.put(HtmlConstants.PADDING_RIGHT, null); keysNoStroke.put(Chunk.SUBSUPSCRIPT, null); keysNoStroke.put(Chunk.SPLITCHARACTER, null); keysNoStroke.put(Chunk.HYPHENATION, null); @@ -267,14 +265,9 @@ public class PdfChunk { // the color can't be stored in a PdfFont noStroke.put(Chunk.COLOR, f.getColor()); // noStroke.put(Chunk.ENCODING, font.getFont().getEncoding()); - if(attributes.get("padding-left") != null){ - String paddingLeft = (String)attributes.get("padding-left"); - indent.setLeft(Float.parseFloat(paddingLeft)); - } - if(attributes.get("padding-right") != null){ - String paddingRight = (String)attributes.get("padding-right"); - indent.setRight(Float.parseFloat(paddingRight)); - } + + indent = ParseIndentAttrUtils.parsePaddingAttribute(attr); + Object obj[] = (Object[])attributes.get(Chunk.IMAGE); if (obj == null) { image = null; @@ -837,7 +830,7 @@ public class PdfChunk { public void dealFontStyle(StringBuffer s, Font font) { s.append("font-size:").append(font.getSize()).append("px;"); s.append("font-family :").append(font.getFontName()).append(";"); - s.append("color :").append(font.getColor()).append(";"); + s.append("color :").append(javaColor2JSColorWithAlpha(font.getColor())).append(";"); if (font.isBold()) { s.append("font-weight : bold;"); } @@ -849,6 +842,23 @@ public class PdfChunk { } } + public String javaColor2JSColorWithAlpha(Color var0) { + if(var0 == null) { + return ""; + } else { + StringBuffer var1 = new StringBuffer("rgba("); + var1.append(var0.getRed()); + var1.append(','); + var1.append(var0.getGreen()); + var1.append(','); + var1.append(var0.getBlue()); + var1.append(','); + var1.append((double)var0.getAlpha() / 255.0D); + var1.append(')'); + return var1.toString(); + } + } + public static boolean noPrint(int c) { return ((c >= 0x200b && c <= 0x200f) || (c >= 0x202a && c <= 0x202e)); } diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfLine.java b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfLine.java index c854a5ef2..b499cc8ca 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfLine.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfLine.java @@ -192,10 +192,12 @@ public class PdfLine { return overflow; } // if the chunk couldn't even be truncated, we add everything, so be it - else { - if (overflow != null) - addToLine(overflow); + else if (overflow != null){ + addToLine(overflow); return null; + }else{ + addToLine(chunk); + return chunk; } } else {