display_svg_spec.js 4.7 KB

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