Search in sources :

Example 1 with BezierCurve

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

the class PdfCleanUpFilter method approximateCircle.

/**
 * Approximate a circle with 4 Bezier curves (one for each 90 degrees sector).
 *
 * @param center center of the circle
 * @param radius radius of the circle
 */
private static BezierCurve[] approximateCircle(Point center, double radius) {
    // The circle is split into 4 sectors. Arc of each sector
    // is approximated  with bezier curve separately.
    BezierCurve[] approximation = new BezierCurve[4];
    double x = center.getX();
    double y = center.getY();
    approximation[0] = new BezierCurve(Arrays.asList(new Point(x, y + radius), new Point(x + radius * CIRCLE_APPROXIMATION_CONST, y + radius), new Point(x + radius, y + radius * CIRCLE_APPROXIMATION_CONST), new Point(x + radius, y)));
    approximation[1] = new BezierCurve(Arrays.asList(new Point(x + radius, y), new Point(x + radius, y - radius * CIRCLE_APPROXIMATION_CONST), new Point(x + radius * CIRCLE_APPROXIMATION_CONST, y - radius), new Point(x, y - radius)));
    approximation[2] = new BezierCurve(Arrays.asList(new Point(x, y - radius), new Point(x - radius * CIRCLE_APPROXIMATION_CONST, y - radius), new Point(x - radius, y - radius * CIRCLE_APPROXIMATION_CONST), new Point(x - radius, y)));
    approximation[3] = new BezierCurve(Arrays.asList(new Point(x - radius, y), new Point(x - radius, y + radius * CIRCLE_APPROXIMATION_CONST), new Point(x - radius * CIRCLE_APPROXIMATION_CONST, y + radius), new Point(x, y + radius)));
    return approximation;
}
Also used : Point(com.itextpdf.kernel.geom.Point) BezierCurve(com.itextpdf.kernel.geom.BezierCurve)

Example 2 with BezierCurve

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

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

Aggregations

BezierCurve (com.itextpdf.kernel.geom.BezierCurve)3 Point (com.itextpdf.kernel.geom.Point)2 Subpath (com.itextpdf.kernel.geom.Subpath)2 IShape (com.itextpdf.kernel.geom.IShape)1 PdfCanvas (com.itextpdf.kernel.pdf.canvas.PdfCanvas)1 ArrayList (java.util.ArrayList)1