-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
[api-minor] Adds additional parameter so background color of canvas can be set #8413
[api-minor] Adds additional parameter so background color of canvas can be set #8413
Conversation
Isn't this basically a duplicate of PR #7911?
Based on this comment, it seems that a side-effect of a transparent canvas would be worse text rendering!? |
Didn't realise someone had already tried to get this in. I don't completely understand why it was rejected. |
@cgreening there are multiple reasons, but main one (from my point of view) is we are hesitant to accept something without tests. Since we are doing refactoring sometimes, we might break some functionality we don't use. By accepting a patch, we usually committed to support it in the future, without testing it is just a burden :) |
src/display/canvas.js
Outdated
@@ -706,7 +706,8 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { | |||
CanvasGraphics.prototype = { | |||
|
|||
beginDrawing: function CanvasGraphics_beginDrawing(transform, viewport, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
refactor beginDrawing to destructure parameters:
beginDrawing({transform, viewport, transparency, backgroundColor})
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure this can be done as the transparency argument doesn't seem to come from params:
this.gfx.beginDrawing(params.transform,
params.viewport,
transparency,
params.backgroundColor);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming that @yurydelendik meant that you should change the method signature like this (and also change api.js
as indicated elsewhere):
beginDrawing({ transform, viewport, transparency,
backgroundColor = null, }) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That said. Let's add a tests for that, e.g. create a new file "custom_spec.js" to the unit tests that will read canvas pixels.
src/display/api.js
Outdated
this.gfx.beginDrawing(params.transform, | ||
params.viewport, | ||
transparency, | ||
params.backgroundColor); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add documentation about new parameter https://github.com/MRMBRAND/pdf.js/blob/f52376929d7584d41f38b2c741b6ed29b17fa181/src/display/api.js#L700
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done - let me know if it has enough details
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the changed signature of beginDrawing
, this becomes:
this.gfx.beginDrawing({
transform: params.transform,
viewport: params.viewport,
transparency,
backgroundColor: params.backgroundColor,
});
Will update the code with requested changes and add some tests. |
src/display/api.js
Outdated
@@ -717,6 +717,8 @@ var PDFDocumentProxy = (function PDFDocumentProxyClosure() { | |||
* @property {Object} canvasFactory - (optional) The factory that will be used | |||
* when creating canvases. The default value is | |||
* {DOMCanvasFactory}. | |||
* @property {string} backgroundColor - Background color to use for the canvas. | |||
* The default value is 'rgb(255,255,255)' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please mark this as (optional)
, compare e.g. with the canvasFactory
just above. Also, the The default value is 'rgb(255,255,255)'
sentence is missing a period at the end.
Finally, when all review comments have been addressed, please squash the commits into one; see https://github.com/mozilla/pdf.js/wiki/Squashing-Commits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
963f744
to
61f894c
Compare
Tests and documentation added - let me know if you need anything else. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding unit-tests!
However, none of them currently work, since they reference a PDF file that isn't included in the patch.
Also, please use let
rather than var
when you're adding new code.
src/display/api.js
Outdated
@@ -717,6 +717,8 @@ var PDFDocumentProxy = (function PDFDocumentProxyClosure() { | |||
* @property {Object} canvasFactory - (optional) The factory that will be used | |||
* when creating canvases. The default value is | |||
* {DOMCanvasFactory}. | |||
* @property {string} backgroundColor - (optional) Background color to use for the canvas. | |||
* The default value is 'rgb(255,255,255).' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Incorrect placement of the period, it should be The default value is 'rgb(255,255,255)'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, please keep the line lengths in mind, i.e. this needs to be
* @property {string} backgroundColor - (optional) Background color to use for
* the canvas. The default value is 'rgb(255,255,255)'.
test/unit/api_spec.js
Outdated
@@ -12,7 +12,7 @@ | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
|
|||
import { buildGetDocumentParams, NodeFileReaderFactory, TEST_PDFS_PATH } from './test_utils'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is too long, it should be:
import {
buildGetDocumentParams, NodeFileReaderFactory, TEST_PDFS_PATH
} from './test_utils';
And please keep one blank line between the copyright header and the first import
.
test/unit/test_utils.js
Outdated
@@ -57,4 +78,6 @@ class NodeCMapReaderFactory { | |||
export { | |||
NodeFileReaderFactory, | |||
NodeCMapReaderFactory, | |||
buildGetDocumentParams, | |||
TEST_PDFS_PATH |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Add a colon at the end here, i.e. TEST_PDFS_PATH,
.
test/unit/test_utils.js
Outdated
return params; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Please remove a blank line here, since one is enough.
@@ -0,0 +1,99 @@ | |||
import { buildGetDocumentParams } from './test_utils'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is missing the required license header, see e.g. https://github.com/mozilla/pdf.js/blob/master/src/license_header.js.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
test/unit/custom_spec.js
Outdated
|
||
afterAll(function(done) { | ||
CanvasFactory = null; | ||
done(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please clean up the loadingTask
properly here, i.e. loadingTask.destroy().then(done);
.
test/unit/custom_spec.js
Outdated
done(); | ||
}).catch(function (reason) { | ||
done.fail(reason); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: The above can probably be simplified to (applies in the test below as well):
page.render({
canvasContext: canvasAndCtx.context,
viewport,
}).then(function() {
var { r, g, b, a } = getTopLeftPixel(canvasAndCtx.context);
CanvasFactory.destroy(canvasAndCtx);
expect(r).toEqual(255);
expect(g).toEqual(255);
expect(b).toEqual(255);
expect(a).toEqual(255);
done();
}).catch(function (reason) {
done.fail(reason);
});
src/display/canvas.js
Outdated
@@ -706,7 +706,8 @@ var CanvasGraphics = (function CanvasGraphicsClosure() { | |||
CanvasGraphics.prototype = { | |||
|
|||
beginDrawing: function CanvasGraphics_beginDrawing(transform, viewport, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming that @yurydelendik meant that you should change the method signature like this (and also change api.js
as indicated elsewhere):
beginDrawing({ transform, viewport, transparency,
backgroundColor = null, }) {
src/display/api.js
Outdated
this.gfx.beginDrawing(params.transform, | ||
params.viewport, | ||
transparency, | ||
params.backgroundColor); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the changed signature of beginDrawing
, this becomes:
this.gfx.beginDrawing({
transform: params.transform,
viewport: params.viewport,
transparency,
backgroundColor: params.backgroundColor,
});
}); | ||
|
||
afterAll(function(done) { | ||
CanvasFactory = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add page = null;
here too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
b96181f
to
f112aaa
Compare
Should be all the comments addressed - let me know if I've missed one. |
src/display/canvas.js
Outdated
transparency) { | ||
beginDrawing: function CanvasGraphics_beginDrawing({ | ||
transform, viewport, transparency, backgroundColor = null, | ||
}) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is overly verbose, taking full advantage of ES6 notation this can be simplified to:
beginDrawing({ transform, viewport, transparency,
backgroundColor = null, }) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems to be the style of all the functions in the canvas.js file. I'm happy to clean that up as part of this pull request - but it might be better in a new one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We slowly (since not all the code covered by tests) refactoring that to new style. In sake of this PR, we prefer to change only the affected code.
test/unit/custom_spec.js
Outdated
CanvasFactory = null; | ||
page = null; | ||
loadingTask.destroy().then(done); | ||
done(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line needs to be removed, since the point of loadingTask.destroy().then(done);
just above is to wait for everything to be terminated before continuing with the tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
f112aaa
to
70877e8
Compare
src/display/api.js
Outdated
@@ -717,6 +717,8 @@ var PDFDocumentProxy = (function PDFDocumentProxyClosure() { | |||
* @property {Object} canvasFactory - (optional) The factory that will be used | |||
* when creating canvases. The default value is | |||
* {DOMCanvasFactory}. | |||
* @property {string} backgroundColor - (optional) Background color to use for | |||
* the canvas. The default value is 'rgb(255,255,255)'. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can you make a note that the color is specified in the CSS format? Actually it can be string, CanvasGradient, or CanvasPattern -- someone creative might apply paper texture as a background, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have added more docs and changes the name of the parameter to "background" as it's not necessarily a color.
4dc5bf8
to
941a4e5
Compare
/botio test |
From: Bot.io (Linux m4)ReceivedCommand cmd_test from @yurydelendik received. Current queue size: 0 Live output at: http://54.67.70.0:8877/9054e4699dc1ed3/output.txt |
From: Bot.io (Windows)ReceivedCommand cmd_test from @yurydelendik received. Current queue size: 0 Live output at: http://54.215.176.217:8877/416d5de51b82e21/output.txt |
From: Bot.io (Linux m4)FailedFull output at http://54.67.70.0:8877/9054e4699dc1ed3/output.txt Total script time: 23.48 mins
Image differences available at: http://54.67.70.0:8877/9054e4699dc1ed3/reftest-analyzer.html#web=eq.log |
From: Bot.io (Windows)SuccessFull output at http://54.215.176.217:8877/416d5de51b82e21/output.txt Total script time: 25.76 mins
|
src/display/canvas.js
Outdated
transparency) { | ||
beginDrawing({ | ||
transform, viewport, transparency, background = null, | ||
}) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks kind of weird, compared to existing method signatures with ES6 parameter destructuring (e.g. the placement of the {
bracket is strange); it should be:
beginDrawing({ transform, viewport, transparency,
background = null, }) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
941a4e5
to
cfc2f36
Compare
/botio test |
From: Bot.io (Linux m4)ReceivedCommand cmd_test from @Snuffleupagus received. Current queue size: 0 Live output at: http://54.67.70.0:8877/d7f11232bc74cca/output.txt |
From: Bot.io (Windows)ReceivedCommand cmd_test from @Snuffleupagus received. Current queue size: 0 Live output at: http://54.215.176.217:8877/6a35e96e41df208/output.txt |
From: Bot.io (Linux m4)FailedFull output at http://54.67.70.0:8877/d7f11232bc74cca/output.txt Total script time: 23.48 mins
Image differences available at: http://54.67.70.0:8877/d7f11232bc74cca/reftest-analyzer.html#web=eq.log |
From: Bot.io (Windows)FailedFull output at http://54.215.176.217:8877/6a35e96e41df208/output.txt Total script time: 25.50 mins
Image differences available at: http://54.215.176.217:8877/6a35e96e41df208/reftest-analyzer.html#web=eq.log |
Thank you for the patch! |
I'm a bit confused: are the Windows failures expected here? Ninja edit: Already answered below :) |
Please note: The test "failures" in Firefox on the Windows bot in the latest test run isn't caused by this patch, since I can replicate all of those changes locally (on Windows) when testing against |
…olor Adds additional parameter so background color of canvas can be set
Adds an additional parameter that lets you set the background color of the canvas when rendering pdf.
e.g. for a transparent background: