|
|
|
@ -52,6 +52,9 @@ package com.fr.third.v2.lowagie.text.pdf;
|
|
|
|
|
import com.fr.third.v2.lowagie.text.ExceptionConverter; |
|
|
|
|
import com.fr.third.v2.lowagie.text.Font; |
|
|
|
|
import com.fr.third.v2.lowagie.text.Image; |
|
|
|
|
import java.awt.font.TextAttribute; |
|
|
|
|
import java.util.HashMap; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import sun.font.FontDesignMetrics; |
|
|
|
|
|
|
|
|
|
import java.awt.FontMetrics; |
|
|
|
@ -78,6 +81,8 @@ public class PdfFont implements Comparable {
|
|
|
|
|
|
|
|
|
|
private Font oriFont; |
|
|
|
|
|
|
|
|
|
public static int SCALE = 100; |
|
|
|
|
|
|
|
|
|
/** the font metrics. */ |
|
|
|
|
// private BaseFont font;
|
|
|
|
|
|
|
|
|
@ -153,6 +158,15 @@ public class PdfFont implements Comparable {
|
|
|
|
|
return width(' '); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private FontMetrics metrics; |
|
|
|
|
|
|
|
|
|
private FontMetrics getMetrics() { |
|
|
|
|
if (null == metrics) { |
|
|
|
|
metrics = FontDesignMetrics.getMetrics(getAwtFont(SCALE)); |
|
|
|
|
} |
|
|
|
|
return metrics; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns the width of a certain character of this font. |
|
|
|
|
* |
|
|
|
@ -161,44 +175,44 @@ 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(replaceNbsp(character))/SCALE : image.getScaledWidth(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
return image == null ? getMetrics().stringWidth(replaceNbsp(s))/SCALE : image.getScaledWidth(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
String replaceNbsp(String str) { |
|
|
|
|
return canDisplayNbsp() ? str : str.replaceAll(String.valueOf((char) 160), String.valueOf((char) 32)); |
|
|
|
|
} |
|
|
|
|
if (image == null) |
|
|
|
|
return (float) width * hScale; |
|
|
|
|
else |
|
|
|
|
return image.getScaledWidth(); |
|
|
|
|
|
|
|
|
|
int replaceNbsp(int character) { |
|
|
|
|
return character == 160 ? (canDisplayNbsp() ? character : 32) : character; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean canDisplayNbsp() { |
|
|
|
|
return getAwtFont().canDisplay((char) 160); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
BaseFont getFont() { |
|
|
|
|
return oriFont.getCalculatedBaseFont(false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public java.awt.Font getAwtFont() { |
|
|
|
|
int style = Font.NORMAL; |
|
|
|
|
return getAwtFont(1); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private java.awt.Font getAwtFont(int scale) { |
|
|
|
|
Map attrMap = new HashMap(4); |
|
|
|
|
attrMap.put(TextAttribute.FAMILY, oriFont.getFontName()); |
|
|
|
|
attrMap.put(TextAttribute.SIZE, new Float(oriFont.getSize() * scale)); |
|
|
|
|
if (oriFont.isBold()) { |
|
|
|
|
style |= Font.BOLD; |
|
|
|
|
attrMap.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD); |
|
|
|
|
} |
|
|
|
|
if (oriFont.isItalic()) { |
|
|
|
|
style |= Font.ITALIC; |
|
|
|
|
attrMap.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE); |
|
|
|
|
} |
|
|
|
|
return new java.awt.Font(oriFont.getFontName(), style, (int)oriFont.getSize()); |
|
|
|
|
return new java.awt.Font(attrMap); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public Font getOriFont(){ |
|
|
|
|