Browse Source

REPORT-7481 && REPORT-7480 html解析之IMG标签;html解析之分页断开

10.0
kerry 7 years ago
parent
commit
f989f4922e
  1. 48
      fine-itext/src/com/fr/third/v2/lowagie/text/Font.java
  2. 4
      fine-itext/src/com/fr/third/v2/lowagie/text/Image.java
  3. 5
      fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/FactoryProperties.java
  4. 3
      fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java
  5. 73
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java
  6. 33
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfFont.java

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

@ -133,6 +133,14 @@ public class Font implements Comparable {
/** the external font */ /** the external font */
private BaseFont baseFont = null; private BaseFont baseFont = null;
private String fontName;
public String getFontName(){
return fontName;
}
// constructors // constructors
/** /**
@ -169,6 +177,13 @@ public class Font implements Comparable {
this.color = color; this.color = color;
} }
public Font(String face, float size, int style, Color color) {
this.fontName = face;
this.size =size;
this.style = style;
this.color = color;
}
/** /**
* Constructs a Font. * Constructs a Font.
* *
@ -335,35 +350,7 @@ public class Font implements Comparable {
* @return the familyname * @return the familyname
*/ */
public String getFamilyname() { public String getFamilyname() {
String tmp = "unknown"; return getFontName();
switch (getFamily()) {
case Font.COURIER:
return FontFactory.COURIER;
case Font.HELVETICA:
return FontFactory.HELVETICA;
case Font.TIMES_ROMAN:
return FontFactory.TIMES_ROMAN;
case Font.SYMBOL:
return FontFactory.SYMBOL;
case Font.ZAPFDINGBATS:
return FontFactory.ZAPFDINGBATS;
default:
if (baseFont != null) {
String[][] names = baseFont.getFamilyFontName();
for (int i = 0; i < names.length; i++) {
if ("0".equals(names[i][2])) {
return names[i][3];
}
if ("1033".equals(names[i][2])) {
tmp = names[i][3];
}
if ("".equals(names[i][2])) {
tmp = names[i][3];
}
}
}
}
return tmp;
} }
/** /**
@ -374,9 +361,10 @@ public class Font implements Comparable {
* A <CODE>String</CODE> representing a certain font-family. * A <CODE>String</CODE> representing a certain font-family.
*/ */
public void setFamily(String family) { public void setFamily(String family) {
this.family = getFamilyIndex(family); this.fontName = family;
} }
/** /**
* Translates a <CODE>String</CODE> -value of a certain family into the * Translates a <CODE>String</CODE> -value of a certain family into the
* index that is used for this family in this class. * index that is used for this family in this class.

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

@ -321,7 +321,9 @@ public abstract class Image extends Rectangle {
} }
throw new IOException(url.toString() throw new IOException(url.toString()
+ " is not a recognized imageformat."); + " is not a recognized imageformat.");
} finally { }catch (Exception e){
return null;
}finally {
if (is != null) { if (is != null) {
is.close(); is.close();
} }

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

@ -193,10 +193,7 @@ public class FactoryProperties {
if (value != null) if (value != null)
size = Float.parseFloat(value); size = Float.parseFloat(value);
Color color = Markup.decodeColor(props.getProperty("color")); Color color = Markup.decodeColor(props.getProperty("color"));
String encoding = props.getProperty("encoding"); return new Font(face, size, style, color);
if (encoding == null)
encoding = BaseFont.WINANSI;
return fontImp.getFont(face, encoding, true, size, style, color);
} }
/** /**

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

@ -338,6 +338,9 @@ public class HTMLWorker implements SimpleXMLDocHandler, DocListener {
} }
img = Image.getInstance(src); img = Image.getInstance(src);
} }
if(img == null){
return;
}
String align = (String) h.get("align"); String align = (String) h.get("align");
String width = (String) h.get("width"); String width = (String) h.get("width");
String height = (String) h.get("height"); String height = (String) h.get("height");

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

@ -50,6 +50,7 @@
package com.fr.third.v2.lowagie.text.pdf; package com.fr.third.v2.lowagie.text.pdf;
import java.awt.Color; import java.awt.Color;
import java.awt.FontMetrics;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
@ -59,6 +60,7 @@ import com.fr.third.v2.lowagie.text.Utilities;
import com.fr.third.v2.lowagie.text.Chunk; import com.fr.third.v2.lowagie.text.Chunk;
import com.fr.third.v2.lowagie.text.Font; import com.fr.third.v2.lowagie.text.Font;
import com.fr.third.v2.lowagie.text.Image; import com.fr.third.v2.lowagie.text.Image;
import sun.font.FontDesignMetrics;
/** /**
* A <CODE>PdfChunk</CODE> is the PDF translation of a <CODE>Chunk</CODE>. * A <CODE>PdfChunk</CODE> is the PDF translation of a <CODE>Chunk</CODE>.
@ -125,7 +127,13 @@ public class PdfChunk {
/** The font for this <CODE>PdfChunk</CODE>. */ /** The font for this <CODE>PdfChunk</CODE>. */
protected PdfFont font; protected PdfFont font;
protected BaseFont baseFont; public PdfFont getFont() {
return font;
}
public void setFont(PdfFont font) {
this.font = font;
}
protected SplitCharacter splitCharacter; protected SplitCharacter splitCharacter;
/** /**
@ -174,7 +182,6 @@ public class PdfChunk {
this.font = other.font; this.font = other.font;
this.attributes = other.attributes; this.attributes = other.attributes;
this.noStroke = other.noStroke; this.noStroke = other.noStroke;
this.baseFont = other.baseFont;
Object obj[] = (Object[])attributes.get(Chunk.IMAGE); Object obj[] = (Object[])attributes.get(Chunk.IMAGE);
if (obj == null) if (obj == null)
image = null; image = null;
@ -184,7 +191,6 @@ public class PdfChunk {
offsetY = ((Float)obj[2]).floatValue(); offsetY = ((Float)obj[2]).floatValue();
changeLeading = ((Boolean)obj[3]).booleanValue(); changeLeading = ((Boolean)obj[3]).booleanValue();
} }
encoding = font.getFont().getEncoding();
splitCharacter = (SplitCharacter)noStroke.get(Chunk.SPLITCHARACTER); splitCharacter = (SplitCharacter)noStroke.get(Chunk.SPLITCHARACTER);
if (splitCharacter == null) if (splitCharacter == null)
splitCharacter = DefaultSplitCharacter.DEFAULT; splitCharacter = DefaultSplitCharacter.DEFAULT;
@ -206,28 +212,20 @@ public class PdfChunk {
float size = f.getSize(); float size = f.getSize();
if (size == Font.UNDEFINED) if (size == Font.UNDEFINED)
size = 12; size = 12;
baseFont = f.getBaseFont();
int style = f.getStyle(); int style = f.getStyle();
if (style == Font.UNDEFINED) { if (style == Font.UNDEFINED) {
style = Font.NORMAL; style = Font.NORMAL;
} }
if (baseFont == null) {
// translation of the font-family to a PDF font-family
baseFont = f.getCalculatedBaseFont(false);
}
else {
// bold simulation // bold simulation
if ((style & Font.BOLD) != 0) if ((style & Font.BOLD) != 0)
attributes.put(Chunk.TEXTRENDERMODE, new Object[]{new Integer(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE), new Float(size / 30f), null}); attributes.put(Chunk.TEXTRENDERMODE, new Object[]{new Integer(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE), new Float(size / 30f), null});
// italic simulation // italic simulation
if ((style & Font.ITALIC) != 0) if ((style & Font.ITALIC) != 0)
attributes.put(Chunk.SKEW, new float[]{0, ITALIC_ANGLE}); 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 = new PdfFont(baseFont, size); font = new PdfFont(f, f.getSize());
float fontSize = font.size(); height = metrics.getHeight();
float ascender = font.getFont().getFontDescriptor(BaseFont.ASCENT, fontSize);
float descender = font.getFont().getFontDescriptor(BaseFont.DESCENT, fontSize);
height = ascender - descender;
// other style possibilities // other style possibilities
HashMap attr = chunk.getAttributes(); HashMap attr = chunk.getAttributes();
if (attr != null) { if (attr != null) {
@ -259,7 +257,7 @@ public class PdfChunk {
attributes.put(Chunk.ACTION, action); attributes.put(Chunk.ACTION, action);
// the color can't be stored in a PdfFont // the color can't be stored in a PdfFont
noStroke.put(Chunk.COLOR, f.getColor()); noStroke.put(Chunk.COLOR, f.getColor());
noStroke.put(Chunk.ENCODING, font.getFont().getEncoding()); // noStroke.put(Chunk.ENCODING, font.getFont().getEncoding());
Object obj[] = (Object[])attributes.get(Chunk.IMAGE); Object obj[] = (Object[])attributes.get(Chunk.IMAGE);
if (obj == null) { if (obj == null) {
image = null; image = null;
@ -267,6 +265,7 @@ public class PdfChunk {
else { else {
attributes.remove(Chunk.HSCALE); // images are scaled in other ways attributes.remove(Chunk.HSCALE); // images are scaled in other ways
image = (Image)obj[0]; image = (Image)obj[0];
this.height = image.getHeight();
offsetX = ((Float)obj[1]).floatValue(); offsetX = ((Float)obj[1]).floatValue();
offsetY = ((Float)obj[2]).floatValue(); offsetY = ((Float)obj[2]).floatValue();
changeLeading = ((Boolean)obj[3]).booleanValue(); changeLeading = ((Boolean)obj[3]).booleanValue();
@ -275,7 +274,6 @@ public class PdfChunk {
Float hs = (Float)attributes.get(Chunk.HSCALE); Float hs = (Float)attributes.get(Chunk.HSCALE);
if (hs != null) if (hs != null)
font.setHorizontalScaling(hs.floatValue()); font.setHorizontalScaling(hs.floatValue());
encoding = font.getFont().getEncoding();
splitCharacter = (SplitCharacter)noStroke.get(Chunk.SPLITCHARACTER); splitCharacter = (SplitCharacter)noStroke.get(Chunk.SPLITCHARACTER);
if (splitCharacter == null) if (splitCharacter == null)
splitCharacter = DefaultSplitCharacter.DEFAULT; splitCharacter = DefaultSplitCharacter.DEFAULT;
@ -290,7 +288,7 @@ public class PdfChunk {
* @return the Unicode equivalent * @return the Unicode equivalent
*/ */
public int getUnicodeEquivalent(int c) { public int getUnicodeEquivalent(int c) {
return baseFont.getUnicodeEquivalent(c); return 0;
} }
protected int getWord(String text, int start) { protected int getWord(String text, int start) {
@ -338,38 +336,7 @@ public class PdfChunk {
int length = value.length(); int length = value.length();
char valueArray[] = value.toCharArray(); char valueArray[] = value.toCharArray();
char character = 0; char character = 0;
BaseFont ft = font.getFont();
boolean surrogate = false; boolean surrogate = false;
if (ft.getFontType() == BaseFont.FONT_TYPE_CJK && ft.getUnicodeEquivalent(' ') != ' ') {
while (currentPosition < length) {
// the width of every character is added to the currentWidth
char cidChar = valueArray[currentPosition];
character = (char)ft.getUnicodeEquivalent(cidChar);
// if a newLine or carriageReturn is encountered
if (character == '\n') {
newlineSplit = true;
String returnValue = value.substring(currentPosition + 1);
value = value.substring(0, currentPosition);
if (value.length() < 1) {
value = "\u0001";
}
PdfChunk pc = new PdfChunk(returnValue, this);
return pc;
}
currentWidth += font.width(cidChar);
if (character == ' ') {
lastSpace = currentPosition + 1;
lastSpaceWidth = currentWidth;
}
if (currentWidth > width)
break;
// if a split-character is encountered, the splitPosition is altered
if (splitCharacter.isSplitCharacter(0, currentPosition, length, valueArray, thisChunk))
splitPosition = currentPosition + 1;
currentPosition++;
}
}
else {
while (currentPosition < length) { while (currentPosition < length) {
// the width of every character is added to the currentWidth // the width of every character is added to the currentWidth
character = valueArray[currentPosition]; character = valueArray[currentPosition];
@ -381,9 +348,6 @@ public class PdfChunk {
inc = 2; inc = 2;
String returnValue = value.substring(currentPosition + inc); String returnValue = value.substring(currentPosition + inc);
value = value.substring(0, currentPosition); value = value.substring(0, currentPosition);
if (value.length() < 1) {
value = " ";
}
PdfChunk pc = new PdfChunk(returnValue, this); PdfChunk pc = new PdfChunk(returnValue, this);
return pc; return pc;
} }
@ -405,7 +369,6 @@ public class PdfChunk {
splitPosition = currentPosition + 1; splitPosition = currentPosition + 1;
currentPosition++; currentPosition++;
} }
}
// if all the characters fit in the total width, null is returned (there is no overflow) // if all the characters fit in the total width, null is returned (there is no overflow)
if (currentPosition == length) { if (currentPosition == length) {
@ -710,7 +673,7 @@ public class PdfChunk {
* @return <CODE>true</CODE> if an image is present * @return <CODE>true</CODE> if an image is present
*/ */
boolean isImage() public boolean isImage()
{ {
return image != null; return image != null;
} }
@ -720,7 +683,7 @@ public class PdfChunk {
* @return the image or <CODE>null</CODE> * @return the image or <CODE>null</CODE>
*/ */
Image getImage() public Image getImage()
{ {
return image; return image;
} }

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

@ -50,7 +50,11 @@
package com.fr.third.v2.lowagie.text.pdf; package com.fr.third.v2.lowagie.text.pdf;
import com.fr.third.v2.lowagie.text.ExceptionConverter; import com.fr.third.v2.lowagie.text.ExceptionConverter;
import com.fr.third.v2.lowagie.text.Font;
import com.fr.third.v2.lowagie.text.Image; import com.fr.third.v2.lowagie.text.Image;
import sun.font.FontDesignMetrics;
import java.awt.FontMetrics;
/** /**
* <CODE>PdfFont</CODE> is the Pdf Font object. * <CODE>PdfFont</CODE> is the Pdf Font object.
@ -67,11 +71,12 @@ import com.fr.third.v2.lowagie.text.Image;
* @see BadPdfFormatException * @see BadPdfFormatException
*/ */
class PdfFont implements Comparable { public class PdfFont implements Comparable {
private Font oriFont;
/** the font metrics. */ /** the font metrics. */
private BaseFont font; // private BaseFont font;
/** the size. */ /** the size. */
private float size; private float size;
@ -83,9 +88,9 @@ class PdfFont implements Comparable {
// constructors // constructors
PdfFont(BaseFont bf, float size) { PdfFont(Font oriFont, float size) {
this.size = size; this.size = size;
font = bf; this.oriFont = oriFont;
} }
// methods // methods
@ -106,7 +111,7 @@ class PdfFont implements Comparable {
PdfFont pdfFont; PdfFont pdfFont;
try { try {
pdfFont = (PdfFont) object; pdfFont = (PdfFont) object;
if (font != pdfFont.font) { if (oriFont != pdfFont.oriFont) {
return 1; return 1;
} }
if (this.size() != pdfFont.size()) { if (this.size() != pdfFont.size()) {
@ -119,6 +124,8 @@ class PdfFont implements Comparable {
} }
} }
/** /**
* Returns the size of this font. * Returns the size of this font.
* *
@ -151,21 +158,27 @@ class PdfFont implements Comparable {
*/ */
float width(int character) { float width(int character) {
FontMetrics metrics = FontDesignMetrics.getMetrics(getAwtFont());
if (image == null) if (image == null)
return font.getWidthPoint(character, size) * hScale; return metrics.charWidth(character) * hScale;
else else
return image.getScaledWidth(); return image.getScaledWidth();
} }
float width(String s) { float width(String s) {
FontMetrics metrics = FontDesignMetrics.getMetrics(getAwtFont());
if (image == null) if (image == null)
return font.getWidthPoint(s, size) * hScale; return metrics.stringWidth(s) * hScale;
else else
return image.getScaledWidth(); return image.getScaledWidth();
} }
BaseFont getFont() { BaseFont getFont() {
return font; return oriFont.getCalculatedBaseFont(false);
}
public java.awt.Font getAwtFont() {
return new java.awt.Font(oriFont.getFontName(), oriFont.getStyle(), (int)oriFont.getSize());
} }
void setImage(Image image) { void setImage(Image image) {
@ -174,8 +187,8 @@ class PdfFont implements Comparable {
static PdfFont getDefaultFont() { static PdfFont getDefaultFont() {
try { try {
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false); // BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false);
return new PdfFont(bf, 12); return new PdfFont(new Font(), 12);
} }
catch (Exception ee) { catch (Exception ee) {
throw new ExceptionConverter(ee); throw new ExceptionConverter(ee);

Loading…
Cancel
Save