Browse Source

REPORT-10262 html大文本精确分页

final/10.0.3
kerry 6 years ago
parent
commit
6a959c0b33
  1. 42
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfFont.java

42
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;
/**
* <CODE>PdfFont</CODE> 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 <CODE>PdfFont</CODE> with another
*
* @param object the other <CODE>PdfFont</CODE>
* @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);

Loading…
Cancel
Save