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