Search in sources :

Example 6 with Subpath

use of com.itextpdf.kernel.geom.Subpath in project i7j-pdfsweep by itext.

the class PdfCleanUpFilter method convertToSquares.

/**
 * Converts specified degenerate subpaths to squares.
 * Note: the list of degenerate subpaths should contain at least 2 elements. Otherwise
 * we can't determine the direction which the rotation of each square depends on.
 *
 * @param squareWidth Width of each constructed square.
 * @param sourcePath  The path which dash pattern applied to. Needed to calc rotation angle of each square.
 * @return {@link java.util.List} consisting of squares constructed on given degenerated subpaths.
 */
private static List<Subpath> convertToSquares(List<Subpath> degenerateSubpaths, double squareWidth, com.itextpdf.kernel.geom.Path sourcePath) {
    List<Point> pathApprox = getPathApproximation(sourcePath);
    if (pathApprox.size() < 2) {
        return Collections.<Subpath>emptyList();
    }
    Iterator<Point> approxIter = pathApprox.iterator();
    Point approxPt1 = approxIter.next();
    Point approxPt2 = approxIter.next();
    StandardLine line = new StandardLine(approxPt1, approxPt2);
    List<Subpath> squares = new ArrayList<>(degenerateSubpaths.size());
    float widthHalf = (float) squareWidth / 2;
    for (Subpath subpath : degenerateSubpaths) {
        Point point = subpath.getStartPoint();
        while (!line.contains(point)) {
            approxPt1 = approxPt2;
            approxPt2 = approxIter.next();
            line = new StandardLine(approxPt1, approxPt2);
        }
        double slope = line.getSlope();
        double angle;
        if (slope != Float.POSITIVE_INFINITY) {
            angle = Math.atan(slope);
        } else {
            angle = Math.PI / 2;
        }
        squares.add(constructSquare(point, widthHalf, angle));
    }
    return squares;
}
Also used : Subpath(com.itextpdf.kernel.geom.Subpath) ArrayList(java.util.ArrayList) Point(com.itextpdf.kernel.geom.Point)

Aggregations

Subpath (com.itextpdf.kernel.geom.Subpath)6 Point (com.itextpdf.kernel.geom.Point)4 ArrayList (java.util.ArrayList)3 BezierCurve (com.itextpdf.kernel.geom.BezierCurve)2 IShape (com.itextpdf.kernel.geom.IShape)1 Line (com.itextpdf.kernel.geom.Line)1 Path (com.itextpdf.kernel.geom.Path)1 PdfCanvas (com.itextpdf.kernel.pdf.canvas.PdfCanvas)1 ClipperOffset (com.itextpdf.kernel.pdf.canvas.parser.clipper.ClipperOffset)1 EndType (com.itextpdf.kernel.pdf.canvas.parser.clipper.IClipper.EndType)1 JoinType (com.itextpdf.kernel.pdf.canvas.parser.clipper.IClipper.JoinType)1 PolyTree (com.itextpdf.kernel.pdf.canvas.parser.clipper.PolyTree)1 HashSet (java.util.HashSet)1 List (java.util.List)1