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 Wed, 16 Mar 2016 16:49:44 GMT
Am 16.03.2016 um 09:52 schrieb Peter Prusinowski:
>
>
> thank you for the hints, now I am overwriting showGlyph() and trying 
> to get the value with
>
>             PDSimpleFont sf = (PDSimpleFont) font;
>             String name = sf.getEncoding().getName(code);
>             sf.getPath(name).getBounds()
>
> but I am getting the same height, no matter which font size is set. 
> This happens with type1 and truetype fonts. What am I doing wrong ? 

Here's some code, use it with the DrawPrintTextLocations example. Please 
tell if it works, and if possible, upload files where it doesn't.


     @Override
     protected void showGlyph(Matrix textRenderingMatrix, PDFont font, 
int code, String unicode, Vector displacement) throws IOException
     {
         super.showGlyph(textRenderingMatrix, font, code, unicode, 
displacement);

         Rectangle2D bounds = null;
         AffineTransform at = textRenderingMatrix.createAffineTransform();

         if (font instanceof PDSimpleFont)
         {
             PDSimpleFont simpleFont = (PDSimpleFont) font;
             String name = simpleFont.getEncoding().getName(code);
             GeneralPath path = simpleFont.getPath(name);
             bounds = path.getBounds2D();

             at.scale(1/1000f, 1/1000f);

             if (font instanceof PDTrueTypeFont)
             {
                 PDTrueTypeFont ttFont = (PDTrueTypeFont) font;
                 int unitsPerEm = 
ttFont.getTrueTypeFont().getHeader().getUnitsPerEm();
                 at.scale(1000d / unitsPerEm, 1000d / unitsPerEm);
             }
         }
         else if (font instanceof PDVectorFont)
         {
             PDVectorFont vectorFont = (PDVectorFont) font;
             GeneralPath path = vectorFont.getPath(code);
             bounds = path.getBounds2D();
             at.scale(1/1000f, 1/1000f);

             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
         {
             System.out.println("TODO other: " + font.getClass());
         }
         if (bounds != null)
         {
             Shape s = at.createTransformedShape(bounds);

             // flip y-axis
             AffineTransform flip = new AffineTransform();
             flip.translate(0, getCurrentPage().getBBox().getHeight());
             flip.scale(1, -1);
             s = flip.createTransformedShape(s);

             AffineTransform transform = g2d.getTransform();
             int rotation = getCurrentPage().getRotation();
             if (rotation != 0)
             {
                 PDRectangle mediaBox = getCurrentPage().getMediaBox();
                 switch (rotation)
                 {
                     case 90:
                         g2d.translate(mediaBox.getHeight(), 0);
                         break;
                     case 270:
                         g2d.translate(0, mediaBox.getWidth());
                         break;
                     case 180:
                         g2d.translate(mediaBox.getWidth(), 
mediaBox.getHeight());
                         break;
                     default:
                         break;
                 }
                 g2d.rotate(Math.toRadians(rotation));
             }

             g2d.setColor(Color.CYAN);
             g2d.draw(s);

             if (rotation != 0)
             {
                 g2d.setTransform(transform);
             }
         }
     }


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


Mime
View raw message