|
|
|
@ -77,11 +77,12 @@ import java.awt.geom.AffineTransform;
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
public class PdfFont implements Comparable { |
|
|
|
|
private static final int ONE_THOUSAND = 1000 ; |
|
|
|
|
|
|
|
|
|
private Font oriFont; |
|
|
|
|
|
|
|
|
|
public static int SCALE = 100; |
|
|
|
|
public static float SCALE = 100; |
|
|
|
|
|
|
|
|
|
private static String DEFAULT_FONT_NAME = ""; |
|
|
|
|
|
|
|
|
|
/** the font metrics. */ |
|
|
|
|
// private BaseFont font;
|
|
|
|
@ -101,6 +102,12 @@ public class PdfFont implements Comparable {
|
|
|
|
|
this.oriFont = oriFont; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void initDefaultFont(String fontName) { |
|
|
|
|
if ("" == DEFAULT_FONT_NAME && null != fontName) { |
|
|
|
|
DEFAULT_FONT_NAME = fontName; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// methods
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -160,9 +167,14 @@ public class PdfFont implements Comparable {
|
|
|
|
|
|
|
|
|
|
private FontMetrics metrics; |
|
|
|
|
|
|
|
|
|
private FontMetrics getMetrics() { |
|
|
|
|
if (null == metrics) { |
|
|
|
|
metrics = FontDesignMetrics.getMetrics(getAwtFont(SCALE)); |
|
|
|
|
private java.awt.Font scaleFont; |
|
|
|
|
|
|
|
|
|
private java.awt.Font scaleDefaultFont; |
|
|
|
|
|
|
|
|
|
private FontMetrics getMetrics(java.awt.Font font) { |
|
|
|
|
if (null == metrics || !font.equals(metrics.getFont())) { |
|
|
|
|
metrics = FontDesignMetrics.getMetrics(font); |
|
|
|
|
return metrics; |
|
|
|
|
} |
|
|
|
|
return metrics; |
|
|
|
|
} |
|
|
|
@ -174,16 +186,27 @@ public class PdfFont implements Comparable {
|
|
|
|
|
* @return a width in Text Space |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
float width(int character) { |
|
|
|
|
return image == null ? getMetrics().charWidth(replaceNbsp(character))/SCALE : image.getScaledWidth(); |
|
|
|
|
public float width(int character) { |
|
|
|
|
if (null != image) { |
|
|
|
|
return image.getScaledWidth(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
float width(String s) { |
|
|
|
|
return image == null ? getMetrics().stringWidth(replaceNbsp(s))/SCALE : image.getScaledWidth(); |
|
|
|
|
java.awt.Font font = getScaleAwtFont(); |
|
|
|
|
font = font.canDisplay(character) ? font : getScaleDefaultAwtFont(); |
|
|
|
|
return getMetrics(font).charWidth(replaceNbsp(character)) / SCALE; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
String replaceNbsp(String str) { |
|
|
|
|
return canDisplayNbsp() ? str : str.replaceAll(String.valueOf((char) 160), String.valueOf((char) 32)); |
|
|
|
|
public float width(String s) { |
|
|
|
|
if (null != image) { |
|
|
|
|
return image.getScaledWidth(); |
|
|
|
|
} |
|
|
|
|
if (null == s) { |
|
|
|
|
return 0f; |
|
|
|
|
} |
|
|
|
|
float num = 0f; |
|
|
|
|
for (int i = 0; i < s.length(); i++) { |
|
|
|
|
num += width(s.charAt(i)); |
|
|
|
|
} |
|
|
|
|
return num; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int replaceNbsp(int character) { |
|
|
|
@ -191,7 +214,7 @@ public class PdfFont implements Comparable {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean canDisplayNbsp() { |
|
|
|
|
return getAwtFont().canDisplay((char) 160); |
|
|
|
|
return getScaleAwtFont().canDisplay((char) 160); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
BaseFont getFont() { |
|
|
|
@ -199,12 +222,27 @@ public class PdfFont implements Comparable {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public java.awt.Font getAwtFont() { |
|
|
|
|
return getAwtFont(1); |
|
|
|
|
return getAwtFont(oriFont.getFontName(), 1f); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private java.awt.Font getScaleAwtFont() { |
|
|
|
|
if (null == scaleFont) { |
|
|
|
|
scaleFont = getAwtFont(oriFont.getFontName(), SCALE); |
|
|
|
|
} |
|
|
|
|
return scaleFont; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private java.awt.Font getAwtFont(int scale) { |
|
|
|
|
private java.awt.Font getScaleDefaultAwtFont() { |
|
|
|
|
if (null == scaleDefaultFont) { |
|
|
|
|
scaleDefaultFont = getAwtFont(DEFAULT_FONT_NAME, SCALE); |
|
|
|
|
} |
|
|
|
|
return scaleDefaultFont; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private java.awt.Font getAwtFont(String fontName,float scale) { |
|
|
|
|
Map attrMap = new HashMap(4); |
|
|
|
|
attrMap.put(TextAttribute.FAMILY, oriFont.getFontName()); |
|
|
|
|
attrMap.put(TextAttribute.FAMILY, fontName); |
|
|
|
|
attrMap.put(TextAttribute.SIZE, new Float(oriFont.getSize() * scale)); |
|
|
|
|
if (oriFont.isBold()) { |
|
|
|
|
attrMap.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD); |
|
|
|
|