pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tilman Hausherr <THaush...@t-online.de>
Subject Re: PrintTextLocations 1.8 vs 2.0
Date Thu, 17 Mar 2016 17:07:25 GMT
I found a case where the strategy you mentioned didn't work for a TT 
font, the file 032431. Here's some updated code.


     private Shape calculateGlyphBounds(Matrix textRenderingMatrix, 
PDFont font, int code) throws IOException
     {
         GeneralPath path = null;
         AffineTransform at = textRenderingMatrix.createAffineTransform();
at.concatenate(font.getFontMatrix().createAffineTransform());
         if (font instanceof PDType3Font)
         {
             PDType3Font t3Font = (PDType3Font) font;
             PDType3CharProc charProc = t3Font.getCharProc(code);
             if (charProc != null)
             {
                 PDRectangle glyphBBox = charProc.getGlyphBBox();
                 if (glyphBBox != null)
                 {
                     path = glyphBBox.toGeneralPath();
                 }
             }
         }
         else if (font instanceof PDVectorFont)
         {
             PDVectorFont vectorFont = (PDVectorFont) font;
             path = vectorFont.getPath(code);

             if (font instanceof PDTrueTypeFont)
             {
                 PDTrueTypeFont ttFont = (PDTrueTypeFont) font;
                 int unitsPerEm = 
ttFont.getTrueTypeFont().getHeader().getUnitsPerEm();
                 at.scale(1000d / unitsPerEm, 1000d / unitsPerEm);
             }
             if (font instanceof PDType0Font)
             {
                 PDType0Font t0font = (PDType0Font) font;
                 if (t0font.getDescendantFont() instanceof PDCIDFontType2)
                 {
                     int unitsPerEm = ((PDCIDFontType2) 
t0font.getDescendantFont()).getTrueTypeFont().getHeader().getUnitsPerEm();
                     at.scale(1000d / unitsPerEm, 1000d / unitsPerEm);
                 }
             }
         }
         else if (font instanceof PDSimpleFont)
         {
             PDSimpleFont simpleFont = (PDSimpleFont) font;

             // these two lines do not always work, e.g. for the TT 
fonts in file 032431.pdf
             // which is why PDVectorFont is tried first.
             String name = simpleFont.getEncoding().getName(code);
             path = simpleFont.getPath(name);
         }
         else
         {
             // shouldn't happen, please open issue in JIRA
             System.out.println("Unknown font class: " + font.getClass());
         }
         if (path == null)
         {
             return null;
         }
         return at.createTransformedShape(path.getBounds2D());
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Mime
View raw message