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.Font;
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 java.awt.FontMetrics;
@ -78,6 +81,8 @@ public class PdfFont implements Comparable {
private Font oriFont;
public static int SCALE = 100;
/** the font metrics. */
// private BaseFont font;
@ -153,6 +158,15 @@ public class PdfFont implements Comparable {
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.
*
@ -161,44 +175,44 @@ public class PdfFont implements Comparable {
*/
float width(int character) {
FontMetrics metrics = FontDesignMetrics.getMetrics(getAwtFont());
if (image == null)
return metrics.charWidth(character) * hScale;
else
return image.getScaledWidth();
return image == null ? getMetrics().charWidth(replaceNbsp(character))/SCALE : image.getScaledWidth();
}
float width(String s) {
double width = 0.0d;
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;
return image == null ? getMetrics().stringWidth(replaceNbsp(s))/SCALE : image.getScaledWidth();
}
String replaceNbsp(String str) {
return canDisplayNbsp() ? str : str.replaceAll(String.valueOf((char) 160), String.valueOf((char) 32));
}
if (image == null)
return (float) width * hScale;
else
return image.getScaledWidth();
int replaceNbsp(int character) {
return character == 160 ? (canDisplayNbsp() ? character : 32) : character;
}
private boolean canDisplayNbsp() {
return getAwtFont().canDisplay((char) 160);
}
BaseFont getFont() {
return oriFont.getCalculatedBaseFont(false);
}
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()) {
style |= Font.BOLD;
attrMap.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
}
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(){

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

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

Loading…
Cancel
Save