Skip to content

Commit

Permalink
[wdspec] browsingContext.print: fix rounding error in page.py test
Browse files Browse the repository at this point in the history
[pytest](https://github.com/web-platform-tests/wpt/blob/7a087d54be8b6c0ca0181a86dc1ff0b28461c383/webdriver/tests/support/image.py)
uses:

    def cm_to_px(cm): return round(cm * 96 / 2.54)

[js](https://github.com/web-platform-tests/wpt/blob/7a087d54be8b6c0ca0181a86dc1ff0b28461c383/tools/wptrunner/wptrunner/print_pdf_runner.html)
uses:

    const viewport = page.getViewport({ scale: 96. / 72. });
    ...
    canvas.height = viewport.height;
    canvas.width = viewport.width;

This produces a rounding error, even though the dimension is correct:

    >       assert cm_to_px(expected_dimensions["height"]) == height
    E       assert 454 == 453
    E         +454
    E         -453

The inconsistency of rounding in both ends becomes clear when we
eliminate "round" in the pytest side:

    >       assert cm_to_px(expected_dimensions["height"]) == height
    E       assert 453.54330708661416 == 453
    E         +453.54330708661416
    E         -453

There are multiple ways to fix this issue.

Option #1: Use "floor" instead of "round" in pytest. This is what this
PR does.

Option #2: Use a range in the assertion comparison, allowing a
difference of up to +-1.0

The problematic part is .96 / .72 which evaluates to 4/3 = 1.333333....
  • Loading branch information
thiagowfx committed Jun 12, 2023
1 parent 0d9a5c5 commit e58b369
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
12 changes: 6 additions & 6 deletions tools/wptrunner/wptrunner/print_pdf_runner.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
let loadingTask = pdfjsLib.getDocument({data: atob(pdfData)});
let pdf = await loadingTask.promise;
let rendered = [];
for (let pageNumber=1; pageNumber<=pdf.numPages; pageNumber++) {
for (let pageNumber = 1; pageNumber <= pdf.numPages; pageNumber++) {
let page = await pdf.getPage(pageNumber);
var viewport = page.getViewport({scale: 96./72.});
const viewport = page.getViewport({ scale: 96. / 72. });
// Prepare canvas using PDF page dimensions
var canvas = document.getElementsByTagName('canvas')[0];
var context = canvas.getContext('2d');
const canvas = document.getElementsByTagName('canvas')[0];
const context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;

// Render PDF page into canvas context
var renderContext = {
const renderContext = {
canvasContext: context,
viewport: viewport
viewport
};
await page.render(renderContext).promise;
rendered.push(canvas.toDataURL());
Expand Down
3 changes: 2 additions & 1 deletion webdriver/tests/support/image.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from math import floor
import struct

from tests.support.asserts import assert_png
Expand All @@ -8,7 +9,7 @@


def cm_to_px(cm):
return round(cm * PPI / inch_in_cm)
return floor(cm * PPI / inch_in_cm)


def px_to_cm(px):
Expand Down

1 comment on commit e58b369

@community-tc-integration
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh oh! Looks like an error! Details

HttpError: Invalid request.

Only 65535 characters are allowed; 2473418 were supplied.

Please sign in to comment.