1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | 'use strict';
|
23 |
|
24 | var _test_utils = require('./test_utils');
|
25 |
|
26 | var _dom_utils = require('../../display/dom_utils');
|
27 |
|
28 | var _api = require('../../display/api');
|
29 |
|
30 | var _is_node = require('../../shared/is_node');
|
31 |
|
32 | var _is_node2 = _interopRequireDefault(_is_node);
|
33 |
|
34 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
35 |
|
36 | function getTopLeftPixel(canvasContext) {
|
37 | var imgData = canvasContext.getImageData(0, 0, 1, 1);
|
38 | return {
|
39 | r: imgData.data[0],
|
40 | g: imgData.data[1],
|
41 | b: imgData.data[2],
|
42 | a: imgData.data[3]
|
43 | };
|
44 | }
|
45 | describe('custom canvas rendering', function () {
|
46 | var transparentGetDocumentParams = (0, _test_utils.buildGetDocumentParams)('transparent.pdf');
|
47 | var CanvasFactory = void 0;
|
48 | var loadingTask = void 0;
|
49 | var page = void 0;
|
50 | beforeAll(function (done) {
|
51 | if ((0, _is_node2.default)()) {} else {
|
52 | CanvasFactory = new _dom_utils.DOMCanvasFactory();
|
53 | }
|
54 | loadingTask = (0, _api.getDocument)(transparentGetDocumentParams);
|
55 | loadingTask.promise.then(function (doc) {
|
56 | return doc.getPage(1);
|
57 | }).then(function (data) {
|
58 | page = data;
|
59 | done();
|
60 | }).catch(function (reason) {
|
61 | done.fail(reason);
|
62 | });
|
63 | });
|
64 | afterAll(function (done) {
|
65 | CanvasFactory = null;
|
66 | page = null;
|
67 | loadingTask.destroy().then(done);
|
68 | });
|
69 | it('renders to canvas with a default white background', function (done) {
|
70 | if ((0, _is_node2.default)()) {
|
71 | pending('TODO: Support Canvas testing in Node.js.');
|
72 | }
|
73 | var viewport = page.getViewport(1);
|
74 | var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
|
75 | page.render({
|
76 | canvasContext: canvasAndCtx.context,
|
77 | viewport: viewport
|
78 | }).then(function () {
|
79 | var _getTopLeftPixel = getTopLeftPixel(canvasAndCtx.context),
|
80 | r = _getTopLeftPixel.r,
|
81 | g = _getTopLeftPixel.g,
|
82 | b = _getTopLeftPixel.b,
|
83 | a = _getTopLeftPixel.a;
|
84 |
|
85 | CanvasFactory.destroy(canvasAndCtx);
|
86 | expect(r).toEqual(255);
|
87 | expect(g).toEqual(255);
|
88 | expect(b).toEqual(255);
|
89 | expect(a).toEqual(255);
|
90 | done();
|
91 | }).catch(function (reason) {
|
92 | done(reason);
|
93 | });
|
94 | });
|
95 | it('renders to canvas with a custom background', function (done) {
|
96 | if ((0, _is_node2.default)()) {
|
97 | pending('TODO: Support Canvas testing in Node.js.');
|
98 | }
|
99 | var viewport = page.getViewport(1);
|
100 | var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
|
101 | page.render({
|
102 | canvasContext: canvasAndCtx.context,
|
103 | viewport: viewport,
|
104 | background: 'rgba(255,0,0,1.0)'
|
105 | }).then(function () {
|
106 | var _getTopLeftPixel2 = getTopLeftPixel(canvasAndCtx.context),
|
107 | r = _getTopLeftPixel2.r,
|
108 | g = _getTopLeftPixel2.g,
|
109 | b = _getTopLeftPixel2.b,
|
110 | a = _getTopLeftPixel2.a;
|
111 |
|
112 | CanvasFactory.destroy(canvasAndCtx);
|
113 | expect(r).toEqual(255);
|
114 | expect(g).toEqual(0);
|
115 | expect(b).toEqual(0);
|
116 | expect(a).toEqual(255);
|
117 | done();
|
118 | }).catch(function (reason) {
|
119 | done(reason);
|
120 | });
|
121 | });
|
122 | }); |
\ | No newline at end of file |