display_svg_spec.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 _domstubs = require("../../examples/node/domstubs");
  24. var _test_utils = require("./test_utils");
  25. var _api = require("../../display/api");
  26. var _is_node = _interopRequireDefault(require("../../shared/is_node"));
  27. var _util = require("../../shared/util");
  28. var _svg = require("../../display/svg");
  29. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  30. var XLINK_NS = 'http://www.w3.org/1999/xlink';
  31. function withZlib(isZlibRequired, callback) {
  32. if (isZlibRequired) {
  33. if (!(0, _is_node["default"])()) {
  34. throw new Error('zlib test can only be run in Node.js');
  35. }
  36. return callback();
  37. }
  38. if (!(0, _is_node["default"])()) {
  39. return callback();
  40. }
  41. var zlib = require('zlib');
  42. var deflateSync = zlib.deflateSync;
  43. zlib.deflateSync = disabledDeflateSync;
  44. function disabledDeflateSync() {
  45. throw new Error('zlib.deflateSync is explicitly disabled for testing.');
  46. }
  47. function restoreDeflateSync() {
  48. if (zlib.deflateSync === disabledDeflateSync) {
  49. zlib.deflateSync = deflateSync;
  50. }
  51. }
  52. var promise = callback();
  53. promise.then(restoreDeflateSync, restoreDeflateSync);
  54. return promise;
  55. }
  56. describe('SVGGraphics', function () {
  57. var loadingTask;
  58. var page;
  59. beforeAll(function (done) {
  60. loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('xobject-image.pdf', {
  61. nativeImageDecoderSupport: _util.NativeImageDecoding.DISPLAY
  62. }));
  63. loadingTask.promise.then(function (doc) {
  64. doc.getPage(1).then(function (firstPage) {
  65. page = firstPage;
  66. done();
  67. });
  68. });
  69. });
  70. afterAll(function (done) {
  71. loadingTask.destroy().then(done);
  72. });
  73. describe('paintImageXObject', function () {
  74. function getSVGImage() {
  75. var svgGfx;
  76. return page.getOperatorList().then(function (opList) {
  77. var forceDataSchema = true;
  78. svgGfx = new _svg.SVGGraphics(page.commonObjs, page.objs, forceDataSchema);
  79. return svgGfx.loadDependencies(opList);
  80. }).then(function () {
  81. var svgImg;
  82. var elementContainer = {
  83. appendChild: function appendChild(element) {
  84. svgImg = element;
  85. }
  86. };
  87. var xobjectObjId = 'img_p0_1';
  88. if ((0, _is_node["default"])()) {
  89. (0, _domstubs.setStubs)(global);
  90. }
  91. try {
  92. var imgData = svgGfx.objs.get(xobjectObjId);
  93. svgGfx.paintInlineImageXObject(imgData, elementContainer);
  94. } finally {
  95. if ((0, _is_node["default"])()) {
  96. (0, _domstubs.unsetStubs)(global);
  97. }
  98. }
  99. return svgImg;
  100. });
  101. }
  102. it('should fail require("zlib") unless in Node.js', function () {
  103. function testFunc() {
  104. require('zlib');
  105. }
  106. expect(testFunc.toString()).toMatch(/\srequire\(["']zlib["']\)/);
  107. if ((0, _is_node["default"])()) {
  108. expect(testFunc).not.toThrow();
  109. } else {
  110. expect(testFunc).toThrow();
  111. }
  112. });
  113. it('should produce a reasonably small svg:image', function (done) {
  114. if (!(0, _is_node["default"])()) {
  115. pending('zlib.deflateSync is not supported in non-Node environments.');
  116. }
  117. withZlib(true, getSVGImage).then(function (svgImg) {
  118. expect(svgImg.nodeName).toBe('svg:image');
  119. expect(svgImg.getAttributeNS(null, 'width')).toBe('200px');
  120. expect(svgImg.getAttributeNS(null, 'height')).toBe('100px');
  121. var imgUrl = svgImg.getAttributeNS(XLINK_NS, 'href');
  122. expect(imgUrl).toMatch(/^data:image\/png;base64,/);
  123. expect(imgUrl.length).toBeLessThan(367);
  124. }).then(done, done.fail);
  125. });
  126. it('should be able to produce a svg:image without zlib', function (done) {
  127. withZlib(false, getSVGImage).then(function (svgImg) {
  128. expect(svgImg.nodeName).toBe('svg:image');
  129. expect(svgImg.getAttributeNS(null, 'width')).toBe('200px');
  130. expect(svgImg.getAttributeNS(null, 'height')).toBe('100px');
  131. var imgUrl = svgImg.getAttributeNS(XLINK_NS, 'href');
  132. expect(imgUrl).toMatch(/^data:image\/png;base64,/);
  133. expect(imgUrl.length).toBe(80246);
  134. }).then(done, done.fail);
  135. });
  136. });
  137. });