Browse Source

REPORT-14598 pdf HTML导出换行与浏览器不一致问题

release/10.0
Hugh.C 5 years ago
parent
commit
e68d073599
  1. 56
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfFont.java
  2. 7
      fine-itext/src/com/fr/third/v2/lowagie/text/xml/simpleparser/SimpleXMLParser.java

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

@ -52,6 +52,9 @@ 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.Font;
import com.fr.third.v2.lowagie.text.Image; import com.fr.third.v2.lowagie.text.Image;
import java.awt.font.TextAttribute;
import java.util.HashMap;
import java.util.Map;
import sun.font.FontDesignMetrics; import sun.font.FontDesignMetrics;
import java.awt.FontMetrics; import java.awt.FontMetrics;
@ -78,6 +81,8 @@ public class PdfFont implements Comparable {
private Font oriFont; private Font oriFont;
public static int SCALE = 100;
/** the font metrics. */ /** the font metrics. */
// private BaseFont font; // private BaseFont font;
@ -153,6 +158,15 @@ public class PdfFont implements Comparable {
return width(' '); return width(' ');
} }
private FontMetrics metrics;
private FontMetrics getMetrics() {
if (null == metrics) {
metrics = FontDesignMetrics.getMetrics(getAwtFont(SCALE));
}
return metrics;
}
/** /**
* Returns the width of a certain character of this font. * Returns the width of a certain character of this font.
* *
@ -161,44 +175,44 @@ public class PdfFont implements Comparable {
*/ */
float width(int character) { float width(int character) {
FontMetrics metrics = FontDesignMetrics.getMetrics(getAwtFont()); return image == null ? getMetrics().charWidth(replaceNbsp(character))/SCALE : image.getScaledWidth();
if (image == null)
return metrics.charWidth(character) * hScale;
else
return image.getScaledWidth();
} }
float width(String s) { float width(String s) {
double width = 0.0d; return image == null ? getMetrics().stringWidth(replaceNbsp(s))/SCALE : image.getScaledWidth();
java.awt.Font font = this.getAwtFont();
if (font.getSize2D() > 0) {
float scale = ONE_THOUSAND / font.getSize2D();
java.awt.Font derivedFont = font.deriveFont(AffineTransform.getScaleInstance(scale, scale));
width = derivedFont.getStringBounds(s, new FontRenderContext(new AffineTransform(), true, true)).getWidth();
if (derivedFont.isTransformed()){
width /= scale;
} }
String replaceNbsp(String str) {
return canDisplayNbsp() ? str : str.replaceAll(String.valueOf((char) 160), String.valueOf((char) 32));
} }
if (image == null)
return (float) width * hScale; int replaceNbsp(int character) {
else return character == 160 ? (canDisplayNbsp() ? character : 32) : character;
return image.getScaledWidth();
} }
private boolean canDisplayNbsp() {
return getAwtFont().canDisplay((char) 160);
}
BaseFont getFont() { BaseFont getFont() {
return oriFont.getCalculatedBaseFont(false); return oriFont.getCalculatedBaseFont(false);
} }
public java.awt.Font getAwtFont() { public java.awt.Font getAwtFont() {
int style = Font.NORMAL; return getAwtFont(1);
}
private java.awt.Font getAwtFont(int scale) {
Map attrMap = new HashMap(4);
attrMap.put(TextAttribute.FAMILY, oriFont.getFontName());
attrMap.put(TextAttribute.SIZE, new Float(oriFont.getSize() * scale));
if (oriFont.isBold()) { if (oriFont.isBold()) {
style |= Font.BOLD; attrMap.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
} }
if (oriFont.isItalic()) { if (oriFont.isItalic()) {
style |= Font.ITALIC; attrMap.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
} }
return new java.awt.Font(oriFont.getFontName(), style, (int)oriFont.getSize()); return new java.awt.Font(attrMap);
} }
public Font getOriFont(){ public Font getOriFont(){

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

@ -335,6 +335,8 @@ public final class SimpleXMLParser {
return; return;
} }
state = restoreState(); state = restoreState();
//防止 如<br/> 等标签后面的空格键生效
nowhite = false;
break; break;
// we are processing CDATA // we are processing CDATA
@ -377,8 +379,11 @@ public final class SimpleXMLParser {
char ce = EntitiesToUnicode.decodeEntity(cent); char ce = EntitiesToUnicode.decodeEntity(cent);
if (ce == '\0') if (ce == '\0')
text.append('&').append(cent).append(';'); text.append('&').append(cent).append(';');
else else {
text.append(ce); text.append(ce);
//防止如 &nbsp; 等标签后面的空格无效
nowhite = true;
}
} else if ((character != '#' && (character < '0' || character > '9') && (character < 'a' || character > 'z') } else if ((character != '#' && (character < '0' || character > '9') && (character < 'a' || character > 'z')
&& (character < 'A' || character > 'Z')) || entity.length() >= 7) { && (character < 'A' || character > 'Z')) || entity.length() >= 7) {
state = restoreState(); state = restoreState();

Loading…
Cancel
Save