Browse Source

REPORT-7872 && REPORT-7874 && REPORT-7584 html解析之文字抗锯齿;解析 文字 加粗 颜色 背景 无效;html背景图形, 打印出来发现没有铺满单元格, 只有文字部分有背景

10.0
kerry 7 years ago
parent
commit
088b8f06c2
  1. 9
      fine-itext/src/com/fr/third/v2/lowagie/text/List.java
  2. 10
      fine-itext/src/com/fr/third/v2/lowagie/text/Paragraph.java
  3. 2
      fine-itext/src/com/fr/third/v2/lowagie/text/html/WebColors.java
  4. 60
      fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/ChainedProperties.java
  5. 10
      fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java
  6. 35
      fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HtmlConstants.java
  7. 6
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java
  8. 4
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfFont.java

9
fine-itext/src/com/fr/third/v2/lowagie/text/List.java

@ -127,6 +127,15 @@ public class List implements TextElementArray {
protected boolean lettered = false;
/** Indicates if the listsymbols are lowercase or uppercase. */
protected boolean lowercase = false;
public ArrayList getList() {
return list;
}
public void setList(ArrayList list) {
this.list = list;
}
/** Indicates if the indentation has to be set automatically. */
protected boolean autoindent = false;
/** Indicates if the indentation of all the items has to be aligned. */

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

@ -103,6 +103,8 @@ public class Paragraph extends Phrase {
/** Does the paragraph has to be kept together on 1 page. */
protected boolean keeptogether = false;
protected String background;
// constructors
@ -499,4 +501,12 @@ public class Paragraph extends Phrase {
return spacingAfter;
}
public void setBackground(String background){
this.background = background;
}
public String getBackground(){
return this.background;
}
}

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

@ -221,7 +221,7 @@ public class WebColors extends HashMap {
*/
public static Color getRGBColor(String name)
throws IllegalArgumentException {
int[] c = { 0, 0, 0, 0 };
int[] c = { 0, 0, 0, 255 };
if (name.startsWith("#")) {
if (name.length() == 4) {
c[0] = Integer.parseInt(name.substring(1, 2), 16) * 16;

60
fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/ChainedProperties.java

@ -51,6 +51,7 @@
package com.fr.third.v2.lowagie.text.html.simpleparser;
import com.fr.third.v2.lowagie.text.ElementTags;
import java.util.ArrayList;
import java.util.HashMap;
@ -134,4 +135,63 @@ public class ChainedProperties {
}
}
}
public String getPropertyFromChain (String tag, String key) {
//块级元素不继承行内元素属性
if(HtmlConstants.BLOCK_ELEMENTS.contains(tag)){
for (int k = chain.size() - 1; k >= 0; --k) {
Object obj[] = (Object[]) chain.get(k);
if(HtmlConstants.INLINE_ELEMENTS.contains(obj[0])){
continue;
}
HashMap prop = (HashMap) obj[1];
String ret = (String) prop.get(key);
if (ret != null)
return ret;
}
}else{
for (int k = chain.size() - 1; k >= 0; --k) {
Object obj[] = (Object[]) chain.get(k);
if(HtmlConstants.BLOCK_ELEMENTS.contains(obj[0])){
return null;
}
HashMap prop = (HashMap) obj[1];
String ret = (String) prop.get(key);
if (ret != null)
return ret;
}
}
return null;
}
public boolean hasPropertyInChain(String tag, String key) {
if (HtmlConstants.BLOCK_ELEMENTS.contains(tag)) {
for (int k = chain.size() - 1; k >= 0; --k) {
Object obj[] = (Object[]) chain.get(k);
if( HtmlConstants.INLINE_ELEMENTS.contains(obj[0])){
continue;
}
HashMap prop = (HashMap) obj[1];
if (prop.containsKey(key))
return true;
}
} else {
for (int k = chain.size() - 1; k >= 0; --k) {
Object obj[] = (Object[]) chain.get(k);
if( HtmlConstants.BLOCK_ELEMENTS.contains(obj[0])){
return false;
}
HashMap prop = (HashMap) obj[1];
if (prop.containsKey(key))
return true;
}
}
return false;
}
}

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

@ -91,10 +91,10 @@ public class FactoryProperties {
else if (props.hasProperty("sup"))
ck.setTextRise(size);
ck.setHyphenation(getHyphenation(props));
//chunk添加背景属性
if (props.hasProperty("background")) {
ck.setBackground(props.getProperty("background"));
if(props.hasPropertyInChain("span", "background")){
ck.setBackground(props.getPropertyFromChain("span", "background"));
}
return ck;
}
private static void setParagraphLeading(Paragraph p, String leading) {
@ -128,6 +128,10 @@ public class FactoryProperties {
else if (value.equalsIgnoreCase("justify"))
p.setAlignment(Element.ALIGN_JUSTIFIED);
}
if(props.hasPropertyInChain("div", "background")){
p.setBackground(props.getPropertyFromChain("div", "background"));
}
p.setHyphenation(getHyphenation(props));
setParagraphLeading(p, props.getProperty("leading"));
value = props.getProperty("before");

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

@ -0,0 +1,35 @@
package com.fr.third.v2.lowagie.text.html.simpleparser;
import com.fr.third.v2.lowagie.text.html.HtmlTags;
import java.util.ArrayList;
/**
* @author kerry
* @date 2018/5/2
*/
public class HtmlConstants {
//块级元素集合
public static final ArrayList BLOCK_ELEMENTS = new ArrayList();
//行内元素集合
public static final ArrayList INLINE_ELEMENTS = new ArrayList();
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);
INLINE_ELEMENTS.add(HtmlTags.ANCHOR);
INLINE_ELEMENTS.add(HtmlTags.SUB);
INLINE_ELEMENTS.add(HtmlTags.SUP);
INLINE_ELEMENTS.add(HtmlTags.I);
INLINE_ELEMENTS.add(HtmlTags.U);
INLINE_ELEMENTS.add(HtmlTags.EM);
}
}

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

@ -81,6 +81,8 @@ public class PdfChunk {
/** The allowed attributes in variable <CODE>attributes</CODE>. */
private static final HashMap keysAttributes = new HashMap();
private static final float FONT_SCALE = 100f;
public float getHeight() {
return height;
}
@ -223,9 +225,9 @@ public class PdfChunk {
// italic simulation
if ((style & Font.ITALIC) != 0)
attributes.put(Chunk.SKEW, new float[]{0, ITALIC_ANGLE});
FontMetrics metrics = FontDesignMetrics.getMetrics(new java.awt.Font(f.getFontName(), f.getStyle(), (int)f.getSize()));
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();
height = metrics.getHeight() / FONT_SCALE;
// other style possibilities
HashMap attr = chunk.getAttributes();
if (attr != null) {

4
fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfFont.java

@ -181,6 +181,10 @@ public class PdfFont implements Comparable {
return new java.awt.Font(oriFont.getFontName(), oriFont.getStyle(), (int)oriFont.getSize());
}
public Font getOriFont(){
return oriFont;
}
void setImage(Image image) {
this.image = image;
}

Loading…
Cancel
Save