Browse Source

Merge pull request #44 in CORE/base-third from ~KERRY/base-third:feature/10.0 to feature/10.0

* commit '6619d84f6fd62d6d3089843c80eb2cbb8d8ee86f':
  无
  REPORT-8032 && REPORT-7875 && REPORT-8030 && REPORT-7873 base64img解析,style基本样式和上下标解析
10.0
superman 7 years ago
parent
commit
ee41bf2704
  1. 2
      fine-itext/src/com/fr/third/v2/lowagie/text/Chunk.java
  2. 11
      fine-itext/src/com/fr/third/v2/lowagie/text/Image.java
  3. 18
      fine-itext/src/com/fr/third/v2/lowagie/text/Paragraph.java
  4. 199
      fine-itext/src/com/fr/third/v2/lowagie/text/html/CSS.java
  5. 193
      fine-itext/src/com/fr/third/v2/lowagie/text/html/CSSUtils.java
  6. 48
      fine-itext/src/com/fr/third/v2/lowagie/text/html/IndentAttribute.java
  7. 51
      fine-itext/src/com/fr/third/v2/lowagie/text/html/Markup.java
  8. 43
      fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java
  9. 91
      fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java
  10. 7
      fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HtmlConstants.java
  11. 76
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java
  12. 3
      fine-itext/src/com/fr/third/v2/lowagie/text/xml/simpleparser/SimpleXMLParser.java

2
fine-itext/src/com/fr/third/v2/lowagie/text/Chunk.java

@ -441,7 +441,7 @@ public class Chunk implements Element {
* @return this <CODE>Chunk</CODE>
*/
private Chunk setAttribute(String name, Object obj) {
public Chunk setAttribute(String name, Object obj) {
if (attributes == null)
attributes = new HashMap();
attributes.put(name, obj);

11
fine-itext/src/com/fr/third/v2/lowagie/text/Image.java

@ -218,6 +218,16 @@ public abstract class Image extends Rectangle {
// image from file or URL
private String srcString = "";
public String getSrcString() {
return srcString;
}
public void setSrcString(String srcString) {
this.srcString = srcString;
}
/**
* Constructs an <CODE>Image</CODE> -object, using an <VAR>url </VAR>.
*
@ -915,6 +925,7 @@ public abstract class Image extends Rectangle {
super(image);
this.type = image.type;
this.url = image.url;
this.srcString = image.srcString;
this.rawData = image.rawData;
this.bpc = image.bpc;
this.template = image.template;

18
fine-itext/src/com/fr/third/v2/lowagie/text/Paragraph.java

@ -49,6 +49,8 @@
package com.fr.third.v2.lowagie.text;
import java.util.HashMap;
/**
* A <CODE>Paragraph</CODE> is a series of <CODE>Chunk</CODE>s and/or <CODE>Phrases</CODE>.
* <P>
@ -106,6 +108,22 @@ public class Paragraph extends Phrase {
protected String background;
protected HashMap attributes = new HashMap();
public HashMap getAttributes() {
return attributes;
}
public void setAttributes(HashMap attributes) {
this.attributes = attributes;
}
public void setAttribute(String name, Object obj) {
if (attributes == null)
attributes = new HashMap();
attributes.put(name, obj);
}
// constructors
/**

199
fine-itext/src/com/fr/third/v2/lowagie/text/html/CSS.java

@ -0,0 +1,199 @@
package com.fr.third.v2.lowagie.text.html;
/**
* @author kerry
* @date 2018/5/11
*/
public class CSS{
public final class Value {
private Value(){};
public static final String THIN = "thin";
public static final String MEDIUM = "medium";
public static final String THICK = "thick";
public static final String NONE = "none";
public static final String HIDDEN = "hidden";
public static final String DOTTED = "dotted";
public static final String DASHED = "dashed";
public static final String SOLID = "solid";
public static final String DOUBLE = "double";
public static final String GROOVE = "groove";
public static final String RIDGE = "ridge";
public static final String INSET = "inset";
public static final String OUTSET = "outset";
public static final String LEFT = "left";
public static final String CENTER = "center";
public static final String JUSTIFY = "justify";
public static final String BOTTOM = "bottom";
public static final String TOP = "top";
public static final String RIGHT = "right";
public static final String REPEAT = "repeat";
public static final String NO_REPEAT = "no-repeat";
public static final String REPEAT_X = "repeat-x";
public static final String REPEAT_Y = "repeat-y";
public static final String FIXED = "fixed";
public static final String SCROLL = "scroll";
public static final String DISC = "disc";
public static final String SQUARE = "square";
public static final String CIRCLE = "circle";
public static final String DECIMAL = "decimal";
public static final String LOWER_ROMAN = "lower-roman";
public static final String UPPER_ROMAN = "upper-roman";
public static final String LOWER_GREEK = "lower-greek";
public static final String UPPER_GREEK = "upper-greek";
public static final String LOWER_ALPHA = "lower-alpha";
public static final String UPPER_ALPHA = "upper-alpha";
public static final String LOWER_LATIN = "lower-latin";
public static final String UPPER_LATIN = "upper-latin";
public static final String INSIDE = "inside";
public static final String OUTSIDE = "outside";
public static final String INHERIT = "inherit";
public static final String UNDERLINE = "underline";
public static final String BOLD = "bold";
public static final String ITALIC = "italic";
public static final String OBLIQUE = "oblique";
public static final String SUPER = "super";
public static final String SUB = "sub";
public static final String TEXT_TOP = "text-top";
public static final String TEXT_BOTTOM = "text-bottom";
public static final String LINE_THROUGH = "line-through";
public static final String RELATIVE = "relative";
public static final String HIDE = "hide";
public static final String XX_SMALL = "xx-small";
public static final String X_SMALL = "x-small";
public static final String SMALL = "small";
public static final String LARGE = "large";
public static final String X_LARGE = "x-large";
public static final String XX_LARGE = "xx-large";
public static final String SMALLER = "smaller";
public static final String LARGER = "larger";
public static final String PX = "px";
public static final String IN = "in";
public static final String CM = "cm";
public static final String MM = "mm";
public static final String PT = "pt";
public static final String PC = "pc";
public static final String PERCENTAGE = "%";
public static final String EM = "em";
public static final String EX = "ex";
public static final String ALWAYS = "always";
public static final String AVOID = "avoid";
public static final String ABSOLUTE = "absolute";
public static final String AUTO = "auto";
public static final String INLINE = "inline";
public static final String BLOCK = "block";
public static final String SEPARATE = "separate";
public static final String COLLAPSE = "collapse";
public static final String RTL = "rtl";
public static final String LTR = "ltr";
public static final String INLINE_BLOCK = "inline-block";
public static final String INLINE_TABLE = "inline-table";
public static final String LIST_ITEM = "list-item";
public static final String RUN_IN = "run-in";
public static final String TABLE = "table";
public static final String TABLE_CAPTION = "table-caption";
public static final String TABLE_CELL = "table-cell";
public static final String TABLE_COLUMN_GROUP = "table-column-group";
public static final String TABLE_COLUMN = "table-column";
public static final String TABLE_FOOTER_GROUP = "table-footer-group";
public static final String TABLE_HEADER_GROUP = "table-header-group";
public static final String TABLE_ROW = "table-row";
public static final String TABLE_ROW_GROUP = "table-row-group";
}
public final class Property {
private Property() {};
public static final String BACKGROUND = "background";
public static final String BACKGROUND_IMAGE = "background-image";
public static final String BACKGROUND_REPEAT = "background-repeat";
public static final String BACKGROUND_ATTACHMENT = "background-attachment";
public static final String BACKGROUND_POSITION = "background-position";
public static final String BACKGROUND_COLOR = "background-color";
public static final String LIST_STYLE = "list-style";
public static final String LIST_STYLE_TYPE = "list-style-type";
public static final String LIST_STYLE_POSITION = "list-style-position";
public static final String LIST_STYLE_IMAGE = "list-style-image";
public static final String MARGIN = "margin";
public static final String TOP = "top";
public static final String MARGIN_LEFT = "margin-left";
public static final String MARGIN_RIGHT = "margin-right";
public static final String MARGIN_TOP = "margin-top";
public static final String MARGIN_BOTTOM = "margin-bottom";
public static final String BORDER = "border";
public static final String BORDER_LEFT = "border-left";
public static final String BORDER_TOP = "border-top";
public static final String BORDER_RIGHT = "border-right";
public static final String BORDER_BOTTOM = "border-bottom";
public static final String BORDER_WIDTH = "border-width";
public static final String BORDER_STYLE = "border-style";
public static final String BORDER_COLOR = "border-color";
public static final String BORDER_COLLAPSE = "border-collapse";
public static final String BORDER_SPACING = "border-spacing";
public static final String BORDER_TOP_WIDTH = "border-top-width";
public static final String BORDER_BOTTOM_WIDTH = "border-bottom-width";
public static final String BORDER_LEFT_WIDTH = "border-left-width";
public static final String BORDER_RIGHT_WIDTH = "border-right-width";
public static final String BORDER_TOP_COLOR = "border-top-color";
public static final String BORDER_BOTTOM_COLOR = "border-bottom-color";
public static final String BORDER_LEFT_COLOR = "border-left-color";
public static final String BORDER_RIGHT_COLOR = "border-right-color";
public static final String BORDER_TOP_STYLE = "border-top-style";
public static final String BORDER_BOTTOM_STYLE = "border-bottom-style";
public static final String BORDER_LEFT_STYLE = "border-left-style";
public static final String BORDER_RIGHT_STYLE = "border-right-style";
public static final String PADDING = "padding";
public static final String PADDING_TOP = "padding-top";
public static final String PADDING_BOTTOM = "padding-bottom";
public static final String PADDING_LEFT = "padding-left";
public static final String PADDING_RIGHT = "padding-right";
public static final String FONT = "font";
public static final String FONT_WEIGHT = "font-weight";
public static final String FONT_SIZE = "font-size";
public static final String FONT_STYLE = "font-style";
public static final String FONT_FAMILY = "font-family";
public static final String TEXT_DECORATION = "text-decoration";
public static final String COLOR = "color";
public static final String TAB_INTERVAL = "tab-interval";
public static final String XFA_TAB_COUNT = "xfa-tab-count";
public static final String XFA_FONT_HORIZONTAL_SCALE = "xfa-font-horizontal-scale";
public static final String XFA_FONT_VERTICAL_SCALE = "xfa-font-vertical-scale";
public static final String BEFORE = "before";
public static final String AFTER = "after";
public static final String HEIGHT = "height";
public static final String WIDTH = "width";
public static final String LETTER_SPACING = "letter-spacing";
public static final String VERTICAL_ALIGN = "vertical-align";
public static final String LINE_HEIGHT = "line-height";
public static final String TEXT_ALIGN = "text-align";
public static final String TEXT_VALIGN = "text-valign";
public static final String TEXT_INDENT = "text-indent";
public static final String POSITION = "position";
public static final String EMPTY_CELLS = "empty-cells";
public static final String CELLPADDING = "cellpadding";
//deprecated
public static final String CELLPADDING_LEFT = "cellpadding-left";
public static final String CELLPADDING_TOP = "cellpadding-top";
public static final String CELLPADDING_RIGHT = "cellpadding-right";
public static final String CELLPADDING_BOTTOM = "cellpadding-bottom";
public static final String CAPTION_SIDE = "caption-side";
public static final String TAB_STOPS = "tab-stops";
public static final String XFA_TAB_STOPS = "xfa-tab-stops";
public static final String PAGE_BREAK_BEFORE = "page-break-before";
public static final String PAGE_BREAK_INSIDE = "page-break-inside";
public static final String PAGE_BREAK_AFTER = "page-break-after";
public static final String REPEAT_HEADER = "repeat-header";
public static final String REPEAT_FOOTER = "repeat-footer";
public static final String LEFT = "left";
public static final String DISPLAY = "display";
public static final String MIN_WIDTH = "min-width";
public static final String MAX_WIDTH = "max-width";
public static final String MIN_HEIGHT = "min-height";
public static final String MAX_HEIGHT = "max-height";
public static final String RIGHT = "right";
public static final String BOTTOM = "bottom";
public static final String FLOAT = "float";
public static final String DIRECTION = "direction";
}
}

193
fine-itext/src/com/fr/third/v2/lowagie/text/html/CSSUtils.java

@ -12,199 +12,6 @@ import java.util.Set;
*/
public class CSSUtils {
public class CSS{
public final class Value {
private Value(){};
public static final String THIN = "thin";
public static final String MEDIUM = "medium";
public static final String THICK = "thick";
public static final String NONE = "none";
public static final String HIDDEN = "hidden";
public static final String DOTTED = "dotted";
public static final String DASHED = "dashed";
public static final String SOLID = "solid";
public static final String DOUBLE = "double";
public static final String GROOVE = "groove";
public static final String RIDGE = "ridge";
public static final String INSET = "inset";
public static final String OUTSET = "outset";
public static final String LEFT = "left";
public static final String CENTER = "center";
public static final String JUSTIFY = "justify";
public static final String BOTTOM = "bottom";
public static final String TOP = "top";
public static final String RIGHT = "right";
public static final String REPEAT = "repeat";
public static final String NO_REPEAT = "no-repeat";
public static final String REPEAT_X = "repeat-x";
public static final String REPEAT_Y = "repeat-y";
public static final String FIXED = "fixed";
public static final String SCROLL = "scroll";
public static final String DISC = "disc";
public static final String SQUARE = "square";
public static final String CIRCLE = "circle";
public static final String DECIMAL = "decimal";
public static final String LOWER_ROMAN = "lower-roman";
public static final String UPPER_ROMAN = "upper-roman";
public static final String LOWER_GREEK = "lower-greek";
public static final String UPPER_GREEK = "upper-greek";
public static final String LOWER_ALPHA = "lower-alpha";
public static final String UPPER_ALPHA = "upper-alpha";
public static final String LOWER_LATIN = "lower-latin";
public static final String UPPER_LATIN = "upper-latin";
public static final String INSIDE = "inside";
public static final String OUTSIDE = "outside";
public static final String INHERIT = "inherit";
public static final String UNDERLINE = "underline";
public static final String BOLD = "bold";
public static final String ITALIC = "italic";
public static final String OBLIQUE = "oblique";
public static final String SUPER = "super";
public static final String SUB = "sub";
public static final String TEXT_TOP = "text-top";
public static final String TEXT_BOTTOM = "text-bottom";
public static final String LINE_THROUGH = "line-through";
public static final String RELATIVE = "relative";
public static final String HIDE = "hide";
public static final String XX_SMALL = "xx-small";
public static final String X_SMALL = "x-small";
public static final String SMALL = "small";
public static final String LARGE = "large";
public static final String X_LARGE = "x-large";
public static final String XX_LARGE = "xx-large";
public static final String SMALLER = "smaller";
public static final String LARGER = "larger";
public static final String PX = "px";
public static final String IN = "in";
public static final String CM = "cm";
public static final String MM = "mm";
public static final String PT = "pt";
public static final String PC = "pc";
public static final String PERCENTAGE = "%";
public static final String EM = "em";
public static final String EX = "ex";
public static final String ALWAYS = "always";
public static final String AVOID = "avoid";
public static final String ABSOLUTE = "absolute";
public static final String AUTO = "auto";
public static final String INLINE = "inline";
public static final String BLOCK = "block";
public static final String SEPARATE = "separate";
public static final String COLLAPSE = "collapse";
public static final String RTL = "rtl";
public static final String LTR = "ltr";
public static final String INLINE_BLOCK = "inline-block";
public static final String INLINE_TABLE = "inline-table";
public static final String LIST_ITEM = "list-item";
public static final String RUN_IN = "run-in";
public static final String TABLE = "table";
public static final String TABLE_CAPTION = "table-caption";
public static final String TABLE_CELL = "table-cell";
public static final String TABLE_COLUMN_GROUP = "table-column-group";
public static final String TABLE_COLUMN = "table-column";
public static final String TABLE_FOOTER_GROUP = "table-footer-group";
public static final String TABLE_HEADER_GROUP = "table-header-group";
public static final String TABLE_ROW = "table-row";
public static final String TABLE_ROW_GROUP = "table-row-group";
}
public final class Property {
private Property() {};
public static final String BACKGROUND = "background";
public static final String BACKGROUND_IMAGE = "background-image";
public static final String BACKGROUND_REPEAT = "background-repeat";
public static final String BACKGROUND_ATTACHMENT = "background-attachment";
public static final String BACKGROUND_POSITION = "background-position";
public static final String BACKGROUND_COLOR = "background-color";
public static final String LIST_STYLE = "list-style";
public static final String LIST_STYLE_TYPE = "list-style-type";
public static final String LIST_STYLE_POSITION = "list-style-position";
public static final String LIST_STYLE_IMAGE = "list-style-image";
public static final String MARGIN = "margin";
public static final String TOP = "top";
public static final String MARGIN_LEFT = "margin-left";
public static final String MARGIN_RIGHT = "margin-right";
public static final String MARGIN_TOP = "margin-top";
public static final String MARGIN_BOTTOM = "margin-bottom";
public static final String BORDER = "border";
public static final String BORDER_LEFT = "border-left";
public static final String BORDER_TOP = "border-top";
public static final String BORDER_RIGHT = "border-right";
public static final String BORDER_BOTTOM = "border-bottom";
public static final String BORDER_WIDTH = "border-width";
public static final String BORDER_STYLE = "border-style";
public static final String BORDER_COLOR = "border-color";
public static final String BORDER_COLLAPSE = "border-collapse";
public static final String BORDER_SPACING = "border-spacing";
public static final String BORDER_TOP_WIDTH = "border-top-width";
public static final String BORDER_BOTTOM_WIDTH = "border-bottom-width";
public static final String BORDER_LEFT_WIDTH = "border-left-width";
public static final String BORDER_RIGHT_WIDTH = "border-right-width";
public static final String BORDER_TOP_COLOR = "border-top-color";
public static final String BORDER_BOTTOM_COLOR = "border-bottom-color";
public static final String BORDER_LEFT_COLOR = "border-left-color";
public static final String BORDER_RIGHT_COLOR = "border-right-color";
public static final String BORDER_TOP_STYLE = "border-top-style";
public static final String BORDER_BOTTOM_STYLE = "border-bottom-style";
public static final String BORDER_LEFT_STYLE = "border-left-style";
public static final String BORDER_RIGHT_STYLE = "border-right-style";
public static final String PADDING = "padding";
public static final String PADDING_TOP = "padding-top";
public static final String PADDING_BOTTOM = "padding-bottom";
public static final String PADDING_LEFT = "padding-left";
public static final String PADDING_RIGHT = "padding-right";
public static final String FONT = "font";
public static final String FONT_WEIGHT = "font-weight";
public static final String FONT_SIZE = "font-size";
public static final String FONT_STYLE = "font-style";
public static final String FONT_FAMILY = "font-family";
public static final String TEXT_DECORATION = "text-decoration";
public static final String COLOR = "color";
public static final String TAB_INTERVAL = "tab-interval";
public static final String XFA_TAB_COUNT = "xfa-tab-count";
public static final String XFA_FONT_HORIZONTAL_SCALE = "xfa-font-horizontal-scale";
public static final String XFA_FONT_VERTICAL_SCALE = "xfa-font-vertical-scale";
public static final String BEFORE = "before";
public static final String AFTER = "after";
public static final String HEIGHT = "height";
public static final String WIDTH = "width";
public static final String LETTER_SPACING = "letter-spacing";
public static final String VERTICAL_ALIGN = "vertical-align";
public static final String LINE_HEIGHT = "line-height";
public static final String TEXT_ALIGN = "text-align";
public static final String TEXT_VALIGN = "text-valign";
public static final String TEXT_INDENT = "text-indent";
public static final String POSITION = "position";
public static final String EMPTY_CELLS = "empty-cells";
public static final String CELLPADDING = "cellpadding";
//deprecated
public static final String CELLPADDING_LEFT = "cellpadding-left";
public static final String CELLPADDING_TOP = "cellpadding-top";
public static final String CELLPADDING_RIGHT = "cellpadding-right";
public static final String CELLPADDING_BOTTOM = "cellpadding-bottom";
public static final String CAPTION_SIDE = "caption-side";
public static final String TAB_STOPS = "tab-stops";
public static final String XFA_TAB_STOPS = "xfa-tab-stops";
public static final String PAGE_BREAK_BEFORE = "page-break-before";
public static final String PAGE_BREAK_INSIDE = "page-break-inside";
public static final String PAGE_BREAK_AFTER = "page-break-after";
public static final String REPEAT_HEADER = "repeat-header";
public static final String REPEAT_FOOTER = "repeat-footer";
public static final String LEFT = "left";
public static final String DISPLAY = "display";
public static final String MIN_WIDTH = "min-width";
public static final String MAX_WIDTH = "max-width";
public static final String MIN_HEIGHT = "min-height";
public static final String MAX_HEIGHT = "max-height";
public static final String RIGHT = "right";
public static final String BOTTOM = "bottom";
public static final String FLOAT = "float";
public static final String DIRECTION = "direction";
}
}
private static final Set<String> backgroundPositions = new HashSet<String>(
Arrays.asList(new String[] { CSS.Value.LEFT, CSS.Value.CENTER, CSS.Value.BOTTOM, CSS.Value.TOP, CSS.Value.RIGHT }));

48
fine-itext/src/com/fr/third/v2/lowagie/text/html/IndentAttribute.java

@ -0,0 +1,48 @@
package com.fr.third.v2.lowagie.text.html;
/**
* @author kerry
* @date 2018/5/9
*/
public class IndentAttribute {
private float top = 0.0f;
private float bottom = 0.0f;
private float left = 0.0f;
private float right = 0.0f;
public float getTop() {
return top;
}
public void setTop(float top) {
this.top = top;
}
public float getBottom() {
return bottom;
}
public void setBottom(float bottom) {
this.bottom = bottom;
}
public float getLeft() {
return left;
}
public void setLeft(float left) {
this.left = left;
}
public float getRight() {
return right;
}
public void setRight(float right) {
this.right = right;
}
public IndentAttribute(){
}
}

51
fine-itext/src/com/fr/third/v2/lowagie/text/html/Markup.java

@ -54,7 +54,6 @@ package com.fr.third.v2.lowagie.text.html;
import java.awt.Color;
import java.util.Properties;
import java.util.StringTokenizer;
/**
* A class that contains all the possible tagnames and their attributes.
@ -441,29 +440,45 @@ public class Markup {
Properties result = new Properties();
if (string == null)
return result;
StringTokenizer keyValuePairs = new StringTokenizer(string, ";");
StringTokenizer keyValuePair;
String key;
String value;
while (keyValuePairs.hasMoreTokens()) {
keyValuePair = new StringTokenizer(keyValuePairs.nextToken(), ":");
if (keyValuePair.hasMoreTokens())
key = keyValuePair.nextToken().trim();
else
continue;
if (keyValuePair.hasMoreTokens())
value = keyValuePair.nextToken().trim();
else
continue;
if (value.startsWith("\""))
value = value.substring(1);
if (value.endsWith("\""))
value = value.substring(0, value.length() - 1);
result.setProperty(key.toLowerCase(), value);
//换种方式解析,style中background里面也有可能会有;符号
String[] styles = string.split(";");
for (String s : styles) {
String[] part = s.split(":",2);
if (part.length == 2) {
key = stripDoubleSpacesAndTrim(part[0]).toLowerCase();
value = stripDoubleSpacesAndTrim(part[1]);
if (value.startsWith("\""))
value = value.substring(1);
if (value.endsWith("\""))
value = value.substring(0, value.length() - 1);
result.setProperty(key.toLowerCase(), value);
}
}
return result;
}
public static String stripDoubleSpacesAndTrim(final String str) {
char[] charArray = str.toCharArray();
if (str.contains(" ")) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < charArray.length; i++) {
char c = charArray[i];
if (c != ' ') {
builder.append(c);
} else {
if (i + 1 < charArray.length && charArray[i + 1] != ' ') {
builder.append(' ');
}
}
}
return builder.toString().trim();
} else {
return String.valueOf(charArray).trim();
}
}
/**
* Removes the comments sections of a String.
*

43
fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java

@ -53,6 +53,8 @@ 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;
@ -84,7 +86,7 @@ public class FactoryProperties {
public Chunk createChunk(String text, ChainedProperties props) {
Font font = getFont(props);
float size = font.getSize();
size /= 2;
size /= 3;
Chunk ck = new Chunk(text, font);
if (props.hasProperty("sub"))
ck.setTextRise(-size);
@ -94,7 +96,14 @@ 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));
}
return ck;
}
private static void setParagraphLeading(Paragraph p, String leading) {
@ -131,7 +140,26 @@ 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));
}
if(props.hasPropertyInChain("div", "padding-right")){
String ss = props.getPropertyFromChain("div", "padding-right");
p.setIndentationRight(Markup.parseLength(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));
}
if(props.hasPropertyInChain("div", "height")){
String ss = props.getPropertyFromChain("div", "height");
p.setAttribute("height", Markup.parseLength(ss));
}
p.setHyphenation(getHyphenation(props));
setParagraphLeading(p, props.getProperty("leading"));
value = props.getProperty("before");
@ -319,6 +347,7 @@ public class FactoryProperties {
}
}
/**
* New method contributed by Lubos Strapko
* @param h
@ -386,9 +415,13 @@ public class FactoryProperties {
} else if (key.equals(Markup.CSS_KEY_TEXTALIGN)) {
String ss = prop.getProperty(key).trim().toLowerCase();
h.put("align", ss);
} else if (key.equals(Markup.CSS_KEY_PADDINGLEFT)) {
} else if (key.equals(Markup.CSS_KEY_BGCOLOR)) {
String ss = prop.getProperty(key).trim().toLowerCase();
h.put("indent", Float.toString(Markup.parseLength(ss)));
h.put("background", ss);
}
else{
String ss = prop.getProperty(key).trim();
h.put(key, ss);
}
}
}

91
fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java

@ -50,8 +50,11 @@
package com.fr.third.v2.lowagie.text.html.simpleparser;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
@ -81,6 +84,9 @@ import com.fr.third.v2.lowagie.text.Paragraph;
import com.fr.third.v2.lowagie.text.Phrase;
import com.fr.third.v2.lowagie.text.pdf.PdfPTable;
import com.fr.third.v2.lowagie.text.xml.simpleparser.SimpleXMLParser;
import sun.misc.BASE64Decoder;
import javax.imageio.ImageIO;
public class HTMLWorker implements SimpleXMLDocHandler, DocListener {
@ -179,52 +185,12 @@ public class HTMLWorker implements SimpleXMLDocHandler, DocListener {
cprops.addToChain("body", h);
}
private void dealStyleAttribute(HashMap h){
String styleAtt = (String) h.get("style");
if (null != styleAtt && styleAtt.length() > 0) {
Map<String, String> tagAttrCss = new LinkedHashMap<String, String>();
String[] styles = styleAtt.split(";");
for (String s : styles) {
String[] part = s.split(":",2);
if (part.length == 2) {
String key = stripDoubleSpacesAndTrim(part[0]).toLowerCase();
String value = stripDoubleSpacesAndTrim(part[1]);
tagAttrCss.put(key, value);
}
}
for (Map.Entry<String, String> e : tagAttrCss.entrySet()) {
h.put(e.getKey(), e.getValue());
}
}
}
public String stripDoubleSpacesAndTrim(final String str) {
char[] charArray = str.toCharArray();
if (str.contains(" ")) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < charArray.length; i++) {
char c = charArray[i];
if (c != ' ') {
builder.append(c);
} else {
if (i + 1 < charArray.length && charArray[i + 1] != ' ') {
builder.append(' ');
}
}
}
return builder.toString().trim();
} else {
return String.valueOf(charArray).trim();
}
}
public void startElement(String tag, HashMap h) {
if (!tagsSupported.containsKey(tag))
return;
try {
style.applyStyle(tag, h);
//解析Style的样式属性
dealStyleAttribute(h);
String follow = (String) FactoryProperties.followTags.get(tag);
if (follow != null) {
HashMap prop = new HashMap();
@ -329,6 +295,14 @@ public class HTMLWorker implements SimpleXMLDocHandler, DocListener {
}
}
}
//处理base64编码图片
if(src.startsWith("data")){
BASE64Decoder decoder = new BASE64Decoder();
String[] srcArray = src.split(",");
String base64string = srcArray[srcArray.length -1];
byte[] bytes = decoder.decodeBuffer(base64string);
img = Image.getInstance(bytes);
}
if (img == null) {
if (!src.startsWith("http")) {
String path = cprops.getProperty("image_path");
@ -341,6 +315,7 @@ public class HTMLWorker implements SimpleXMLDocHandler, DocListener {
if(img == null){
return;
}
img.setSrcString(src);
String align = (String) h.get("align");
String width = (String) h.get("width");
String height = (String) h.get("height");
@ -389,12 +364,22 @@ public class HTMLWorker implements SimpleXMLDocHandler, DocListener {
document.add(img);
cprops.removeChain(tag);
} else {
Chunk ck = new Chunk(img, 0, 0);
if(cprops.hasPropertyInChain("img", "padding-left")){
String ss = cprops.getPropertyFromChain("img", "padding-left");
ck.setAttribute("padding-left", Float.toString(Markup.parseLength(ss)));
}
if(cprops.hasPropertyInChain("img", "padding-right")){
String ss = cprops.getPropertyFromChain("img", "padding-right");
ck.setAttribute("padding-right", Float.toString(Markup.parseLength(ss)));
}
cprops.removeChain(tag);
if (currentParagraph == null) {
currentParagraph = FactoryProperties
.createParagraph(cprops);
}
currentParagraph.add(new Chunk(img, 0, 0));
currentParagraph.add(ck);
}
return;
}
@ -490,6 +475,30 @@ public class HTMLWorker implements SimpleXMLDocHandler, DocListener {
}
}
public BufferedImage GetBufferedImage(String base64string)
{
BufferedImage image = null;
try {
InputStream stream = BaseToInputStream(base64string);
image = ImageIO.read(stream);
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
public InputStream BaseToInputStream(String base64string){
ByteArrayInputStream stream = null;
try {
BASE64Decoder decoder = new BASE64Decoder();
byte[] bytes1 = decoder.decodeBuffer(base64string);
stream = new ByteArrayInputStream(bytes1);
} catch (Exception e) {
}
return stream;
}
public void endElement(String tag) {
if (!tagsSupported.containsKey(tag))
return;

7
fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HtmlConstants.java

@ -13,14 +13,16 @@ public class HtmlConstants {
public static final ArrayList BLOCK_ELEMENTS = new ArrayList();
//行内元素集合
public static final ArrayList INLINE_ELEMENTS = new ArrayList();
//支持解析的属性
public static final String PADDING_LEFT = "padding-left";
public static final String PADDING_RIGHT = "padding-right";
static {
BLOCK_ELEMENTS.add(HtmlTags.DIV);
BLOCK_ELEMENTS.add(HtmlTags.UNORDEREDLIST);
BLOCK_ELEMENTS.add(HtmlTags.PARAGRAPH);
BLOCK_ELEMENTS.add(HtmlTags.ORDEREDLIST);
BLOCK_ELEMENTS.add(HtmlTags.TABLE);
}
static {
INLINE_ELEMENTS.add(HtmlTags.SPAN);
INLINE_ELEMENTS.add(HtmlTags.IMAGE);
INLINE_ELEMENTS.add(HtmlTags.STRONG);
@ -31,5 +33,4 @@ public class HtmlConstants {
INLINE_ELEMENTS.add(HtmlTags.U);
INLINE_ELEMENTS.add(HtmlTags.EM);
}
}

76
fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java

@ -60,6 +60,8 @@ import com.fr.third.v2.lowagie.text.Utilities;
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 sun.font.FontDesignMetrics;
/**
@ -96,6 +98,8 @@ public class PdfChunk {
private float height = 0;
private IndentAttribute indent = new IndentAttribute();
static {
keysAttributes.put(Chunk.ACTION, null);
keysAttributes.put(Chunk.UNDERLINE, null);
@ -111,10 +115,13 @@ 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);
keysNoStroke.put(Chunk.TEXTRENDERMODE, null);
}
// membervariables
@ -227,7 +234,7 @@ public class PdfChunk {
attributes.put(Chunk.SKEW, new float[]{0, ITALIC_ANGLE});
FontMetrics metrics = FontDesignMetrics.getMetrics(new java.awt.Font(f.getFontName(), f.getStyle(), (int) (f.getSize() * FONT_SCALE)));
font = new PdfFont(f, f.getSize());
height = metrics.getHeight() / FONT_SCALE;
height = metrics.getHeight() / FONT_SCALE + indent.getTop() + indent.getBottom();
// other style possibilities
HashMap attr = chunk.getAttributes();
if (attr != null) {
@ -260,6 +267,12 @@ 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){
indent.setLeft((float)attributes.get("padding-left"));
}
if(attributes.get("padding-right") != null){
indent.setRight((float)attributes.get("padding-right"));
}
Object obj[] = (Object[])attributes.get(Chunk.IMAGE);
if (obj == null) {
image = null;
@ -267,7 +280,7 @@ public class PdfChunk {
else {
attributes.remove(Chunk.HSCALE); // images are scaled in other ways
image = (Image)obj[0];
this.height = image.getHeight();
this.height = image.getScaledHeight();
offsetX = ((Float)obj[1]).floatValue();
offsetY = ((Float)obj[2]).floatValue();
changeLeading = ((Boolean)obj[3]).booleanValue();
@ -315,7 +328,7 @@ public class PdfChunk {
PdfChunk split(float width) {
newlineSplit = false;
if (image != null) {
if (image.getScaledWidth() > width) {
if (image.getScaledWidth() + indent.getLeft() + indent.getRight() > width) {
PdfChunk pc = new PdfChunk(Chunk.OBJECT_REPLACEMENT_CHARACTER, this);
value = "";
attributes = new HashMap();
@ -329,7 +342,7 @@ public class PdfChunk {
HyphenationEvent hyphenationEvent = (HyphenationEvent)noStroke.get(Chunk.HYPHENATION);
int currentPosition = 0;
int splitPosition = -1;
float currentWidth = 0;
float currentWidth = indent.getLeft();
// loop over all the characters of a string
// or until the totalWidth is reached
@ -364,7 +377,7 @@ public class PdfChunk {
}
if (surrogate)
currentPosition++;
if (currentWidth > width)
if (currentWidth + indent.getRight()> width)
break;
// if a split-character is encountered, the splitPosition is altered
if (splitCharacter.isSplitCharacter(0, currentPosition, length, valueArray, null))
@ -415,7 +428,7 @@ public class PdfChunk {
PdfChunk truncate(float width) {
if (image != null) {
if (image.getScaledWidth() > width) {
if (image.getScaledWidth() + indent.getLeft() + indent.getRight() > width) {
PdfChunk pc = new PdfChunk("", this);
value = "";
attributes.remove(Chunk.IMAGE);
@ -428,10 +441,10 @@ public class PdfChunk {
}
int currentPosition = 0;
float currentWidth = 0;
float currentWidth = indent.getLeft();
// it's no use trying to split if there isn't even enough place for a space
if (width < font.width()) {
if (width < font.width() + indent.getLeft() + indent.getRight()) {
String returnValue = value.substring(1);
value = value.substring(0, 1);
PdfChunk pc = new PdfChunk(returnValue, this);
@ -505,7 +518,7 @@ public class PdfChunk {
*/
public float width() {
return font.width(value);
return font.width(value) + indent.getLeft() + indent.getRight();
}
/**
@ -529,7 +542,7 @@ public class PdfChunk {
public float getWidthCorrected(float charSpacing, float wordSpacing)
{
if (image != null) {
return image.getScaledWidth() + charSpacing;
return image.getScaledWidth() + indent.getLeft() + indent.getRight() + charSpacing;
}
int numberOfSpaces = 0;
int idx = -1;
@ -792,16 +805,55 @@ public class PdfChunk {
}
public String toHtmlString() {
//todo 需要添加其他样式属性
StringBuffer htmlString = new StringBuffer();
htmlString.append("<span>");
if(this.isImage()){
htmlString.append("<img src='").append(image.getSrcString()).append("'").append("/>");
return htmlString.toString();
}
//todo 需要添加其他样式属性
htmlString.append("<span style='");
htmlString.append("").append(getStyleAttributes()).append("'");
htmlString.append(">");
htmlString.append(value);
htmlString.append("</span>");
return htmlString.toString();
}
private String getStyleAttributes(){
StringBuffer stringBuffer = new StringBuffer();
dealFontStyle(stringBuffer, font.getOriFont());
if (this.getAttribute(Chunk.BACKGROUND) != null) {
Object bgr[] = (Object[]) this.getAttribute(Chunk.BACKGROUND);
String valueStr = (String) bgr[0];
stringBuffer.append("background:").append(valueStr).append(";");
}
return stringBuffer.toString();
}
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(";");
if (font.isBold()) {
s.append("font-weight : bold;");
}
if (font.isItalic()) {
s.append("font-style : italic;");
}
if (font.isUnderlined()) {
s.append("text-decoration :underline;");
}
}
public static boolean noPrint(int c) {
return ((c >= 0x200b && c <= 0x200f) || (c >= 0x202a && c <= 0x202e));
}
public float getLeftIndent(){
return indent.getLeft();
}
}

3
fine-itext/src/com/fr/third/v2/lowagie/text/xml/simpleparser/SimpleXMLParser.java

@ -195,7 +195,6 @@ public final class SimpleXMLParser {
character = previousCharacter;
previousCharacter = -1;
}
// the end of the file was reached
if (character == -1) {
if (html) {
@ -244,7 +243,7 @@ public final class SimpleXMLParser {
saveState(state);
entity.setLength(0);
state = ENTITY;
} else if (Character.isWhitespace((char)character)) {
} else if (Character.isWhitespace((char)character) && character != 12288) { //全角空格在浏览器中会被解释成汉字,不会被识别成分隔符,按照实际空格数显示
if (nowhite)
text.append((char)character);
nowhite = false;

Loading…
Cancel
Save