Browse Source

REPORT-25774 使用和报表一样的方法进行计算字符宽度

bugfix/10.0
Hugh.C 5 years ago
parent
commit
2fb9bbc5b1
  1. 34
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfFont.java

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

@ -55,8 +55,6 @@ import com.fr.third.v2.lowagie.text.Image;
import sun.font.FontDesignMetrics; import sun.font.FontDesignMetrics;
import java.awt.FontMetrics; import java.awt.FontMetrics;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
/** /**
* <CODE>PdfFont</CODE> is the Pdf Font object. * <CODE>PdfFont</CODE> is the Pdf Font object.
@ -74,6 +72,7 @@ import java.awt.geom.AffineTransform;
*/ */
public class PdfFont implements Comparable { public class PdfFont implements Comparable {
private static final int ONE_THOUSAND = 1000 ; private static final int ONE_THOUSAND = 1000 ;
private Font oriFont; private Font oriFont;
@ -153,6 +152,8 @@ public class PdfFont implements Comparable {
return width(' '); return width(' ');
} }
private FontMetrics metrics;
/** /**
* Returns the width of a certain character of this font. * Returns the width of a certain character of this font.
* *
@ -161,30 +162,19 @@ public class PdfFont implements Comparable {
*/ */
float width(int character) { float width(int character) {
FontMetrics metrics = FontDesignMetrics.getMetrics(getAwtFont()); return image == null ? getMetrics().charWidth(character) : image.getScaledWidth();
if (image == null)
return metrics.charWidth(character) * hScale;
else
return image.getScaledWidth();
} }
public float width(String s) { float width(String s) {
double width = 0.0d; return image == null ? getMetrics().stringWidth(s) : 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;
}
}
if (image == null)
return (float) width * hScale;
else
return image.getScaledWidth();
} }
private FontMetrics getMetrics() {
if (null == metrics) {
metrics = FontDesignMetrics.getMetrics(getAwtFont());
}
return metrics;
}
BaseFont getFont() { BaseFont getFont() {
return oriFont.getCalculatedBaseFont(false); return oriFont.getCalculatedBaseFont(false);

Loading…
Cancel
Save