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. 91
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java
  6. 43
      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 */
private BaseFont baseFont = null;
private String fontName;
public String getFontName(){
return fontName;
}
// constructors
/**
@ -169,6 +177,13 @@ public class Font implements Comparable {
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.
*
@ -335,35 +350,7 @@ public class Font implements Comparable {
* @return the familyname
*/
public String getFamilyname() {
String tmp = "unknown";
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;
return getFontName();
}
/**
@ -374,9 +361,10 @@ public class Font implements Comparable {
* A <CODE>String</CODE> representing a certain font-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
* 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()
+ " is not a recognized imageformat.");
} finally {
}catch (Exception e){
return null;
}finally {
if (is != null) {
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)
size = Float.parseFloat(value);
Color color = Markup.decodeColor(props.getProperty("color"));
String encoding = props.getProperty("encoding");
if (encoding == null)
encoding = BaseFont.WINANSI;
return fontImp.getFont(face, encoding, true, size, style, color);
return new Font(face, 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);
}
if(img == null){
return;
}
String align = (String) h.get("align");
String width = (String) h.get("width");
String height = (String) h.get("height");

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

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

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

@ -50,7 +50,11 @@
package com.fr.third.v2.lowagie.text.pdf;
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 sun.font.FontDesignMetrics;
import java.awt.FontMetrics;
/**
* <CODE>PdfFont</CODE> is the Pdf Font object.
@ -67,27 +71,28 @@ import com.fr.third.v2.lowagie.text.Image;
* @see BadPdfFormatException
*/
class PdfFont implements Comparable {
public class PdfFont implements Comparable {
private Font oriFont;
/** the font metrics. */
private BaseFont font;
// private BaseFont font;
/** the size. */
private float size;
/** an image. */
protected Image image;
protected float hScale = 1;
// constructors
PdfFont(BaseFont bf, float size) {
PdfFont(Font oriFont, float size) {
this.size = size;
font = bf;
this.oriFont = oriFont;
}
// methods
/**
@ -106,7 +111,7 @@ class PdfFont implements Comparable {
PdfFont pdfFont;
try {
pdfFont = (PdfFont) object;
if (font != pdfFont.font) {
if (oriFont != pdfFont.oriFont) {
return 1;
}
if (this.size() != pdfFont.size()) {
@ -118,6 +123,8 @@ class PdfFont implements Comparable {
return -2;
}
}
/**
* Returns the size of this font.
@ -151,31 +158,37 @@ class PdfFont implements Comparable {
*/
float width(int character) {
FontMetrics metrics = FontDesignMetrics.getMetrics(getAwtFont());
if (image == null)
return font.getWidthPoint(character, size) * hScale;
return metrics.charWidth(character) * hScale;
else
return image.getScaledWidth();
}
float width(String s) {
FontMetrics metrics = FontDesignMetrics.getMetrics(getAwtFont());
if (image == null)
return font.getWidthPoint(s, size) * hScale;
return metrics.stringWidth(s) * hScale;
else
return image.getScaledWidth();
}
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) {
this.image = image;
}
static PdfFont getDefaultFont() {
try {
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false);
return new PdfFont(bf, 12);
// BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false);
return new PdfFont(new Font(), 12);
}
catch (Exception ee) {
throw new ExceptionConverter(ee);

Loading…
Cancel
Save