Skip to content

Links with bounds outside the page appear in the wrong place #12406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nvinen opened this issue Sep 23, 2020 · 3 comments
Closed

Links with bounds outside the page appear in the wrong place #12406

nvinen opened this issue Sep 23, 2020 · 3 comments

Comments

@nvinen
Copy link

nvinen commented Sep 23, 2020

I'm using the current beta version (pdfjs-2.6.347-dist).

I traced the bug to incorrect code for negating numbers by pasting a hyphen on the front of the string, rather than actually negating the numbers. Pasting a hyphen in front of a negative number like -3 gives you --3 instead of 3.

It's certainly possible for link (and other annotation) bounds can be negative (ie, beyond the top/left edge of the page). The current pdf.js code offsets such regions significantly from where they should be. The following patch fixes this bug and a related bug, and also adds a few lines to clip the regions so that they are within the page so they look neater.

--- pdf.js (revision 1225)
+++ pdf.js (working copy)
@@ -9374,14 +9374,18 @@
page = this.page,
viewport = this.viewport;
const container = document.createElement("section");

  • let width = data.rect[2] - data.rect[0];

  • let height = data.rect[3] - data.rect[1];
    container.setAttribute("data-annotation-id", data.id);

    const rect = _util.Util.normalizeRect([data.rect[0], page.view[3] - data.rect[1] + page.view[1], data.rect[2], page.view[3] - data.rect[3] + page.view[1]]);

  • if( rect[0] < page.view[0] ) rect[0] = page.view[0]; else if( rect[0] > page.view[2] ) rect[0] = page.view[2];

  • if( rect[1] < page.view[1] ) rect[1] = page.view[1]; else if( rect[1] > page.view[3] ) rect[1] = page.view[3];

  • if( rect[2] < page.view[0] ) rect[2] = page.view[0]; else if( rect[2] > page.view[2] ) rect[2] = page.view[2];

  • if( rect[3] < page.view[1] ) rect[3] = page.view[1]; else if( rect[3] > page.view[3] ) rect[3] = page.view[3];

  • let width = rect[2] - rect[0];

  • let height = rect[3] - rect[1];

    container.style.transform = matrix(${viewport.transform.join(",")});

  • container.style.transformOrigin = -${rect[0]}px -${rect[1]}px;
  • container.style.transformOrigin = ${-rect[0]}px ${-rect[1]}px;

    if (!ignoreBorder && data.borderStyle.width > 0) {
    container.style.borderWidth = ${data.borderStyle.width}px;
    @@ -9801,7 +9805,7 @@
    });
    const parentLeft = parseFloat(parentElement.style.left);
    const parentWidth = parseFloat(parentElement.style.width);

  • this.container.style.transformOrigin = -${parentLeft + parentWidth}px -${parentElement.style.top};
  • this.container.style.transformOrigin = ${-(parentLeft + parentWidth)}px ${-parentElement.style.top};
    this.container.style.left = ${parentLeft + parentWidth}px;
    this.container.appendChild(popup.render());
    return this.container;
@Snuffleupagus
Copy link
Collaborator

Snuffleupagus commented Sep 23, 2020

When opening an issue, please remember to provide all of the information requested in https://github.com/mozilla/pdf.js/blob/master/.github/ISSUE_TEMPLATE.md in order for the issue to be actionable/valid.

Please also note that any patches to this code would need to be accompanied by tests, see https://github.com/mozilla/pdf.js/wiki/Contributing and in particular https://github.com/mozilla/pdf.js/wiki/Contributing#4-run-lint-and-testing

The following patch fixes this bug and a related bug, and also adds a few lines to clip the regions so that they are within the page so they look neater.

Finally, also keep in mind that it's always best to have non-related changes in separate commits (although they can obviously be in the same PR).

@Snuffleupagus
Copy link
Collaborator

@nvinen Do you want to attempt to submit a PR for this?

@timvandermeij
Copy link
Contributor

Fixed by the PR above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants