Author: jahewson Date: Mon Jul 13 06:50:16 2015 New Revision: 1690572 URL: http://svn.apache.org/r1690572 Log: PDFBOX-2876: Better embedding of simple TrueType fonts
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFontEmbedder.java pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType0Font.java Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java?rev=1690572&r1=1690571&r2=1690572&view=diff ============================================================================== --- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java (original) +++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java Mon Jul 13 06:50:16 2015 @@ -42,6 +42,7 @@ import org.apache.pdfbox.pdmodel.font.en import org.apache.pdfbox.pdmodel.font.encoding.GlyphList; import org.apache.pdfbox.pdmodel.font.encoding.MacOSRomanEncoding; import org.apache.pdfbox.pdmodel.font.encoding.Type1Encoding; +import org.apache.pdfbox.pdmodel.font.encoding.WinAnsiEncoding; /** * TrueType font. @@ -70,10 +71,46 @@ public class PDTrueTypeFont extends PDSi } /** - * Loads a TTF to be embedded into a document. + * 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 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 FileInputStream(file), encoding); + } + + /** + * 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 input A TTF file stream + * @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, InputStream input, Encoding encoding) + throws IOException + { + return new PDTrueTypeFont(doc, input, encoding); + } + + /** + * Loads a TTF to be embedded into a document as a simple font. Only supports WinAnsiEncoding. + * + * @param doc The PDF document that will hold the embedded font. + * @param file A TTF file. * @return a PDTrueTypeFont instance. * @throws IOException If there is an error loading the data. * @@ -82,14 +119,14 @@ public class PDTrueTypeFont extends PDSi @Deprecated public static PDTrueTypeFont loadTTF(PDDocument doc, File file) throws IOException { - return new PDTrueTypeFont(doc, new FileInputStream(file)); + return new PDTrueTypeFont(doc, new FileInputStream(file), WinAnsiEncoding.INSTANCE); } /** - * Loads a TTF to be embedded into a document. + * Loads a TTF to be embedded into a document as a simple font. Only supports WinAnsiEncoding. * * @param doc The PDF document that will hold the embedded font. - * @param input a ttf file stream + * @param input A TTF file stream * @return a PDTrueTypeFont instance. * @throws IOException If there is an error loading the data. * @@ -98,7 +135,7 @@ public class PDTrueTypeFont extends PDSi @Deprecated public static PDTrueTypeFont loadTTF(PDDocument doc, InputStream input) throws IOException { - return new PDTrueTypeFont(doc, input); + return new PDTrueTypeFont(doc, input, WinAnsiEncoding.INSTANCE); } private CmapSubtable cmapWinUnicode = null; @@ -211,10 +248,12 @@ public class PDTrueTypeFont extends PDSi /** * Creates a new TrueType font for embedding. */ - private PDTrueTypeFont(PDDocument document, InputStream ttfStream) throws IOException + private PDTrueTypeFont(PDDocument document, InputStream ttfStream, Encoding encoding) + throws IOException { - PDTrueTypeFontEmbedder embedder = new PDTrueTypeFontEmbedder(document, dict, ttfStream); - encoding = embedder.getFontEncoding(); + PDTrueTypeFontEmbedder embedder = new PDTrueTypeFontEmbedder(document, dict, ttfStream, + encoding); + this.encoding = encoding; ttf = embedder.getTrueTypeFont(); setFontDescriptor(embedder.getFontDescriptor()); isEmbedded = true; Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFontEmbedder.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFontEmbedder.java?rev=1690572&r1=1690571&r2=1690572&view=diff ============================================================================== --- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFontEmbedder.java (original) +++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFontEmbedder.java Mon Jul 13 06:50:16 2015 @@ -30,7 +30,6 @@ import org.apache.pdfbox.pdmodel.PDDocum import org.apache.pdfbox.pdmodel.common.COSArrayList; import org.apache.pdfbox.pdmodel.font.encoding.Encoding; import org.apache.pdfbox.pdmodel.font.encoding.GlyphList; -import org.apache.pdfbox.pdmodel.font.encoding.WinAnsiEncoding; /** * Embedded PDTrueTypeFont builder. Helper class to populate a PDTrueTypeFont from a TTF. @@ -45,19 +44,18 @@ final class PDTrueTypeFontEmbedder exten /** * Creates a new TrueType font embedder for the given TTF as a PDTrueTypeFont. * - * @param document parent document - * @param dict font dictionary + * @param document The parent document + * @param dict Font dictionary * @param ttfStream TTF stream + * @param encoding The PostScript encoding vector to be used for embedding. * @throws IOException if the TTF could not be read */ - PDTrueTypeFontEmbedder(PDDocument document, COSDictionary dict, InputStream ttfStream) - throws IOException + PDTrueTypeFontEmbedder(PDDocument document, COSDictionary dict, InputStream ttfStream, + Encoding encoding) throws IOException { super(document, dict, ttfStream, false); dict.setItem(COSName.SUBTYPE, COSName.TRUE_TYPE); - - // only support WinAnsiEncoding encoding right now - Encoding encoding = new WinAnsiEncoding(); + GlyphList glyphList = GlyphList.getAdobeGlyphList(); this.fontEncoding = encoding; dict.setItem(COSName.ENCODING, encoding.getCOSObject()); Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType0Font.java URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType0Font.java?rev=1690572&r1=1690571&r2=1690572&view=diff ============================================================================== --- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType0Font.java (original) +++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDType0Font.java Mon Jul 13 06:50:16 2015 @@ -49,7 +49,7 @@ public class PDType0Font extends PDFont private PDCIDFontType2Embedder embedder; /** - * Loads a TTF to be embedded into a document. + * Loads a TTF to be embedded into a document as a Type 0 font. * * @param doc The PDF document that will hold the embedded font. * @param file A TrueType font. @@ -62,7 +62,7 @@ public class PDType0Font extends PDFont } /** - * Loads a TTF to be embedded into a document. + * Loads a TTF to be embedded into a document as a Type 0 font. * * @param doc The PDF document that will hold the embedded font. * @param input A TrueType font. @@ -75,7 +75,7 @@ public class PDType0Font extends PDFont } /** - * Loads a TTF to be embedded into a document. + * Loads a TTF to be embedded into a document as a Type 0 font. * * @param doc The PDF document that will hold the embedded font. * @param input A TrueType font.