/** * create a new NativeFont object based on a description of the font from the PDF file. If the * description happens to contain an in-line true-type font file (under key "FontFile2"), use the * true type font. Otherwise, parse the description for key information and use that to generate * an appropriate font. */ public NativeFont(String baseFont, PDFObject fontObj, PDFFontDescriptor descriptor) throws IOException { super(baseFont, fontObj, descriptor); String fontName = descriptor.getFontName(); PDFObject ttf = descriptor.getFontFile2(); if (ttf != null) { byte[] fontdata = ttf.getStream(); try { setFont(fontdata); } catch (FontFormatException ffe) { throw new PDFParseException("Font format exception: " + ffe); } } else { int flags = descriptor.getFlags(); int style = ((flags & PDFFontDescriptor.FORCEBOLD) != 0) ? Font.BOLD : Font.PLAIN; if (fontName.indexOf("Bold") > 0) { style |= Font.BOLD; } if (descriptor.getItalicAngle() != 0) { style |= Font.ITALIC; } if ((flags & PDFFontDescriptor.FIXED_PITCH) != 0) { // fixed width setFont(new Font("Monospaced", style, 1)); } else if ((flags & PDFFontDescriptor.SERIF) != 0) { // serif font setFont(new Font("Serif", style, 1)); } else { setFont(new Font("Sans-serif", style, 1)); } } }
/** * create a new BuiltingFont object based on a description of the font from the PDF file. Parse * the description for key information and use that to generate an appropriate font. */ public BuiltinFont(String baseFont, PDFObject fontObj, PDFFontDescriptor descriptor) throws IOException { super(baseFont, fontObj, descriptor); String fontName = descriptor.getFontName(); // check if it's one of the 14 base fonts for (int i = 0; i < baseFonts.length; i++) { if (fontName.equalsIgnoreCase(baseFonts[i])) { parseFont(fontName); return; } } // check if it's a mapped font for (int i = 0; i < mappedFonts.length; i += 2) { if (fontName.equalsIgnoreCase(mappedFonts[i])) { parseFont(mappedFonts[i + 1]); return; } } int flags = descriptor.getFlags(); int style = ((flags & PDFFontDescriptor.FORCEBOLD) != 0) ? Font.BOLD : Font.PLAIN; if (fontName.indexOf("Bold") > 0) { style |= Font.BOLD; } if ((descriptor.getItalicAngle() != 0) || ((flags & (PDFFontDescriptor.SCRIPT | PDFFontDescriptor.ITALIC)) != 0)) { style |= Font.ITALIC; } String name = null; if ((flags & PDFFontDescriptor.FIXED_PITCH) != 0) { // fixed width if (((style & Font.BOLD) > 0) && ((style & Font.ITALIC) > 0)) { name = "Courier-BoldOblique"; } else if ((style & Font.BOLD) > 0) { name = "Courier-Bold"; } else if ((style & Font.ITALIC) > 0) { name = "Courier-Oblique"; } else { name = "Courier"; } } else if ((flags & PDFFontDescriptor.SERIF) != 0) { // serif font if (((style & Font.BOLD) > 0) && ((style & Font.ITALIC) > 0)) { name = "Times-BoldItalic"; } else if ((style & Font.BOLD) > 0) { name = "Times-Bold"; } else if ((style & Font.ITALIC) > 0) { name = "Times-Italic"; } else { name = "Times-Roman"; } } else { if (((style & Font.BOLD) > 0) && ((style & Font.ITALIC) > 0)) { name = "Helvetica-BoldOblique"; } else if ((style & Font.BOLD) > 0) { name = "Helvetica-Bold"; } else if ((style & Font.ITALIC) > 0) { name = "Helvetica-Oblique"; } else { name = "Helvetica"; } } parseFont(name); }