|
|
@ -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.ExceptionConverter; |
|
|
|
import com.fr.third.v2.lowagie.text.Font; |
|
|
|
import com.fr.third.v2.lowagie.text.Font; |
|
|
|
import com.fr.third.v2.lowagie.text.Image; |
|
|
|
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 sun.font.FontDesignMetrics; |
|
|
|
|
|
|
|
|
|
|
|
import java.awt.FontMetrics; |
|
|
|
import java.awt.FontMetrics; |
|
|
@ -74,10 +77,13 @@ import java.awt.geom.AffineTransform; |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
public class PdfFont implements Comparable { |
|
|
|
public class PdfFont implements Comparable { |
|
|
|
private static final int ONE_THOUSAND = 1000 ; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Font oriFont; |
|
|
|
private Font oriFont; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static float SCALE = 100; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static String DEFAULT_FONT_NAME = ""; |
|
|
|
|
|
|
|
|
|
|
|
/** the font metrics. */ |
|
|
|
/** the font metrics. */ |
|
|
|
// private BaseFont font;
|
|
|
|
// private BaseFont font;
|
|
|
|
|
|
|
|
|
|
|
@ -96,6 +102,12 @@ public class PdfFont implements Comparable { |
|
|
|
this.oriFont = oriFont; |
|
|
|
this.oriFont = oriFont; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void initDefaultFont(String fontName) { |
|
|
|
|
|
|
|
if ("" == DEFAULT_FONT_NAME && null != fontName) { |
|
|
|
|
|
|
|
DEFAULT_FONT_NAME = fontName; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// methods
|
|
|
|
// methods
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -153,6 +165,20 @@ public class PdfFont implements Comparable { |
|
|
|
return width(' '); |
|
|
|
return width(' '); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private FontMetrics metrics; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Returns the width of a certain character of this font. |
|
|
|
* Returns the width of a certain character of this font. |
|
|
|
* |
|
|
|
* |
|
|
@ -160,45 +186,71 @@ public class PdfFont implements Comparable { |
|
|
|
* @return a width in Text Space |
|
|
|
* @return a width in Text Space |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
float width(int character) { |
|
|
|
public float width(int character) { |
|
|
|
FontMetrics metrics = FontDesignMetrics.getMetrics(getAwtFont()); |
|
|
|
if (null != image) { |
|
|
|
if (image == null) |
|
|
|
|
|
|
|
return metrics.charWidth(character) * hScale; |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
return image.getScaledWidth(); |
|
|
|
return image.getScaledWidth(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
java.awt.Font font = getScaleAwtFont(); |
|
|
|
|
|
|
|
font = font.canDisplay(character) ? font : getScaleDefaultAwtFont(); |
|
|
|
|
|
|
|
return getMetrics(font).charWidth(replaceNbsp(character)) / SCALE; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
float width(String s) { |
|
|
|
public float width(String s) { |
|
|
|
double width = 0.0d; |
|
|
|
if (null != image) { |
|
|
|
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(); |
|
|
|
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) { |
|
|
|
|
|
|
|
return character == 160 ? (canDisplayNbsp() ? character : 32) : character; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean canDisplayNbsp() { |
|
|
|
|
|
|
|
return getScaleAwtFont().canDisplay((char) 160); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
BaseFont getFont() { |
|
|
|
BaseFont getFont() { |
|
|
|
return oriFont.getCalculatedBaseFont(false); |
|
|
|
return oriFont.getCalculatedBaseFont(false); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public java.awt.Font getAwtFont() { |
|
|
|
public java.awt.Font getAwtFont() { |
|
|
|
int style = Font.NORMAL; |
|
|
|
return getAwtFont(oriFont.getFontName(), 1f); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private java.awt.Font getScaleAwtFont() { |
|
|
|
|
|
|
|
if (null == scaleFont) { |
|
|
|
|
|
|
|
scaleFont = getAwtFont(oriFont.getFontName(), SCALE); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return scaleFont; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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, fontName); |
|
|
|
|
|
|
|
attrMap.put(TextAttribute.SIZE, new Float(oriFont.getSize() * scale)); |
|
|
|
if (oriFont.isBold()) { |
|
|
|
if (oriFont.isBold()) { |
|
|
|
style |= Font.BOLD; |
|
|
|
attrMap.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD); |
|
|
|
} |
|
|
|
} |
|
|
|
if(oriFont.isItalic()){ |
|
|
|
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(){ |
|
|
|
public Font getOriFont(){ |
|
|
|