Browse Source

Merge pull request #359 in CORE/base-third from ~HUGH.C/base-third:bugfix/10.0 to bugfix/10.0

* commit '2fb9bbc5b119129327e271748a9175d943d9b85b':
  REPORT-25774 使用和报表一样的方法进行计算字符宽度
bugfix/10.0
ju.ju 5 years ago
parent
commit
5240ce9f27
  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 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,31 +162,20 @@ 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;
} }
private FontMetrics getMetrics() {
if (null == metrics) {
metrics = FontDesignMetrics.getMetrics(getAwtFont());
} }
if (image == null) return metrics;
return (float) width * hScale;
else
return image.getScaledWidth();
} }
BaseFont getFont() { BaseFont getFont() {
return oriFont.getCalculatedBaseFont(false); return oriFont.getCalculatedBaseFont(false);
} }

Loading…
Cancel
Save