@Override protected Encoding readEncodingFromFont() throws IOException if (!isEmbedded() && getStandard14AFM() != null) return new Type1Encoding(getStandard14AFM()); if (getSymbolicFlag() != null &&!getSymbolicFlag()) String standard14Name = Standard14Fonts.getMappedFontName(getName()); if (isStandard14() && !standard14Name.equals("Symbol") && !standard14Name.equals("ZapfDingbats")) for (int code = 0; code <= 256; code++) int gid = codeToGID(code); if (gid > 0)
if (getFontDescriptor() != null) LOG.warn("Could not read embedded TTF for font " + getBaseFont(), e); fontIsDamaged = true; .getTrueTypeFont(getBaseFont(), getFontDescriptor()); ttfFont = mapping.getFont(); LOG.warn("Using fallback font '" + ttfFont + "' for '" + getBaseFont() + "'"); readEncoding();
PDFontDescriptor fd = ttFont.getFontDescriptor(); if (fd != null) Encoding encodingValue = ttFont.getEncoding(); if (!(encodingValue instanceof MacRomanEncoding || encodingValue instanceof WinAnsiEncoding))
@Override public boolean hasGlyph(int code) throws IOException { if (font.isEmbedded()) { int gid = font.codeToGID(code); return gid != 0; } return false; } }
@Override public GeneralPath getNormalizedPath(int code) throws IOException { boolean hasScaling = ttf.getUnitsPerEm() != 1000; float scale = 1000f / ttf.getUnitsPerEm(); int gid = codeToGID(code); GeneralPath path = getPath(code); // Acrobat only draws GID 0 for embedded or "Standard 14" fonts, see PDFBOX-2372 if (gid == 0 && !isEmbedded() && !isStandard14()) { path = null; } if (path == null) { // empty glyph (e.g. space, newline) return new GeneralPath(); } else { if (hasScaling) { path.transform(AffineTransform.getScaleInstance(scale, scale)); } return path; } }
/** * Constructor. * * @param ttfFont TrueType font */ TTFGlyph2D(PDTrueTypeFont ttfFont) throws IOException { this(ttfFont.getTrueTypeFont(), ttfFont, false); vectorFont = ttfFont; }
@Override protected void processFontFile(PDFontDescriptor fontDescriptor, PDStream fontFile) { if (font.isDamaged()) { this.fContainer.push(new ValidationError(ERROR_FONTS_TRUETYPE_DAMAGED, this.font.getName() + ": The FontFile can't be read")); } else { // there must be exactly one encoding in the "cmap" table if the font is symbolic TrueTypeFont ttf = pdTrueTypeFont.getTrueTypeFont(); try { if (pdTrueTypeFont.isSymbolic() && ttf.getCmap().getCmaps().length != 1) { this.fContainer.push(new ValidationError(ERROR_FONTS_ENCODING, this.font.getName() + ": Symbolic TrueType font has more than one 'cmap' entry")); } } catch (IOException e) { this.fContainer.push(new ValidationError(ERROR_FONTS_TRUETYPE_DAMAGED, this.font.getName() + ": The TTF 'cmap' could not be read")); } } } }
@Override public boolean hasGlyph(int code) throws IOException { return codeToGID(code) != 0; }
extractCmapTable(); int gid = 0; if (!isSymbolic()) // non-symbolic
/** * Loads a TTF to be embedded into a document as a simple font. * * <p> * <b>Note:</b> Simple fonts only support 256 characters. For Unicode support, use * {@link PDType0Font#load(PDDocument, InputStream)} instead. * </p> * * @param doc The PDF document that will hold the embedded font. * @param ttf A true type font * @param encoding The PostScript encoding vector to be used for embedding. * @return a PDTrueTypeFont instance. * @throws IOException If there is an error loading the data. */ public static PDTrueTypeFont load(PDDocument doc, TrueTypeFont ttf, Encoding encoding) throws IOException { return new PDTrueTypeFont(doc, ttf, encoding, false); }
private BoundingBox generateBoundingBox() throws IOException { if (getFontDescriptor() != null) { PDRectangle bbox = getFontDescriptor().getFontBoundingBox(); if (bbox != null) { return new BoundingBox(bbox.getLowerLeftX(), bbox.getLowerLeftY(), bbox.getUpperRightX(), bbox.getUpperRightY()); } } return ttf.getFontBBox(); }
@Override public Boolean getdifferencesAreUnicodeCompliant() { Encoding encoding = ((org.apache.pdfbox.pdmodel.font.PDTrueTypeFont) this.pdFontLike).getEncoding(); if (encoding != null && encoding instanceof DictionaryEncoding) { GlyphList glyphList = GlyphList.getAdobeGlyphList(); Map<Integer, String> differences = ((DictionaryEncoding) encoding).getDifferences(); if (differences == null || differences.isEmpty()) { return Boolean.TRUE; } for (Map.Entry<Integer, String> entry : differences.entrySet()) { if (!glyphList.containsGlyphName(entry.getValue()).booleanValue()) { return Boolean.FALSE; } } if (((org.apache.pdfbox.pdmodel.font.PDTrueTypeFont) this.pdFontLike).getCmapWinUnicode() == null) { return Boolean.FALSE; } } return Boolean.TRUE; }
int unitsPerEm = ttFont.getTrueTypeFont().getHeader().getUnitsPerEm(); at.scale(1000d / unitsPerEm, 1000d / unitsPerEm);
@Override protected void processFontFile(PDFontDescriptor fontDescriptor, PDStream fontFile) { if (font.isDamaged()) { this.fContainer.push(new ValidationError(ERROR_FONTS_TRUETYPE_DAMAGED, this.font.getName() + ": The FontFile can't be read")); } else { // there must be exactly one encoding in the "cmap" table if the font is symbolic TrueTypeFont ttf = pdTrueTypeFont.getTrueTypeFont(); try { if (pdTrueTypeFont.isSymbolic() && ttf.getCmap().getCmaps().length != 1) { this.fContainer.push(new ValidationError(ERROR_FONTS_ENCODING, this.font.getName() + ": Symbolic TrueType font has more than one 'cmap' entry")); } } catch (IOException e) { this.fContainer.push(new ValidationError(ERROR_FONTS_TRUETYPE_DAMAGED, this.font.getName() + ": The TTF 'cmap' could not be read")); } } } }
@Override public GeneralPath getNormalizedPath(int code) throws IOException { boolean hasScaling = ttf.getUnitsPerEm() != 1000; float scale = 1000f / ttf.getUnitsPerEm(); int gid = codeToGID(code); GeneralPath path = getPath(code); // Acrobat only draws GID 0 for embedded or "Standard 14" fonts, see PDFBOX-2372 if (gid == 0 && !isEmbedded() && !isStandard14()) { path = null; } if (path == null) { // empty glyph (e.g. space, newline) return new GeneralPath(); } else { if (hasScaling) { path.transform(AffineTransform.getScaleInstance(scale, scale)); } return path; } }
/** * Inverts the font's code -> GID mapping. Any duplicate (GID -> code) mappings will be lost. */ protected Map<Integer, Integer> getGIDToCode() throws IOException { if (gidToCode != null) { return gidToCode; } gidToCode = new HashMap<>(); for (int code = 0; code <= 255; code++) { int gid = codeToGID(code); if (!gidToCode.containsKey(gid)) { gidToCode.put(gid, code); } } return gidToCode; }
@Override public boolean hasGlyph(int code) throws IOException { if (font.isEmbedded()) { int gid = font.codeToGID(code); return gid != 0; } return false; } }
extractCmapTable(); int gid = 0; if (!isSymbolic()) // non-symbolic
/** * Loads a TTF to be embedded into a document as a simple font. * * <p><b>Note:</b> Simple fonts only support 256 characters. For Unicode support, use * {@link PDType0Font#load(PDDocument, File)} instead.</p> * * @param doc The PDF document that will hold the embedded font. * @param file A TTF file. * @param encoding The PostScript encoding vector to be used for embedding. * @return a PDTrueTypeFont instance. * @throws IOException If there is an error loading the data. */ public static PDTrueTypeFont load(PDDocument doc, File file, Encoding encoding) throws IOException { return new PDTrueTypeFont(doc, new TTFParser().parse(file), encoding, true); }
private BoundingBox generateBoundingBox() throws IOException { if (getFontDescriptor() != null) { PDRectangle bbox = getFontDescriptor().getFontBoundingBox(); if (bbox != null) { return new BoundingBox(bbox.getLowerLeftX(), bbox.getLowerLeftY(), bbox.getUpperRightX(), bbox.getUpperRightY()); } } return ttf.getFontBBox(); }