From 2fb9bbc5b119129327e271748a9175d943d9b85b Mon Sep 17 00:00:00 2001 From: "Hugh.C" Date: Thu, 9 Jan 2020 12:35:12 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-25774=20=E4=BD=BF=E7=94=A8=E5=92=8C?= =?UTF-8?q?=E6=8A=A5=E8=A1=A8=E4=B8=80=E6=A0=B7=E7=9A=84=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E8=AE=A1=E7=AE=97=E5=AD=97=E7=AC=A6=E5=AE=BD?= =?UTF-8?q?=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fr/third/v2/lowagie/text/pdf/PdfFont.java | 34 +++++++------------ 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfFont.java b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfFont.java index 60917613c..4b213ac98 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfFont.java +++ b/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; /** * PdfFont 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,30 +162,19 @@ 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; - } - } - if (image == null) - return (float) width * hScale; - else - return image.getScaledWidth(); + float width(String s) { + return image == null ? getMetrics().stringWidth(s) : image.getScaledWidth(); } + private FontMetrics getMetrics() { + if (null == metrics) { + metrics = FontDesignMetrics.getMetrics(getAwtFont()); + } + return metrics; + } BaseFont getFont() { return oriFont.getCalculatedBaseFont(false);