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