use of org.apache.pdfbox.pdmodel.font.PDType0Font in project pdfbox by apache.
the class GlyphCache method getPathForCharacterCode.
public GeneralPath getPathForCharacterCode(int code) {
GeneralPath path = cache.get(code);
if (path != null) {
return path;
}
try {
if (!font.hasGlyph(code)) {
String fontName = ((PDFontLike) font).getName();
if (font instanceof PDType0Font) {
int cid = ((PDType0Font) font).codeToCID(code);
String cidHex = String.format("%04x", cid);
LOG.warn("No glyph for code " + code + " (CID " + cidHex + ") in font " + fontName);
} else if (font instanceof PDSimpleFont) {
LOG.warn("No glyph for code " + code + " in " + font.getClass().getSimpleName() + " " + fontName + " (embedded or system font used: " + ((PDSimpleFont) font).getFontBoxFont().getName() + ")");
PDSimpleFont simpleFont = (PDSimpleFont) font;
if (code == 10 && simpleFont.isStandard14()) {
// PDFBOX-4001 return empty path for line feed on std14
path = new GeneralPath();
cache.put(code, path);
return path;
}
} else {
LOG.warn("No glyph for code " + code + " in font " + fontName);
}
}
path = font.getNormalizedPath(code);
cache.put(code, path);
return path;
} catch (IOException e) {
// todo: escalate this error?
LOG.error("Glyph rendering failed", e);
return new GeneralPath();
}
}
use of org.apache.pdfbox.pdmodel.font.PDType0Font in project pdfbox by apache.
the class DrawPrintTextLocations method calculateGlyphBounds.
// this calculates the real (except for type 3 fonts) individual glyph bounds
private Shape calculateGlyphBounds(Matrix textRenderingMatrix, PDFont font, int code) throws IOException {
GeneralPath path = null;
AffineTransform at = textRenderingMatrix.createAffineTransform();
at.concatenate(font.getFontMatrix().createAffineTransform());
if (font instanceof PDType3Font) {
// It is difficult to calculate the real individual glyph bounds for type 3 fonts
// because these are not vector fonts, the content stream could contain almost anything
// that is found in page content streams.
PDType3Font t3Font = (PDType3Font) font;
PDType3CharProc charProc = t3Font.getCharProc(code);
if (charProc != null) {
BoundingBox fontBBox = t3Font.getBoundingBox();
PDRectangle glyphBBox = charProc.getGlyphBBox();
if (glyphBBox != null) {
// PDFBOX-3850: glyph bbox could be larger than the font bbox
glyphBBox.setLowerLeftX(Math.max(fontBBox.getLowerLeftX(), glyphBBox.getLowerLeftX()));
glyphBBox.setLowerLeftY(Math.max(fontBBox.getLowerLeftY(), glyphBBox.getLowerLeftY()));
glyphBBox.setUpperRightX(Math.min(fontBBox.getUpperRightX(), glyphBBox.getUpperRightX()));
glyphBBox.setUpperRightY(Math.min(fontBBox.getUpperRightY(), glyphBBox.getUpperRightY()));
path = glyphBBox.toGeneralPath();
}
}
} else if (font instanceof PDVectorFont) {
PDVectorFont vectorFont = (PDVectorFont) font;
path = vectorFont.getPath(code);
if (font instanceof PDTrueTypeFont) {
PDTrueTypeFont ttFont = (PDTrueTypeFont) font;
int unitsPerEm = ttFont.getTrueTypeFont().getHeader().getUnitsPerEm();
at.scale(1000d / unitsPerEm, 1000d / unitsPerEm);
}
if (font instanceof PDType0Font) {
PDType0Font t0font = (PDType0Font) font;
if (t0font.getDescendantFont() instanceof PDCIDFontType2) {
int unitsPerEm = ((PDCIDFontType2) t0font.getDescendantFont()).getTrueTypeFont().getHeader().getUnitsPerEm();
at.scale(1000d / unitsPerEm, 1000d / unitsPerEm);
}
}
} else if (font instanceof PDSimpleFont) {
PDSimpleFont simpleFont = (PDSimpleFont) font;
// these two lines do not always work, e.g. for the TT fonts in file 032431.pdf
// which is why PDVectorFont is tried first.
String name = simpleFont.getEncoding().getName(code);
path = simpleFont.getPath(name);
} else {
// shouldn't happen, please open issue in JIRA
System.out.println("Unknown font class: " + font.getClass());
}
if (path == null) {
return null;
}
return at.createTransformedShape(path.getBounds2D());
}
use of org.apache.pdfbox.pdmodel.font.PDType0Font in project pancm_project by xuwujing.
the class PdfBoxTest method main.
public static void main(String[] args) throws IOException {
// String formTemplate = "E:/module.pdf";
String formTemplate = "module.pdf";
// 定义文档对象
PDDocument document = new PDDocument();
// 定义一页,大小A4
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
// 获取字体
PDType0Font font = PDType0Font.load(document, new File("C:\\Windows\\Fonts\\Arial.ttf"));
// 定义页面内容流
PDPageContentStream stream = new PDPageContentStream(document, page);
// 设置字体及文字大小
stream.setFont(font, 12);
// 设置画笔颜色
stream.setNonStrokingColor(Color.BLACK);
// 添加矩形
stream.addRect(29, 797, 100, 14);
// 填充矩形
stream.fill();
stream.setNonStrokingColor(Color.BLACK);
// 文本填充开始
stream.beginText();
// 设置行距
stream.setLeading(18f);
// 设置文字位置
stream.newLineAtOffset(30, 800);
// 填充文字
stream.showText("11");
// 换行
stream.newLine();
stream.showText("22");
stream.newLine();
stream.showText("33");
// 文本填充结束
stream.endText();
// 关闭流
stream.close();
// 保存
document.save(formTemplate);
// 释放资源
document.close();
}
Aggregations