|
|
@ -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; |
|
|
@ -78,6 +81,8 @@ public class PdfFont implements Comparable { |
|
|
|
|
|
|
|
|
|
|
|
private Font oriFont; |
|
|
|
private Font oriFont; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static int SCALE = 100; |
|
|
|
|
|
|
|
|
|
|
|
/** the font metrics. */ |
|
|
|
/** the font metrics. */ |
|
|
|
// private BaseFont font;
|
|
|
|
// private BaseFont font;
|
|
|
|
|
|
|
|
|
|
|
@ -153,6 +158,15 @@ public class PdfFont implements Comparable { |
|
|
|
return width(' '); |
|
|
|
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. |
|
|
|
* Returns the width of a certain character of this font. |
|
|
|
* |
|
|
|
* |
|
|
@ -161,44 +175,44 @@ public class PdfFont implements Comparable { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
float width(int character) { |
|
|
|
float width(int character) { |
|
|
|
FontMetrics metrics = FontDesignMetrics.getMetrics(getAwtFont()); |
|
|
|
return image == null ? getMetrics().charWidth(replaceNbsp(character))/SCALE : image.getScaledWidth(); |
|
|
|
if (image == null) |
|
|
|
|
|
|
|
return metrics.charWidth(character) * hScale; |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
return image.getScaledWidth(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
float width(String s) { |
|
|
|
float width(String s) { |
|
|
|
double width = 0.0d; |
|
|
|
return image == null ? getMetrics().stringWidth(replaceNbsp(s))/SCALE : image.getScaledWidth(); |
|
|
|
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; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String replaceNbsp(String str) { |
|
|
|
|
|
|
|
return canDisplayNbsp() ? str : str.replaceAll(String.valueOf((char) 160), String.valueOf((char) 32)); |
|
|
|
} |
|
|
|
} |
|
|
|
if (image == null) |
|
|
|
|
|
|
|
return (float) width * hScale; |
|
|
|
int replaceNbsp(int character) { |
|
|
|
else |
|
|
|
return character == 160 ? (canDisplayNbsp() ? character : 32) : character; |
|
|
|
return image.getScaledWidth(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean canDisplayNbsp() { |
|
|
|
|
|
|
|
return getAwtFont().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(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()) { |
|
|
|
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(){ |
|
|
|