Skip to content

Commit

Permalink
For #185 #205 #213 - Correct link annotation placement.
Browse files Browse the repository at this point in the history
Also fix a NPE in BlockBox related to recent work on replaced elements.
  • Loading branch information
danfickle committed Apr 22, 2018
1 parent b941752 commit 1ffd271
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1518,7 +1518,7 @@ public void calcMinMaxWidth(LayoutContext c) {
int width = getCSSWidth(c, true);

if (width == -1) {
if (isReplaced()) {
if (isReplaced() && getReplacedElement() != null) {
width = getReplacedElement().getIntrinsicWidth();
} else {
int height = getCSSHeight(c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,7 @@ private Rectangle2D calcTotalLinkArea(RenderingContext c, Box box, float pageHei
}

private Rectangle2D add(Rectangle2D r1, Rectangle2D r2) {
float llx = (float) Math.min(r1.getMinX(), r2.getMinX());
float urx = (float) Math.max(r1.getMaxX(), r2.getMaxX());
float lly = (float) Math.min(r1.getMaxY(), r2.getMaxY());
float ury = (float) Math.max(r1.getMinY(), r2.getMinY());

return new Rectangle2D.Float(llx, lly, urx, ury);
return r1.createUnion(r2);
}

private String createRectKey(Rectangle2D rect, Shape linkShape, AffineTransform transform) {
Expand Down Expand Up @@ -366,14 +361,14 @@ private PDPageXYZDestination createDestination(RenderingContext c, Box box) {
public static Rectangle2D createTargetArea(RenderingContext c, Box box, float pageHeight, AffineTransform transform,
Box _root, PdfBoxOutputDevice _od) {
Rectangle bounds = box.getContentAreaEdge(box.getAbsX(), box.getAbsY(), c);
PageBox page = _root.getLayer().getPage(c, bounds.y);

float bottom = _od.getDeviceLength(
page.getBottom() - (bounds.y + bounds.height) + page.getMarginBorderPadding(c, CalculatedStyle.BOTTOM));
float left = _od.getDeviceLength(page.getMarginBorderPadding(c, CalculatedStyle.LEFT) + bounds.x);

return new Rectangle2D.Float(left, bottom, _od.getDeviceLength(bounds.width),
_od.getDeviceLength(bounds.height));

Point2D pt = new Point2D.Float(bounds.x, (float) bounds.getMaxY());
Point2D ptTransformed = transform.transform(pt, null);

return new Rectangle2D.Float((float) ptTransformed.getX(),
_od.normalizeY((float) ptTransformed.getY(), pageHeight),
_od.getDeviceLength(bounds.width),
_od.getDeviceLength(bounds.height));
}

public static class LinkDetails {
Expand All @@ -387,14 +382,20 @@ public static class LinkDetails {

public void processLinkLater(RenderingContext c, Box box, PDPage page, float pageHeight,
AffineTransform transform) {

if ((box instanceof BlockBox &&
((BlockBox) box).getReplacedElement() != null) ||
(box.getElement() != null && box.getElement().getNodeName().equals("a"))) {

LinkDetails link = new LinkDetails();
link.c = c;
link.box = box;
link.page = page;
link.pageHeight = pageHeight;
link.transform = transform;
link.transform = (AffineTransform) transform.clone();

_links.add(link);
}
}

public void processLinks() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,11 +767,18 @@ private void followPath(Shape s, int drawType) {
}

/**
* Converts a top down unit to a bottom up PDF unit.
* Converts a top down unit to a bottom up PDF unit for the current page.
*/
private float normalizeY(float y) {
return _pageHeight - y;
}

/**
* Converts a top down unit to a bottom up PDF unit for the specified page height.
*/
public float normalizeY(float y, float pageHeight) {
return pageHeight - y;
}

private void normalizeY(float[] coords) {
coords[1] = normalizeY(coords[1]);
Expand Down

0 comments on commit 1ffd271

Please sign in to comment.