Search in sources :

Example 1 with Subpath

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

the class PdfCleanUpFilter method constructSquare.

private static Subpath constructSquare(Point squareCenter, double widthHalf, double rotationAngle) {
    // Orthogonal square is the square with sides parallel to one of the axes.
    Point[] ortogonalSquareVertices = { new Point(-widthHalf, -widthHalf), new Point(-widthHalf, widthHalf), new Point(widthHalf, widthHalf), new Point(widthHalf, -widthHalf) };
    Point[] rotatedSquareVertices = getRotatedSquareVertices(ortogonalSquareVertices, rotationAngle, squareCenter);
    Subpath square = new Subpath();
    square.addSegment(new Line(rotatedSquareVertices[0], rotatedSquareVertices[1]));
    square.addSegment(new Line(rotatedSquareVertices[1], rotatedSquareVertices[2]));
    square.addSegment(new Line(rotatedSquareVertices[2], rotatedSquareVertices[3]));
    square.addSegment(new Line(rotatedSquareVertices[3], rotatedSquareVertices[0]));
    return square;
}
Also used : Subpath(com.itextpdf.kernel.geom.Subpath) Line(com.itextpdf.kernel.geom.Line) Point(com.itextpdf.kernel.geom.Point)

Example 2 with Subpath

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

the class PdfCleanUpProcessor method writePath.

private void writePath(Path path) {
    PdfCanvas canvas = getCanvas();
    for (Subpath subpath : path.getSubpaths()) {
        canvas.moveTo((float) subpath.getStartPoint().getX(), (float) subpath.getStartPoint().getY());
        for (IShape segment : subpath.getSegments()) {
            if (segment instanceof BezierCurve) {
                List<Point> basePoints = segment.getBasePoints();
                Point p2 = basePoints.get(1);
                Point p3 = basePoints.get(2);
                Point p4 = basePoints.get(3);
                canvas.curveTo((float) p2.getX(), (float) p2.getY(), (float) p3.getX(), (float) p3.getY(), (float) p4.getX(), (float) p4.getY());
            } else {
                // segment is Line
                Point destination = segment.getBasePoints().get(1);
                canvas.lineTo((float) destination.getX(), (float) destination.getY());
            }
        }
        if (subpath.isClosed()) {
            canvas.closePath();
        }
    }
}
Also used : Subpath(com.itextpdf.kernel.geom.Subpath) PdfCanvas(com.itextpdf.kernel.pdf.canvas.PdfCanvas) Point(com.itextpdf.kernel.geom.Point) IShape(com.itextpdf.kernel.geom.IShape) BezierCurve(com.itextpdf.kernel.geom.BezierCurve)

Example 3 with Subpath

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

the class LineDashPattern method applyDashPattern.

/**
 * Apply a LineDashPattern along a Path
 *
 * @param path            input path
 * @param lineDashPattern input LineDashPattern
 * @return a dashed Path
 */
public static Path applyDashPattern(Path path, LineDashPattern lineDashPattern) {
    Set<Integer> modifiedSubpaths = new HashSet<>(path.replaceCloseWithLine());
    Path dashedPath = new Path();
    int currentSubpath = 0;
    for (Subpath subpath : path.getSubpaths()) {
        List<Point> subpathApprox = subpath.getPiecewiseLinearApproximation();
        if (subpathApprox.size() > 1) {
            dashedPath.moveTo((float) subpathApprox.get(0).getX(), (float) subpathApprox.get(0).getY());
            float remainingDist = 0;
            boolean remainingIsGap = false;
            for (int i = 1; i < subpathApprox.size(); ++i) {
                Point nextPoint = null;
                if (remainingDist != 0) {
                    nextPoint = getNextPoint(subpathApprox.get(i - 1), subpathApprox.get(i), remainingDist);
                    remainingDist = applyDash(dashedPath, subpathApprox.get(i - 1), subpathApprox.get(i), nextPoint, remainingIsGap);
                }
                while (Float.compare(remainingDist, 0) == 0 && !dashedPath.getCurrentPoint().equals(subpathApprox.get(i))) {
                    LineDashPattern.DashArrayElem currentElem = lineDashPattern.next();
                    nextPoint = getNextPoint(nextPoint != null ? nextPoint : subpathApprox.get(i - 1), subpathApprox.get(i), currentElem.getVal());
                    remainingDist = applyDash(dashedPath, subpathApprox.get(i - 1), subpathApprox.get(i), nextPoint, currentElem.isGap());
                    remainingIsGap = currentElem.isGap();
                }
            }
            // the first dash (or gap) of the path.
            if (modifiedSubpaths.contains(currentSubpath)) {
                lineDashPattern.reset();
                LineDashPattern.DashArrayElem currentElem = lineDashPattern.next();
                Point nextPoint = getNextPoint(subpathApprox.get(0), subpathApprox.get(1), currentElem.getVal());
                applyDash(dashedPath, subpathApprox.get(0), subpathApprox.get(1), nextPoint, currentElem.isGap());
            }
        }
        // According to PDF spec. line dash pattern should be restarted for each new subpath.
        lineDashPattern.reset();
        ++currentSubpath;
    }
    return dashedPath;
}
Also used : Path(com.itextpdf.kernel.geom.Path) Subpath(com.itextpdf.kernel.geom.Subpath) Point(com.itextpdf.kernel.geom.Point) Point(com.itextpdf.kernel.geom.Point) HashSet(java.util.HashSet)

Example 4 with Subpath

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

the class PdfCleanUpFilter method convertToCircles.

/**
 * Converts specified degenerate subpaths to circles.
 * Note: actually the resultant subpaths are not real circles but approximated.
 *
 * @param radius Radius of each constructed circle.
 * @return {@link java.util.List} consisting of circles constructed on given degenerated subpaths.
 */
private static List<Subpath> convertToCircles(List<Subpath> degenerateSubpaths, double radius) {
    List<Subpath> circles = new ArrayList<>(degenerateSubpaths.size());
    for (Subpath subpath : degenerateSubpaths) {
        BezierCurve[] circleSectors = approximateCircle(subpath.getStartPoint(), radius);
        Subpath circle = new Subpath();
        circle.addSegment(circleSectors[0]);
        circle.addSegment(circleSectors[1]);
        circle.addSegment(circleSectors[2]);
        circle.addSegment(circleSectors[3]);
        circles.add(circle);
    }
    return circles;
}
Also used : Subpath(com.itextpdf.kernel.geom.Subpath) ArrayList(java.util.ArrayList) BezierCurve(com.itextpdf.kernel.geom.BezierCurve)

Example 5 with Subpath

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

the class PdfCleanUpFilter method filterStrokePath.

private com.itextpdf.kernel.geom.Path filterStrokePath(com.itextpdf.kernel.geom.Path sourcePath, Matrix ctm, float lineWidth, int lineCapStyle, int lineJoinStyle, float miterLimit, LineDashPattern lineDashPattern) {
    com.itextpdf.kernel.geom.Path path = sourcePath;
    JoinType joinType = ClipperBridge.getJoinType(lineJoinStyle);
    EndType endType = ClipperBridge.getEndType(lineCapStyle);
    if (lineDashPattern != null && !lineDashPattern.isSolid()) {
        path = LineDashPattern.applyDashPattern(path, lineDashPattern);
    }
    ClipperOffset offset = new ClipperOffset(miterLimit, PdfCleanUpTool.arcTolerance * PdfCleanUpTool.floatMultiplier);
    List<Subpath> degenerateSubpaths = ClipperBridge.addPath(offset, path, joinType, endType);
    PolyTree resultTree = new PolyTree();
    offset.execute(resultTree, lineWidth * PdfCleanUpTool.floatMultiplier / 2);
    com.itextpdf.kernel.geom.Path offsetedPath = ClipperBridge.convertToPath(resultTree);
    if (degenerateSubpaths.size() > 0) {
        if (endType == EndType.OPEN_ROUND) {
            List<Subpath> circles = convertToCircles(degenerateSubpaths, lineWidth / 2);
            offsetedPath.addSubpaths(circles);
        } else if (endType == EndType.OPEN_SQUARE && lineDashPattern != null) {
            List<Subpath> squares = convertToSquares(degenerateSubpaths, lineWidth, sourcePath);
            offsetedPath.addSubpaths(squares);
        }
    }
    return filterFillPath(offsetedPath, ctm, PdfCanvasConstants.FillingRule.NONZERO_WINDING);
}
Also used : Subpath(com.itextpdf.kernel.geom.Subpath) PolyTree(com.itextpdf.kernel.pdf.canvas.parser.clipper.PolyTree) ClipperOffset(com.itextpdf.kernel.pdf.canvas.parser.clipper.ClipperOffset) JoinType(com.itextpdf.kernel.pdf.canvas.parser.clipper.IClipper.JoinType) EndType(com.itextpdf.kernel.pdf.canvas.parser.clipper.IClipper.EndType) List(java.util.List) ArrayList(java.util.ArrayList)

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