Skip to content

Commit

Permalink
danfickle#477: Reduce duplication of the mapShapeToQuadPoints method
Browse files Browse the repository at this point in the history
Yes, those classes are copies, so there is duplication. But before extending this method
I rather have it only in one place.
  • Loading branch information
rototor committed May 14, 2020
1 parent 55a3fc8 commit 72c8548
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ private boolean placeAnnotation(AffineTransform transform, Shape linkShape, Rect
return true;
}

private float[] mapShapeToQuadPoints(AffineTransform transform, Shape linkShape, Rectangle2D targetArea) {
static float[] mapShapeToQuadPoints(AffineTransform transform, Shape linkShape, Rectangle2D targetArea) {
List<Point2D.Float> points = new ArrayList<Point2D.Float>();
AffineTransform transformForQuads = new AffineTransform();
transformForQuads.translate(targetArea.getMinX(), targetArea.getMinY());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.util.List;
import java.util.Map.Entry;

import static com.openhtmltopdf.pdfboxout.PdfBoxFastLinkManager.mapShapeToQuadPoints;

/**
* @deprecated Use fast link manager instead.
*/
Expand Down Expand Up @@ -264,71 +266,6 @@ private boolean placeAnnotation(AffineTransform transform, Shape linkShape, Rect
return true;
}

private float[] mapShapeToQuadPoints(AffineTransform transform, Shape linkShape, Rectangle2D targetArea) {
List<Point2D.Float> points = new ArrayList<Point2D.Float>();
AffineTransform transformForQuads = new AffineTransform();
transformForQuads.translate(targetArea.getMinX(), targetArea.getMinY());
// We must flip the whole thing upside down
transformForQuads.translate(0, targetArea.getHeight());
transformForQuads.scale(1, -1);
transformForQuads.concatenate(AffineTransform.getScaleInstance(transform.getScaleX(), transform.getScaleX()));
Area area = new Area(linkShape);
PathIterator pathIterator = area.getPathIterator(transformForQuads, 1.0);
double[] vals = new double[6];
while (!pathIterator.isDone()) {
int type = pathIterator.currentSegment(vals);
switch (type) {
case PathIterator.SEG_CUBICTO:
throw new RuntimeException("Invalid State, Area should never give us a curve here!");
case PathIterator.SEG_LINETO:
points.add(new Point2D.Float((float) vals[0], (float) vals[1]));
break;
case PathIterator.SEG_MOVETO:
points.add(new Point2D.Float((float) vals[0], (float) vals[1]));
break;
case PathIterator.SEG_QUADTO:
throw new RuntimeException("Invalid State, Area should never give us a curve here!");
case PathIterator.SEG_CLOSE:
break;
default:
break;
}
pathIterator.next();
}

removeDoublicatePoints(points);

KongAlgo algo = new KongAlgo(points);
algo.runKong();

float ret[] = new float[algo.getTriangles().size() * 8];
int i = 0;
for (Triangle triangle : algo.getTriangles()) {
ret[i++] = triangle.a.x;
ret[i++] = triangle.a.y;
ret[i++] = triangle.b.x;
ret[i++] = triangle.b.y;
/*
* To get a quad we add the point between b and c
*/
ret[i++] = triangle.b.x + (triangle.c.x - triangle.b.x) / 2;
ret[i++] = triangle.b.y + (triangle.c.y - triangle.b.y) / 2;

ret[i++] = triangle.c.x;
ret[i++] = triangle.c.y;
}

if (ret.length % 8 != 0)
throw new IllegalStateException("Not exact 8xn QuadPoints!");
for (; i < ret.length; i += 2) {
if (ret[i] < targetArea.getMinX() || ret[i] > targetArea.getMaxX())
throw new IllegalStateException("Invalid rectangle calculation. Map shape is out of bound.");
if (ret[i + 1] < targetArea.getMinY() || ret[i + 1] > targetArea.getMaxY())
throw new IllegalStateException("Invalid rectangle calculation. Map shape is out of bound.");
}
return ret;
}

private void addLinkToPage(PDPage page, PDAnnotationLink annot) {
PDBorderStyleDictionary styleDict = new PDBorderStyleDictionary();
styleDict.setWidth(0);
Expand Down

0 comments on commit 72c8548

Please sign in to comment.