From 7c49b26bbc0a5719e68cae98cb7225204374d59c Mon Sep 17 00:00:00 2001 From: kerry Date: Sat, 29 Sep 2018 16:13:24 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-10262=20html=E5=A4=A7=E6=96=87=E6=9C=AC?= =?UTF-8?q?=E7=B2=BE=E7=A1=AE=E5=88=86=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fr/third/v2/lowagie/text/pdf/PdfFont.java | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 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 da9d4743d..0f80b75aa 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,6 +55,8 @@ 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. @@ -72,36 +74,37 @@ import java.awt.FontMetrics; */ public class PdfFont implements Comparable { + private static final int ONE_THOUSAND = 1000 ; private Font oriFont; /** the font metrics. */ // private BaseFont font; - + /** the size. */ private float size; - + /** an image. */ protected Image image; protected float hScale = 1; - + // constructors - + PdfFont(Font oriFont, float size) { this.size = size; this.oriFont = oriFont; } // methods - + /** * Compares this PdfFont with another * * @param object the other PdfFont * @return a value */ - + public int compareTo(Object object) { if (image != null) return 0; @@ -125,13 +128,13 @@ public class PdfFont implements Comparable { } - + /** * Returns the size of this font. * * @return a size */ - + float size() { if (image == null) return size; @@ -139,24 +142,24 @@ public class PdfFont implements Comparable { return image.getScaledHeight(); } } - + /** * Returns the approximative width of 1 character of this font. * * @return a width in Text Space */ - + float width() { return width(' '); } - + /** * Returns the width of a certain character of this font. * * @param character a certain character * @return a width in Text Space */ - + float width(int character) { FontMetrics metrics = FontDesignMetrics.getMetrics(getAwtFont()); if (image == null) @@ -164,15 +167,21 @@ public class PdfFont implements Comparable { else return image.getScaledWidth(); } - + float width(String s) { - FontMetrics metrics = FontDesignMetrics.getMetrics(getAwtFont()); + java.awt.Font font = this.getAwtFont(); + float scale = ONE_THOUSAND / font.getSize2D(); + java.awt.Font derivedFont = font.deriveFont(AffineTransform.getScaleInstance(scale, scale)); + double width = derivedFont.getStringBounds(s, new FontRenderContext(new AffineTransform(), true, true)).getWidth(); + if (derivedFont.isTransformed()){ + width /= scale; + } if (image == null) - return metrics.stringWidth(s) * hScale; + return (float) width * hScale; else return image.getScaledWidth(); } - + BaseFont getFont() { return oriFont.getCalculatedBaseFont(false); } @@ -195,7 +204,7 @@ public class PdfFont implements Comparable { void setImage(Image image) { this.image = image; } - + static PdfFont getDefaultFont() { try { // BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false);