You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2020/11/24 19:08:32 UTC

svn commit: r1883798 - /pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/pagepane/DebugTextOverlay.java

Author: tilman
Date: Tue Nov 24 19:08:32 2020
New Revision: 1883798

URL: http://svn.apache.org/viewvc?rev=1883798&view=rev
Log:
PDFBOX-5021: refactor

Modified:
    pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/pagepane/DebugTextOverlay.java

Modified: pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/pagepane/DebugTextOverlay.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/pagepane/DebugTextOverlay.java?rev=1883798&r1=1883797&r2=1883798&view=diff
==============================================================================
--- pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/pagepane/DebugTextOverlay.java (original)
+++ pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/pagepane/DebugTextOverlay.java Tue Nov 24 19:08:32 2020
@@ -206,8 +206,35 @@ final class DebugTextOverlay
             }
 
             AffineTransform at = textRenderingMatrix.createAffineTransform();
-            at.concatenate(font.getFontMatrix().createAffineTransform());
+            Shape bbox = calculateGlyphBounds(at, font, code, displacement);
+            if (bbox == null)
+            {
+                return;
+            }
 
+            Shape transformedBBox = flip.createTransformedShape(bbox);
+
+            // save
+            Color color = graphics.getColor();
+            Stroke stroke = graphics.getStroke();
+            Shape clip = graphics.getClip();
+
+            // draw
+            graphics.setClip(graphics.getDeviceConfiguration().getBounds());
+            graphics.setColor(Color.cyan);
+            graphics.setStroke(new BasicStroke(.5f));                
+            graphics.draw(transformedBBox);
+
+            // restore
+            graphics.setStroke(stroke);
+            graphics.setColor(color);
+            graphics.setClip(clip);
+        }
+
+        private Shape calculateGlyphBounds(
+                AffineTransform at, PDFont font, int code,Vector displacement) throws IOException
+        {
+            at.concatenate(font.getFontMatrix().createAffineTransform());
             // compute glyph path
             GeneralPath path;
             if (font instanceof PDType3Font)
@@ -219,14 +246,14 @@ final class DebugTextOverlay
                 PDType3CharProc charProc = t3Font.getCharProc(code);
                 if (charProc == null)
                 {
-                    return;
+                    return null;
                 }
 
                 BoundingBox fontBBox = t3Font.getBoundingBox();
                 PDRectangle glyphBBox = charProc.getGlyphBBox();
                 if (glyphBBox == null)
                 {
-                    return;
+                    return null;
                 }
 
                 // PDFBOX-3850: glyph bbox could be larger than the font bbox
@@ -243,7 +270,7 @@ final class DebugTextOverlay
 
                 if (path == null)
                 {
-                    return;
+                    return null;
                 }
 
                 // stretch non-embedded glyph if it does not match the width contained in the PDF
@@ -251,33 +278,15 @@ final class DebugTextOverlay
                 {
                     float fontWidth = font.getWidthFromFont(code);
                     if (fontWidth > 0 && // ignore spaces
-                        Math.abs(fontWidth - displacement.getX() * 1000) > 0.0001)
+                            Math.abs(fontWidth - displacement.getX() * 1000) > 0.0001)
                     {
                         float pdfWidth = displacement.getX() * 1000;
                         at.scale(pdfWidth / fontWidth, 1);
                     }
                 }
             }
-
             // compute visual bounds
-            Shape bbox = at.createTransformedShape(path.getBounds2D());
-            Shape transformedBBox = flip.createTransformedShape(bbox);
-
-            // save
-            Color color = graphics.getColor();
-            Stroke stroke = graphics.getStroke();
-            Shape clip = graphics.getClip();
-
-            // draw
-            graphics.setClip(graphics.getDeviceConfiguration().getBounds());
-            graphics.setColor(Color.cyan);
-            graphics.setStroke(new BasicStroke(.5f));                
-            graphics.draw(transformedBBox);
-
-            // restore
-            graphics.setStroke(stroke);
-            graphics.setColor(color);
-            graphics.setClip(clip);
+            return at.createTransformedShape(path.getBounds2D());
         }
     }