From 6a959c0b33e40f76dfaabe2a2d76b2158ab898a0 Mon Sep 17 00:00:00 2001 From: kerry Date: Sat, 29 Sep 2018 16:09:44 +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 | 42 +++++++++++-------- 1 file changed, 25 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..b1d2183b7 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. @@ -77,31 +79,31 @@ public class PdfFont implements Comparable { /** 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 +127,13 @@ public class PdfFont implements Comparable { } - + /** * Returns the size of this font. * * @return a size */ - + float size() { if (image == null) return size; @@ -139,24 +141,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 +166,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 = 1000 / 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 +203,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);