Browse Source

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

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

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

Loading…
Cancel
Save