2
0

custom_spec.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. var _test_utils = require("./test_utils");
  24. var _display_utils = require("../../display/display_utils");
  25. var _api = require("../../display/api");
  26. var _is_node = _interopRequireDefault(require("../../shared/is_node"));
  27. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  28. function getTopLeftPixel(canvasContext) {
  29. var imgData = canvasContext.getImageData(0, 0, 1, 1);
  30. return {
  31. r: imgData.data[0],
  32. g: imgData.data[1],
  33. b: imgData.data[2],
  34. a: imgData.data[3]
  35. };
  36. }
  37. describe('custom canvas rendering', function () {
  38. var transparentGetDocumentParams = (0, _test_utils.buildGetDocumentParams)('transparent.pdf');
  39. var CanvasFactory;
  40. var loadingTask;
  41. var page;
  42. beforeAll(function (done) {
  43. if ((0, _is_node["default"])()) {
  44. CanvasFactory = new _test_utils.NodeCanvasFactory();
  45. } else {
  46. CanvasFactory = new _display_utils.DOMCanvasFactory();
  47. }
  48. loadingTask = (0, _api.getDocument)(transparentGetDocumentParams);
  49. loadingTask.promise.then(function (doc) {
  50. return doc.getPage(1);
  51. }).then(function (data) {
  52. page = data;
  53. done();
  54. })["catch"](done.fail);
  55. });
  56. afterAll(function (done) {
  57. CanvasFactory = null;
  58. page = null;
  59. loadingTask.destroy().then(done);
  60. });
  61. it('renders to canvas with a default white background', function (done) {
  62. var viewport = page.getViewport({
  63. scale: 1
  64. });
  65. var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
  66. var renderTask = page.render({
  67. canvasContext: canvasAndCtx.context,
  68. viewport: viewport
  69. });
  70. renderTask.promise.then(function () {
  71. expect(getTopLeftPixel(canvasAndCtx.context)).toEqual({
  72. r: 255,
  73. g: 255,
  74. b: 255,
  75. a: 255
  76. });
  77. CanvasFactory.destroy(canvasAndCtx);
  78. done();
  79. })["catch"](done.fail);
  80. });
  81. it('renders to canvas with a custom background', function (done) {
  82. var viewport = page.getViewport({
  83. scale: 1
  84. });
  85. var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
  86. var renderTask = page.render({
  87. canvasContext: canvasAndCtx.context,
  88. viewport: viewport,
  89. background: 'rgba(255,0,0,1.0)'
  90. });
  91. renderTask.promise.then(function () {
  92. expect(getTopLeftPixel(canvasAndCtx.context)).toEqual({
  93. r: 255,
  94. g: 0,
  95. b: 0,
  96. a: 255
  97. });
  98. CanvasFactory.destroy(canvasAndCtx);
  99. done();
  100. })["catch"](done.fail);
  101. });
  102. });