Skip to content

Commit 1ffd271

Browse files
committed
For danfickle#185 danfickle#205 danfickle#213 - Correct link annotation placement.
Also fix a NPE in BlockBox related to recent work on replaced elements.
1 parent b941752 commit 1ffd271

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

openhtmltopdf-core/src/main/java/com/openhtmltopdf/render/BlockBox.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1518,7 +1518,7 @@ public void calcMinMaxWidth(LayoutContext c) {
15181518
int width = getCSSWidth(c, true);
15191519

15201520
if (width == -1) {
1521-
if (isReplaced()) {
1521+
if (isReplaced() && getReplacedElement() != null) {
15221522
width = getReplacedElement().getIntrinsicWidth();
15231523
} else {
15241524
int height = getCSSHeight(c);

openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxLinkManager.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,7 @@ private Rectangle2D calcTotalLinkArea(RenderingContext c, Box box, float pageHei
7878
}
7979

8080
private Rectangle2D add(Rectangle2D r1, Rectangle2D r2) {
81-
float llx = (float) Math.min(r1.getMinX(), r2.getMinX());
82-
float urx = (float) Math.max(r1.getMaxX(), r2.getMaxX());
83-
float lly = (float) Math.min(r1.getMaxY(), r2.getMaxY());
84-
float ury = (float) Math.max(r1.getMinY(), r2.getMinY());
85-
86-
return new Rectangle2D.Float(llx, lly, urx, ury);
81+
return r1.createUnion(r2);
8782
}
8883

8984
private String createRectKey(Rectangle2D rect, Shape linkShape, AffineTransform transform) {
@@ -366,14 +361,14 @@ private PDPageXYZDestination createDestination(RenderingContext c, Box box) {
366361
public static Rectangle2D createTargetArea(RenderingContext c, Box box, float pageHeight, AffineTransform transform,
367362
Box _root, PdfBoxOutputDevice _od) {
368363
Rectangle bounds = box.getContentAreaEdge(box.getAbsX(), box.getAbsY(), c);
369-
PageBox page = _root.getLayer().getPage(c, bounds.y);
370-
371-
float bottom = _od.getDeviceLength(
372-
page.getBottom() - (bounds.y + bounds.height) + page.getMarginBorderPadding(c, CalculatedStyle.BOTTOM));
373-
float left = _od.getDeviceLength(page.getMarginBorderPadding(c, CalculatedStyle.LEFT) + bounds.x);
374-
375-
return new Rectangle2D.Float(left, bottom, _od.getDeviceLength(bounds.width),
376-
_od.getDeviceLength(bounds.height));
364+
365+
Point2D pt = new Point2D.Float(bounds.x, (float) bounds.getMaxY());
366+
Point2D ptTransformed = transform.transform(pt, null);
367+
368+
return new Rectangle2D.Float((float) ptTransformed.getX(),
369+
_od.normalizeY((float) ptTransformed.getY(), pageHeight),
370+
_od.getDeviceLength(bounds.width),
371+
_od.getDeviceLength(bounds.height));
377372
}
378373

379374
public static class LinkDetails {
@@ -387,14 +382,20 @@ public static class LinkDetails {
387382

388383
public void processLinkLater(RenderingContext c, Box box, PDPage page, float pageHeight,
389384
AffineTransform transform) {
385+
386+
if ((box instanceof BlockBox &&
387+
((BlockBox) box).getReplacedElement() != null) ||
388+
(box.getElement() != null && box.getElement().getNodeName().equals("a"))) {
389+
390390
LinkDetails link = new LinkDetails();
391391
link.c = c;
392392
link.box = box;
393393
link.page = page;
394394
link.pageHeight = pageHeight;
395-
link.transform = transform;
395+
link.transform = (AffineTransform) transform.clone();
396396

397397
_links.add(link);
398+
}
398399
}
399400

400401
public void processLinks() {

openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxOutputDevice.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,11 +767,18 @@ private void followPath(Shape s, int drawType) {
767767
}
768768

769769
/**
770-
* Converts a top down unit to a bottom up PDF unit.
770+
* Converts a top down unit to a bottom up PDF unit for the current page.
771771
*/
772772
private float normalizeY(float y) {
773773
return _pageHeight - y;
774774
}
775+
776+
/**
777+
* Converts a top down unit to a bottom up PDF unit for the specified page height.
778+
*/
779+
public float normalizeY(float y, float pageHeight) {
780+
return pageHeight - y;
781+
}
775782

776783
private void normalizeY(float[] coords) {
777784
coords[1] = normalizeY(coords[1]);

0 commit comments

Comments
 (0)