Skip to content
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

Merged
merged 1 commit into from
May 17, 2017

Conversation

cgreening
Copy link

Adds an additional parameter that lets you set the background color of the canvas when rendering pdf.

e.g. for a transparent background:

page.render({ canvasContext, viewport, backgroundColor: 'rgba(0,0,0,0)' });

@Snuffleupagus
Copy link
Collaborator

Snuffleupagus commented May 16, 2017

Isn't this basically a duplicate of PR #7911?

e.g. for a transparent background:

Based on this comment, it seems that a side-effect of a transparent canvas would be worse text rendering!?

@cgreening
Copy link
Author

Didn't realise someone had already tried to get this in.

I don't completely understand why it was rejected.

@yurydelendik
Copy link
Contributor

yurydelendik commented May 16, 2017

@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 :)

@@ -706,7 +706,8 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
CanvasGraphics.prototype = {

beginDrawing: function CanvasGraphics_beginDrawing(transform, viewport,
Copy link
Contributor

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})

Copy link
Author

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);

Copy link
Collaborator

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, }) {

Copy link
Contributor

@yurydelendik yurydelendik left a 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.

this.gfx.beginDrawing(params.transform,
params.viewport,
transparency,
params.backgroundColor);
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Author

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

Copy link
Collaborator

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,
});

@cgreening
Copy link
Author

Will update the code with requested changes and add some tests.

@@ -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)'
Copy link
Collaborator

@Snuffleupagus Snuffleupagus May 17, 2017

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.

Copy link
Author

Choose a reason for hiding this comment

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

done

@cgreening cgreening force-pushed the set-canvas-background-color branch from 963f744 to 61f894c Compare May 17, 2017 10:54
@cgreening
Copy link
Author

Tests and documentation added - let me know if you need anything else.

Copy link
Collaborator

@Snuffleupagus Snuffleupagus left a 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.

@@ -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).'
Copy link
Collaborator

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)'.

Copy link
Collaborator

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)'.

@@ -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';
Copy link
Collaborator

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.

@@ -57,4 +78,6 @@ class NodeCMapReaderFactory {
export {
NodeFileReaderFactory,
NodeCMapReaderFactory,
buildGetDocumentParams,
TEST_PDFS_PATH
Copy link
Collaborator

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,.

return params;
}


Copy link
Collaborator

@Snuffleupagus Snuffleupagus May 17, 2017

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';
Copy link
Collaborator

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.

Copy link
Author

Choose a reason for hiding this comment

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

done


afterAll(function(done) {
CanvasFactory = null;
done();
Copy link
Collaborator

@Snuffleupagus Snuffleupagus May 17, 2017

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);.

done();
}).catch(function (reason) {
done.fail(reason);
});
Copy link
Collaborator

@Snuffleupagus Snuffleupagus May 17, 2017

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);
});

@@ -706,7 +706,8 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
CanvasGraphics.prototype = {

beginDrawing: function CanvasGraphics_beginDrawing(transform, viewport,
Copy link
Collaborator

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, }) {

this.gfx.beginDrawing(params.transform,
params.viewport,
transparency,
params.backgroundColor);
Copy link
Collaborator

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;
Copy link
Collaborator

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.

Copy link
Author

Choose a reason for hiding this comment

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

done

@cgreening cgreening force-pushed the set-canvas-background-color branch from b96181f to f112aaa Compare May 17, 2017 12:33
@cgreening
Copy link
Author

Should be all the comments addressed - let me know if I've missed one.

transparency) {
beginDrawing: function CanvasGraphics_beginDrawing({
transform, viewport, transparency, backgroundColor = null,
}) {
Copy link
Collaborator

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, }) {

Refer to https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Object_initializer#New_notations_in_ECMAScript_2015.

Copy link
Author

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?

Copy link
Contributor

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.

CanvasFactory = null;
page = null;
loadingTask.destroy().then(done);
done();
Copy link
Collaborator

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.

Copy link
Author

Choose a reason for hiding this comment

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

done

@cgreening cgreening force-pushed the set-canvas-background-color branch from f112aaa to 70877e8 Compare May 17, 2017 13:55
@@ -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)'.
Copy link
Contributor

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?

Copy link
Author

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.

@cgreening cgreening force-pushed the set-canvas-background-color branch from 4dc5bf8 to 941a4e5 Compare May 17, 2017 14:33
@yurydelendik
Copy link
Contributor

/botio test

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Received

Command cmd_test from @yurydelendik received. Current queue size: 0

Live output at: http://54.67.70.0:8877/9054e4699dc1ed3/output.txt

@pdfjsbot
Copy link

From: Bot.io (Windows)


Received

Command cmd_test from @yurydelendik received. Current queue size: 0

Live output at: http://54.215.176.217:8877/416d5de51b82e21/output.txt

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Failed

Full output at http://54.67.70.0:8877/9054e4699dc1ed3/output.txt

Total script time: 23.48 mins

  • Font tests: Passed
  • Unit tests: FAILED
  • Regression tests: FAILED

Image differences available at: http://54.67.70.0:8877/9054e4699dc1ed3/reftest-analyzer.html#web=eq.log

@pdfjsbot
Copy link

From: Bot.io (Windows)


Success

Full output at http://54.215.176.217:8877/416d5de51b82e21/output.txt

Total script time: 25.76 mins

  • Font tests: Passed
  • Unit tests: Passed
  • Regression tests: Passed

transparency) {
beginDrawing({
transform, viewport, transparency, background = null,
}) {
Copy link
Collaborator

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, }) {

Copy link
Author

Choose a reason for hiding this comment

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

done

@cgreening cgreening force-pushed the set-canvas-background-color branch from 941a4e5 to cfc2f36 Compare May 17, 2017 16:07
@Snuffleupagus
Copy link
Collaborator

/botio test

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Received

Command cmd_test from @Snuffleupagus received. Current queue size: 0

Live output at: http://54.67.70.0:8877/d7f11232bc74cca/output.txt

@pdfjsbot
Copy link

From: Bot.io (Windows)


Received

Command cmd_test from @Snuffleupagus received. Current queue size: 0

Live output at: http://54.215.176.217:8877/6a35e96e41df208/output.txt

@pdfjsbot
Copy link

From: Bot.io (Linux m4)


Failed

Full output at http://54.67.70.0:8877/d7f11232bc74cca/output.txt

Total script time: 23.48 mins

  • Font tests: Passed
  • Unit tests: Passed
  • Regression tests: FAILED

Image differences available at: http://54.67.70.0:8877/d7f11232bc74cca/reftest-analyzer.html#web=eq.log

@pdfjsbot
Copy link

From: Bot.io (Windows)


Failed

Full output at http://54.215.176.217:8877/6a35e96e41df208/output.txt

Total script time: 25.50 mins

  • Font tests: Passed
  • Unit tests: Passed
  • Regression tests: FAILED

Image differences available at: http://54.215.176.217:8877/6a35e96e41df208/reftest-analyzer.html#web=eq.log

@Snuffleupagus Snuffleupagus merged commit 5958daf into mozilla:master May 17, 2017
@Snuffleupagus
Copy link
Collaborator

Thank you for the patch!

@timvandermeij
Copy link
Contributor

timvandermeij commented May 17, 2017

I'm a bit confused: are the Windows failures expected here?

Ninja edit: Already answered below :)

@Snuffleupagus
Copy link
Collaborator

Snuffleupagus commented May 17, 2017

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 master.

@brendandahl brendandahl changed the title Adds additional parameter so background color of canvas can be set [api-minor] Adds additional parameter so background color of canvas can be set May 18, 2017
movsb pushed a commit to movsb/pdf.js that referenced this pull request Jul 14, 2018
…olor

Adds additional parameter so background color of canvas can be set
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants