Search in sources :

Example 6 with Point

use of com.itextpdf.kernel.geom.Point 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 7 with Point

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

the class LineDashPattern method getNextPoint.

private static Point getNextPoint(Point segStart, Point segEnd, float dist) {
    Point vector = componentwiseDiff(segEnd, segStart);
    Point unitVector = getUnitVector(vector);
    return new Point(segStart.getX() + dist * unitVector.getX(), segStart.getY() + dist * unitVector.getY());
}
Also used : Point(com.itextpdf.kernel.geom.Point)

Example 8 with Point

use of com.itextpdf.kernel.geom.Point 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 9 with Point

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

the class PdfCleanUpFilter method getTextRectangle.

/**
 * Get the bounding box of a TextRenderInfo object.
 *
 * @param renderInfo input TextRenderInfo object
 */
private static Point[] getTextRectangle(TextRenderInfo renderInfo) {
    LineSegment ascent = renderInfo.getAscentLine();
    LineSegment descent = renderInfo.getDescentLine();
    return new Point[] { new Point(ascent.getStartPoint().get(0), ascent.getStartPoint().get(1)), new Point(ascent.getEndPoint().get(0), ascent.getEndPoint().get(1)), new Point(descent.getEndPoint().get(0), descent.getEndPoint().get(1)), new Point(descent.getStartPoint().get(0), descent.getStartPoint().get(1)) };
}
Also used : Point(com.itextpdf.kernel.geom.Point) LineSegment(com.itextpdf.kernel.geom.LineSegment)

Example 10 with Point

use of com.itextpdf.kernel.geom.Point 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

Point (com.itextpdf.kernel.geom.Point)10 Subpath (com.itextpdf.kernel.geom.Subpath)4 PdfException (com.itextpdf.kernel.exceptions.PdfException)2 BezierCurve (com.itextpdf.kernel.geom.BezierCurve)2 NoninvertibleTransformException (com.itextpdf.kernel.geom.NoninvertibleTransformException)2 DefaultClipper (com.itextpdf.kernel.pdf.canvas.parser.clipper.DefaultClipper)2 IClipper (com.itextpdf.kernel.pdf.canvas.parser.clipper.IClipper)2 PolyTree (com.itextpdf.kernel.pdf.canvas.parser.clipper.PolyTree)2 ArrayList (java.util.ArrayList)2 AffineTransform (com.itextpdf.kernel.geom.AffineTransform)1 IShape (com.itextpdf.kernel.geom.IShape)1 Line (com.itextpdf.kernel.geom.Line)1 LineSegment (com.itextpdf.kernel.geom.LineSegment)1 Path (com.itextpdf.kernel.geom.Path)1 Rectangle (com.itextpdf.kernel.geom.Rectangle)1 PdfCanvas (com.itextpdf.kernel.pdf.canvas.PdfCanvas)1 PolyFillType (com.itextpdf.kernel.pdf.canvas.parser.clipper.IClipper.PolyFillType)1 Paths (com.itextpdf.kernel.pdf.canvas.parser.clipper.Paths)1 HashSet (java.util.HashSet)1