You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Neil McErlean (JIRA)" <ji...@apache.org> on 2010/05/28 11:51:37 UTC

[jira] Updated: (PDFBOX-732) Loading TTF font files from the classpath

     [ https://issues.apache.org/jira/browse/PDFBOX-732?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Neil McErlean updated PDFBOX-732:
---------------------------------

    Description: 
Currently (pdfbox 1.1.0 & 1.2.0 snapshot) TTF font files can only be loaded from the file system as PDTrueTypeFont exposes two load methods:
    public static PDTrueTypeFont loadTTF(PDDocument, String)
    public static PDTrueTypeFont loadTTF(PDDocument, File)
The first wraps the String in a java.io.File object and delegates to the second, so all TTF reading is from the file system.

It would be useful to be able to read TTF files from the classpath - or indeed from any arbitrary stream.
Could we have a third method?:
    public static PDTrueTypeFont loadTTF(PDDocument, InputStream)

This would allow TTFs to be loaded like so:
PDTrueTypeFont.load(myDoc, MyClass.class.getClassLoader().getResourceAsStream("myFont.ttf"));

For what it's worth here's a patch of what I did (see below).
One uncertainty I had was about the use of the COSName.LENGTH1 field which only seems to be used currently in PDTrueTypeFont. So it may be reasonable or not to push the setting of this attribute into the PDStream constructor.


Index: pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/PDStream.java
===================================================================
--- pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/PDStream.java	(revision 948363)
+++ pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/PDStream.java	(working copy)
@@ -114,12 +114,17 @@
             {
                 output = stream.createUnfilteredStream();
             }
+            int bytesInInputStream = 0;
             byte[] buffer = new byte[ 1024 ];
             int amountRead = -1;
             while( (amountRead = str.read(buffer)) != -1 )
             {
                 output.write( buffer, 0, amountRead );
+                bytesInInputStream += amountRead;
             }
+            
+            // Set the number of bytes read from the input stream
+            this.stream.setInt( COSName.LENGTH1, bytesInInputStream );
         }
         finally
         {
Index: pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java
===================================================================
--- pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java	(revision 948363)
+++ pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java	(working copy)
@@ -130,7 +130,7 @@
     }
 
     /**
-     * This will load a TTF to be embedding into a document.
+     * This will load a TTF to be embedded into a document.
      *
      * @param doc The PDF document that will hold the embedded font.
      * @param file A TTF file stream.
@@ -139,21 +139,32 @@
      */
     public static PDTrueTypeFont loadTTF( PDDocument doc, File file ) throws IOException
     {
+        return loadTTF( doc, new FileInputStream( file ) );
+    }
+
+    /**
+     * This will load a TTF to be embedded into a document.
+     *
+     * @param doc The PDF document that will hold the embedded font.
+     * @param stream A TTF input stream.
+     * @return A PDF TTF.
+     * @throws IOException If there is an error loading the data.
+     */
+    public static PDTrueTypeFont loadTTF( PDDocument doc, InputStream stream ) throws IOException
+    {
         PDTrueTypeFont retval = new PDTrueTypeFont();
         PDFontDescriptorDictionary fd = new PDFontDescriptorDictionary();
-        PDStream fontStream = new PDStream(doc, new FileInputStream( file ), false );
-        fontStream.getStream().setInt( COSName.LENGTH1, (int)file.length() );
+        PDStream fontStream = new PDStream(doc, stream, false );
         fontStream.addCompression();
         fd.setFontFile2( fontStream );
         retval.setFontDescriptor( fd );
-        InputStream ttfData = new FileInputStream(file);
         try
         {
-            loadDescriptorDictionary(retval, fd, ttfData);
+            loadDescriptorDictionary(retval, fd, stream);
         }
         finally
         {
-            ttfData.close();
+            stream.close();
         }
         //only support winansi encoding right now, should really
         //just use Identity-H with unicode mapping



  was:
Currently (pdfbox 1.1.0 & 1.2.0 snapshot) TTF font files can only be loaded from the file system as PDTrueTypeFont exposes two load methods:
    public static PDTrueTypeFont loadTTF(PDDocument, String)
    public static PDTrueTypeFont loadTTF(PDDocument, File)
The first wraps the String in a java.io.File object and delegates to the second, so all TTF reading is from the file system.

It would be useful to be able to read TTF files from the classpath - or indeed from any arbitrary stream.
Could we have a third method?:
    public static PDTrueTypeFont loadTTF(PDDocument, InputStream)

This would allow TTFs to be loaded like so:
PDTrueTypeFont.load(myDoc, MyClass.class.getClassLoader().getResourceAsStream("myFont.ttf"));



Adding a patch of the proposed changes.

> Loading TTF font files from the classpath
> -----------------------------------------
>
>                 Key: PDFBOX-732
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-732
>             Project: PDFBox
>          Issue Type: Improvement
>          Components: PDModel
>    Affects Versions: 1.1.0
>            Reporter: Neil McErlean
>
> Currently (pdfbox 1.1.0 & 1.2.0 snapshot) TTF font files can only be loaded from the file system as PDTrueTypeFont exposes two load methods:
>     public static PDTrueTypeFont loadTTF(PDDocument, String)
>     public static PDTrueTypeFont loadTTF(PDDocument, File)
> The first wraps the String in a java.io.File object and delegates to the second, so all TTF reading is from the file system.
> It would be useful to be able to read TTF files from the classpath - or indeed from any arbitrary stream.
> Could we have a third method?:
>     public static PDTrueTypeFont loadTTF(PDDocument, InputStream)
> This would allow TTFs to be loaded like so:
> PDTrueTypeFont.load(myDoc, MyClass.class.getClassLoader().getResourceAsStream("myFont.ttf"));
> For what it's worth here's a patch of what I did (see below).
> One uncertainty I had was about the use of the COSName.LENGTH1 field which only seems to be used currently in PDTrueTypeFont. So it may be reasonable or not to push the setting of this attribute into the PDStream constructor.
> Index: pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/PDStream.java
> ===================================================================
> --- pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/PDStream.java	(revision 948363)
> +++ pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/PDStream.java	(working copy)
> @@ -114,12 +114,17 @@
>              {
>                  output = stream.createUnfilteredStream();
>              }
> +            int bytesInInputStream = 0;
>              byte[] buffer = new byte[ 1024 ];
>              int amountRead = -1;
>              while( (amountRead = str.read(buffer)) != -1 )
>              {
>                  output.write( buffer, 0, amountRead );
> +                bytesInInputStream += amountRead;
>              }
> +            
> +            // Set the number of bytes read from the input stream
> +            this.stream.setInt( COSName.LENGTH1, bytesInInputStream );
>          }
>          finally
>          {
> Index: pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java
> ===================================================================
> --- pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java	(revision 948363)
> +++ pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java	(working copy)
> @@ -130,7 +130,7 @@
>      }
>  
>      /**
> -     * This will load a TTF to be embedding into a document.
> +     * This will load a TTF to be embedded into a document.
>       *
>       * @param doc The PDF document that will hold the embedded font.
>       * @param file A TTF file stream.
> @@ -139,21 +139,32 @@
>       */
>      public static PDTrueTypeFont loadTTF( PDDocument doc, File file ) throws IOException
>      {
> +        return loadTTF( doc, new FileInputStream( file ) );
> +    }
> +
> +    /**
> +     * This will load a TTF to be embedded into a document.
> +     *
> +     * @param doc The PDF document that will hold the embedded font.
> +     * @param stream A TTF input stream.
> +     * @return A PDF TTF.
> +     * @throws IOException If there is an error loading the data.
> +     */
> +    public static PDTrueTypeFont loadTTF( PDDocument doc, InputStream stream ) throws IOException
> +    {
>          PDTrueTypeFont retval = new PDTrueTypeFont();
>          PDFontDescriptorDictionary fd = new PDFontDescriptorDictionary();
> -        PDStream fontStream = new PDStream(doc, new FileInputStream( file ), false );
> -        fontStream.getStream().setInt( COSName.LENGTH1, (int)file.length() );
> +        PDStream fontStream = new PDStream(doc, stream, false );
>          fontStream.addCompression();
>          fd.setFontFile2( fontStream );
>          retval.setFontDescriptor( fd );
> -        InputStream ttfData = new FileInputStream(file);
>          try
>          {
> -            loadDescriptorDictionary(retval, fd, ttfData);
> +            loadDescriptorDictionary(retval, fd, stream);
>          }
>          finally
>          {
> -            ttfData.close();
> +            stream.close();
>          }
>          //only support winansi encoding right now, should really
>          //just use Identity-H with unicode mapping

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.