Example usage for com.itextpdf.text.pdf.parser Path getSubpaths

List of usage examples for com.itextpdf.text.pdf.parser Path getSubpaths

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf.parser Path getSubpaths.

Prototype

public List<Subpath> getSubpaths() 

Source Link

Usage

From source file:mkl.testarea.itext5.pdfcleanup.PdfCleanUpContentOperator.java

License:Open Source License

private void writePath(Path path, byte[] pathPaintingOperator, PdfContentByte canvas) throws IOException {
    if (path.isEmpty()) {
        return;/*ww  w.  j  av  a  2 s .c  o m*/
    }

    for (Subpath subpath : path.getSubpaths()) {
        writeMoveTo(subpath.getStartPoint(), canvas);

        for (Shape segment : subpath.getSegments()) {
            if (segment instanceof BezierCurve) {
                writeBezierCurve((BezierCurve) segment, canvas);
            } else {
                writeLine((Line) segment, canvas);
            }
        }

        if (subpath.isClosed()) {
            canvas.getInternalBuffer().append(h);
        }
    }

    if (pathPaintingOperator != null) {
        canvas.getInternalBuffer().append(pathPaintingOperator);
    }
}

From source file:mkl.testarea.itext5.pdfcleanup.PdfCleanUpRegionFilter.java

License:Open Source License

private static List<Point2D> getPathApproximation(Path path) {
    List<Point2D> approx = new ArrayList<Point2D>() {
        @Override/*ww  w.  j  a v a  2 s.  com*/
        public boolean addAll(Collection<? extends Point2D> c) {
            Point2D prevPoint = (size() - 1 < 0 ? null : get(size() - 1));
            boolean ret = false;

            for (Point2D pt : c) {
                if (!pt.equals(prevPoint)) {
                    add(pt);
                    prevPoint = pt;
                    ret = true;
                }
            }

            return true;
        }
    };

    for (Subpath subpath : path.getSubpaths()) {
        approx.addAll(subpath.getPiecewiseLinearApproximation());
    }

    return approx;
}

From source file:mkl.testarea.itext5.pdfcleanup.PdfCleanUpRegionFilter.java

License:Open Source License

/**
 * Adds all subpaths of the path to the {@link ClipperOffset} object with one
 * note: it doesn't add degenerate subpaths.
 *
 * @return {@link java.util.List} consisting of all degenerate subpaths of the path.
 *//*  w ww . j  a v a 2s  .co m*/
private static List<Subpath> addPath(ClipperOffset offset, Path path, JoinType joinType, EndType endType) {
    List<Subpath> degenerateSubpaths = new ArrayList<Subpath>();

    for (Subpath subpath : path.getSubpaths()) {
        if (subpath.isDegenerate()) {
            degenerateSubpaths.add(subpath);
            continue;
        }

        if (!subpath.isSinglePointClosed() && !subpath.isSinglePointOpen()) {
            EndType et;

            if (subpath.isClosed()) {
                // Offsetting is never used for path being filled
                et = EndType.CLOSED_LINE;
            } else {
                et = endType;
            }

            List<Point2D> linearApproxPoints = subpath.getPiecewiseLinearApproximation();
            offset.addPath(convertToIntPoints(linearApproxPoints), joinType, et);
        }
    }

    return degenerateSubpaths;
}

From source file:mkl.testarea.itext5.pdfcleanup.PdfCleanUpRegionFilter.java

License:Open Source License

private static void addPath(Clipper clipper, Path path) {
    for (Subpath subpath : path.getSubpaths()) {
        if (!subpath.isSinglePointClosed() && !subpath.isSinglePointOpen()) {
            List<Point2D> linearApproxPoints = subpath.getPiecewiseLinearApproximation();
            clipper.addPath(convertToIntPoints(linearApproxPoints), PolyType.SUBJECT, subpath.isClosed());
        }/*from  w ww.  j a v  a2s  .  c  om*/
    }
}

From source file:mkl.testarea.itext5.pdfcleanup.PdfCleanUpRegionFilter.java

License:Open Source License

private static Path applyDashPattern(Path path, LineDashPattern lineDashPattern) {
    Set<Integer> modifiedSubpaths = new HashSet<Integer>(path.replaceCloseWithLine());
    Path dashedPath = new Path();
    int currentSubpath = 0;

    for (Subpath subpath : path.getSubpaths()) {
        List<Point2D> 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) {
                Point2D 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);
                }/*from   ww w  . j  a  v a 2  s . c  om*/

                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();
                }
            }

            // If true, then the line closing the subpath was explicitly added (see Path.ReplaceCloseWithLine).
            // This causes a loss of a visual effect of line join style parameter, so in this clause
            // we simply add overlapping dash (or gap, no matter), which continues the last dash and equals to
            // the first dash (or gap) of the path.
            if (modifiedSubpaths.contains(currentSubpath)) {
                lineDashPattern.reset();
                LineDashPattern.DashArrayElem currentElem = lineDashPattern.next();
                Point2D 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;
}