Guest User

LinePa

a guest
Jun 23rd, 2016
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.09 KB | None | 0 0
  1. import org.apache.pdfbox.contentstream.PDFGraphicsStreamEngine;
  2. import org.apache.pdfbox.cos.COSName;
  3. import org.apache.pdfbox.pdmodel.PDPage;
  4. import org.apache.pdfbox.pdmodel.graphics.image.PDImage;
  5.  
  6. import java.awt.geom.Point2D;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.Iterator;
  10. import java.util.List;
  11.  
  12. public class LinePathFinder extends PDFGraphicsStreamEngine implements Iterable<Path>
  13. {
  14.     public int index=0;
  15.  
  16.     public LinePathFinder(PDPage page)
  17.     {
  18.         super(page);
  19.     }
  20.  
  21.     //
  22.     // PDFGraphicsStreamEngine overrides
  23.     //
  24.     public void findLinePaths() throws IOException
  25.     {
  26.         processPage(getPage());
  27.     }
  28.  
  29.     @Override
  30.     public void appendRectangle(Point2D p0, Point2D p1, Point2D p2, Point2D p3) throws IOException
  31.     {
  32.         index+=1;
  33.         startPathIfNecessary();
  34.         currentPath.appendRectangle(toFloat(p0), toFloat(p1), toFloat(p2), toFloat(p3),getGraphicsState().getCurrentTransformationMatrix());
  35.     }
  36.  
  37.     @Override
  38.     public void drawImage(PDImage pdImage) throws IOException { index+=1;}
  39.  
  40.     @Override
  41.     public void clip(int windingRule) throws IOException
  42.     {
  43.         index+=1;
  44.         currentPath.complete(windingRule);
  45.         //paths.add(currentPath);
  46.         //currentPath = null;
  47.  
  48.     }
  49.  
  50.     @Override
  51.     public void moveTo(float x, float y) throws IOException
  52.     {
  53.         index+=1;
  54.         startPathIfNecessary();
  55.         currentPath.moveTo(x, y);
  56.     }
  57.  
  58.     @Override
  59.     public void lineTo(float x, float y) throws IOException
  60.     {
  61.         index+=1;
  62.         currentPath.lineTo(x, y, getGraphicsState().getCurrentTransformationMatrix());
  63.     }
  64.  
  65.     @Override
  66.     public void curveTo(float x1, float y1, float x2, float y2, float x3, float y3) throws IOException
  67.     {
  68.         System.out.println("[index]: "+index);
  69.         System.out.println(getGraphicsState().getCurrentTransformationMatrix());
  70.         index+=1;
  71.         currentPath.curveTo(x1, y1, x2, y2, x3, y3,  getGraphicsState().getCurrentTransformationMatrix());
  72.     }
  73.  
  74.     @Override
  75.     public Point2D.Float getCurrentPoint() throws IOException
  76.     {
  77.         index+=1;
  78.         return currentPath.getCurrentPoint();
  79.     }
  80.  
  81.     @Override
  82.     public void closePath() throws IOException
  83.     {
  84.         index+=1;
  85.         currentPath.closePath(getGraphicsState().getCurrentTransformationMatrix());
  86.     }
  87.  
  88.     @Override
  89.     public void endPath() throws IOException
  90.     {
  91.         //paths.add(currentPath); //this has no effect because `n` always follows `W`
  92.         // or `W*`. We are setting currentPath to null in those cases anyway.
  93.         index+=1;
  94.         currentPath = null;
  95.     }
  96.  
  97.     @Override
  98.     public void strokePath() throws IOException
  99.     {
  100.         index+=1;
  101.         currentPath.complete(-2);
  102.         paths.add(currentPath);
  103.         currentPath = null;
  104.     }
  105.  
  106.     @Override
  107.     public void fillPath(int windingRule) throws IOException
  108.     {
  109.         index+=1;
  110.         currentPath.complete(-2);
  111.         paths.add(currentPath);
  112.         currentPath = null;
  113.     }
  114.  
  115.     @Override
  116.     public void fillAndStrokePath(int windingRule) throws IOException
  117.     {
  118.         index+=1;
  119.         currentPath.complete(-2);
  120.         paths.add(currentPath);
  121.         currentPath = null;
  122.     }
  123.  
  124.     @Override
  125.     public void shadingFill(COSName shadingName) throws IOException
  126.     {
  127.         index+=1;
  128.         currentPath.complete(-2);
  129.         paths.add(currentPath);
  130.         currentPath = null;
  131.     }
  132.  
  133.     void startPathIfNecessary()
  134.     {
  135.         index+=1;
  136.         if (currentPath == null)
  137.             currentPath = new Path();
  138.     }
  139.  
  140.     Point2D.Float toFloat(Point2D p)
  141.     {
  142.         if (p == null || (p instanceof Point2D.Float))
  143.         {
  144.             return (Point2D.Float)p;
  145.         }
  146.         return new Point2D.Float((float)p.getX(), (float)p.getY());
  147.     }
  148.  
  149.     //
  150.     // Iterable<Path> implementation
  151.     //
  152.     public Iterator<Path> iterator()
  153.     {
  154.         return paths.iterator();
  155.     }
  156.  
  157.     Path currentPath = null;
  158.     final List<Path> paths = new ArrayList<Path>();
  159. }
Add Comment
Please, Sign In to add comment