use of org.apache.pdfbox.pdmodel.font.PDType0Font in project pdfbox by apache.
the class LegacyPDFStreamEngine method showGlyph.
/**
* This method was originally written by Ben Litchfield for PDFStreamEngine.
*/
@Override
protected void showGlyph(Matrix textRenderingMatrix, PDFont font, int code, String unicode, Vector displacement) throws IOException {
//
// legacy calculations which were previously in PDFStreamEngine
//
// DO NOT USE THIS CODE UNLESS YOU ARE WORKING WITH PDFTextStripper.
// THIS CODE IS DELIBERATELY INCORRECT
//
PDGraphicsState state = getGraphicsState();
Matrix ctm = state.getCurrentTransformationMatrix();
float fontSize = state.getTextState().getFontSize();
float horizontalScaling = state.getTextState().getHorizontalScaling() / 100f;
Matrix textMatrix = getTextMatrix();
BoundingBox bbox = font.getBoundingBox();
if (bbox.getLowerLeftY() < Short.MIN_VALUE) {
// PDFBOX-2158 and PDFBOX-3130
// files by Salmat eSolutions / ClibPDF Library
bbox.setLowerLeftY(-(bbox.getLowerLeftY() + 65536));
}
// 1/2 the bbox is used as the height todo: why?
float glyphHeight = bbox.getHeight() / 2;
// sometimes the bbox has very high values, but CapHeight is OK
PDFontDescriptor fontDescriptor = font.getFontDescriptor();
if (fontDescriptor != null) {
float capHeight = fontDescriptor.getCapHeight();
if (Float.compare(capHeight, 0) != 0 && (capHeight < glyphHeight || Float.compare(glyphHeight, 0) == 0)) {
glyphHeight = capHeight;
}
}
// transformPoint from glyph space -> text space
float height;
if (font instanceof PDType3Font) {
height = font.getFontMatrix().transformPoint(0, glyphHeight).y;
} else {
height = glyphHeight / 1000;
}
float displacementX = displacement.getX();
// calculate our own
if (font.isVertical()) {
displacementX = font.getWidth(code) / 1000;
// there may be an additional scaling factor for true type fonts
TrueTypeFont ttf = null;
if (font instanceof PDTrueTypeFont) {
ttf = ((PDTrueTypeFont) font).getTrueTypeFont();
} else if (font instanceof PDType0Font) {
PDCIDFont cidFont = ((PDType0Font) font).getDescendantFont();
if (cidFont instanceof PDCIDFontType2) {
ttf = ((PDCIDFontType2) cidFont).getTrueTypeFont();
}
}
if (ttf != null && ttf.getUnitsPerEm() != 1000) {
displacementX *= 1000f / ttf.getUnitsPerEm();
}
}
//
// legacy calculations which were previously in PDFStreamEngine
//
// DO NOT USE THIS CODE UNLESS YOU ARE WORKING WITH PDFTextStripper.
// THIS CODE IS DELIBERATELY INCORRECT
//
// (modified) combined displacement, this is calculated *without* taking the character
// spacing and word spacing into account, due to legacy code in TextStripper
float tx = displacementX * fontSize * horizontalScaling;
float ty = displacement.getY() * fontSize;
// (modified) combined displacement matrix
Matrix td = Matrix.getTranslateInstance(tx, ty);
// (modified) text rendering matrix
// text space -> device space
Matrix nextTextRenderingMatrix = td.multiply(textMatrix).multiply(ctm);
float nextX = nextTextRenderingMatrix.getTranslateX();
float nextY = nextTextRenderingMatrix.getTranslateY();
// (modified) width and height calculations
float dxDisplay = nextX - textRenderingMatrix.getTranslateX();
float dyDisplay = height * textRenderingMatrix.getScalingFactorY();
//
// start of the original method
//
// Note on variable names. There are three different units being used in this code.
// Character sizes are given in glyph units, text locations are initially given in text
// units, and we want to save the data in display units. The variable names should end with
// Text or Disp to represent if the values are in text or disp units (no glyph units are
// saved).
float glyphSpaceToTextSpaceFactor = 1 / 1000f;
if (font instanceof PDType3Font) {
glyphSpaceToTextSpaceFactor = font.getFontMatrix().getScaleX();
}
float spaceWidthText = 0;
try {
// to avoid crash as described in PDFBOX-614, see what the space displacement should be
spaceWidthText = font.getSpaceWidth() * glyphSpaceToTextSpaceFactor;
} catch (Exception exception) {
LOG.warn(exception, exception);
}
if (Float.compare(spaceWidthText, 0) == 0) {
spaceWidthText = font.getAverageFontWidth() * glyphSpaceToTextSpaceFactor;
// the average space width appears to be higher than necessary so make it smaller
spaceWidthText *= .80f;
}
if (Float.compare(spaceWidthText, 0) == 0) {
// if could not find font, use a generic value
spaceWidthText = 1.0f;
}
// the space width has to be transformed into display units
float spaceWidthDisplay = spaceWidthText * textRenderingMatrix.getScalingFactorX();
// use our additional glyph list for Unicode mapping
unicode = font.toUnicode(code, glyphList);
// this, which is why we leave it until this point in PDFTextStreamEngine.
if (unicode == null) {
if (font instanceof PDSimpleFont) {
char c = (char) code;
unicode = new String(new char[] { c });
} else {
// skips them. See the "allah2.pdf" TestTextStripper file.
return;
}
}
// adjust for cropbox if needed
Matrix translatedTextRenderingMatrix;
if (translateMatrix == null) {
translatedTextRenderingMatrix = textRenderingMatrix;
} else {
translatedTextRenderingMatrix = Matrix.concatenate(translateMatrix, textRenderingMatrix);
nextX -= pageSize.getLowerLeftX();
nextY -= pageSize.getLowerLeftY();
}
processTextPosition(new TextPosition(pageRotation, pageSize.getWidth(), pageSize.getHeight(), translatedTextRenderingMatrix, nextX, nextY, Math.abs(dyDisplay), dxDisplay, Math.abs(spaceWidthDisplay), unicode, new int[] { code }, font, fontSize, (int) (fontSize * textMatrix.getScalingFactorX())));
}
use of org.apache.pdfbox.pdmodel.font.PDType0Font in project pdfbox by apache.
the class EmbeddedFonts method main.
public static void main(String[] args) throws IOException {
try (PDDocument document = new PDDocument()) {
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
String dir = "../pdfbox/src/main/resources/org/apache/pdfbox/resources/ttf/";
PDType0Font font = PDType0Font.load(document, new File(dir + "LiberationSans-Regular.ttf"));
try (PDPageContentStream stream = new PDPageContentStream(document, page)) {
stream.beginText();
stream.setFont(font, 12);
stream.setLeading(12 * 1.2f);
stream.newLineAtOffset(50, 600);
stream.showText("PDFBox's Unicode with Embedded TrueType Font");
stream.newLine();
stream.showText("Supports full Unicode text ☺");
stream.newLine();
stream.showText("English русский язык Tiếng Việt");
stream.newLine();
// ligature
stream.showText("Ligatures: \uFB01lm \uFB02ood");
stream.endText();
}
document.save("example.pdf");
}
}
use of org.apache.pdfbox.pdmodel.font.PDType0Font in project pdfbox by apache.
the class EmbeddedVerticalFonts method main.
public static void main(String[] args) throws IOException {
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
// The actual font file
// Download: https://ipafont.ipa.go.jp/old/ipafont/ipag00303.php
// (free license: https://www.gnu.org/licenses/license-list.html#IPAFONT)
File ipafont = new File("ipag.ttf");
// You can also use a Windows 7 TrueType font collection, e.g. MingLiU:
// TrueTypeFont ttf = new TrueTypeCollection(new File("C:/windows/fonts/mingliu.ttc")).getFontByName("MingLiU")
// PDType0Font.loadVertical(document, ttf, true)
// Load as horizontal
PDType0Font hfont = PDType0Font.load(document, ipafont);
// Load as vertical
PDType0Font vfont = PDType0Font.loadVertical(document, ipafont);
// Load as vertical, but disable vertical glyph substitution
// (You will usually not want this because it doesn't look good!)
TrueTypeFont ttf = new TTFParser().parse(ipafont);
PDType0Font vfont2 = PDType0Font.loadVertical(document, ttf, true);
ttf.disableGsubFeature("vrt2");
ttf.disableGsubFeature("vert");
try (PDPageContentStream contentStream = new PDPageContentStream(document, page)) {
contentStream.beginText();
contentStream.setFont(hfont, 20);
contentStream.setLeading(25);
contentStream.newLineAtOffset(20, 300);
contentStream.showText("Key:");
contentStream.newLine();
contentStream.showText("① Horizontal");
contentStream.newLine();
contentStream.showText("② Vertical with substitution");
contentStream.newLine();
contentStream.showText("③ Vertical without substitution");
contentStream.endText();
contentStream.beginText();
contentStream.setFont(hfont, 20);
contentStream.newLineAtOffset(20, 650);
contentStream.showText("①「あーだこーだ」");
contentStream.endText();
contentStream.beginText();
contentStream.setFont(vfont, 20);
contentStream.newLineAtOffset(50, 600);
contentStream.showText("②「あーだこーだ」");
contentStream.endText();
contentStream.beginText();
contentStream.setFont(vfont2, 20);
contentStream.newLineAtOffset(100, 600);
contentStream.showText("③「あーだこーだ」");
contentStream.endText();
}
// result file should look like the one attached to JIRA issue PDFBOX-4106
document.save("vertical.pdf");
}
use of org.apache.pdfbox.pdmodel.font.PDType0Font in project pdfbox by apache.
the class ExtractTTFFonts method processResources.
private void processResources(PDResources resources, String prefix, boolean addKey) throws IOException {
if (resources == null) {
return;
}
for (COSName key : resources.getFontNames()) {
PDFont font = resources.getFont(key);
// write the font
if (font instanceof PDTrueTypeFont) {
String name;
if (addKey) {
name = getUniqueFileName(prefix + "_" + key, "ttf");
} else {
name = getUniqueFileName(prefix, "ttf");
}
writeFont(font.getFontDescriptor(), name);
} else if (font instanceof PDType0Font) {
PDCIDFont descendantFont = ((PDType0Font) font).getDescendantFont();
if (descendantFont instanceof PDCIDFontType2) {
String name;
if (addKey) {
name = getUniqueFileName(prefix + "_" + key, "ttf");
} else {
name = getUniqueFileName(prefix, "ttf");
}
writeFont(descendantFont.getFontDescriptor(), name);
}
}
}
for (COSName name : resources.getXObjectNames()) {
PDXObject xobject = resources.getXObject(name);
if (xobject instanceof PDFormXObject) {
PDFormXObject xObjectForm = (PDFormXObject) xobject;
PDResources formResources = xObjectForm.getResources();
processResources(formResources, prefix, addKey);
}
}
}
use of org.apache.pdfbox.pdmodel.font.PDType0Font in project mustangproject by ZUGFeRD.
the class ZUGFeRDExporterFromA3 method removeCidSet.
private void removeCidSet(PDDocumentCatalog catalog, PDDocument doc) throws IOException {
// https://github.com/ZUGFeRD/mustangproject/issues/249
COSName cidSet = COSName.getPDFName("CIDSet");
for (Object object : doc.getPages()) {
if (object instanceof PDPage) {
PDPage page = (PDPage) object;
PDResources res = page.getResources();
for (COSName fontName : res.getFontNames()) {
try {
PDFont pdFont = res.getFont(fontName);
if (pdFont instanceof PDType0Font) {
PDType0Font typedFont = (PDType0Font) pdFont;
if (typedFont.getDescendantFont() instanceof PDCIDFontType2) {
PDCIDFontType2 f = (PDCIDFontType2) typedFont.getDescendantFont();
PDFontDescriptor fontDescriptor = pdFont.getFontDescriptor();
fontDescriptor.getCOSObject().removeItem(cidSet);
}
}
} catch (IOException e) {
throw e;
}
// do stuff with the font
}
}
}
}
Aggregations