display_svg_spec.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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(async function () {
  57. loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)("xobject-image.pdf"));
  58. const doc = await loadingTask.promise;
  59. page = await doc.getPage(1);
  60. });
  61. afterAll(async function () {
  62. await loadingTask.destroy();
  63. });
  64. describe("paintImageXObject", function () {
  65. function getSVGImage() {
  66. let svgGfx;
  67. return page.getOperatorList().then(function (opList) {
  68. const forceDataSchema = true;
  69. svgGfx = new _svg.SVGGraphics(page.commonObjs, page.objs, forceDataSchema);
  70. return svgGfx.loadDependencies(opList);
  71. }).then(function () {
  72. let svgImg;
  73. const elementContainer = {
  74. append(...elements) {
  75. svgImg = elements.at(-1);
  76. }
  77. };
  78. const xobjectObjId = "img_p0_1";
  79. if (_is_node.isNodeJS) {
  80. const {
  81. setStubs
  82. } = require("../../examples/node/domstubs.js");
  83. setStubs(global);
  84. }
  85. try {
  86. const imgData = svgGfx.objs.get(xobjectObjId);
  87. svgGfx.paintInlineImageXObject(imgData, elementContainer);
  88. } finally {
  89. if (_is_node.isNodeJS) {
  90. const {
  91. unsetStubs
  92. } = require("../../examples/node/domstubs.js");
  93. unsetStubs(global);
  94. }
  95. }
  96. return svgImg;
  97. });
  98. }
  99. it('should fail require("zlib") unless in Node.js', function () {
  100. function testFunc() {
  101. require("zlib");
  102. }
  103. if (_is_node.isNodeJS) {
  104. expect(testFunc.toString()).toMatch(/\srequire\(["']zlib["']\)/);
  105. expect(testFunc).not.toThrow();
  106. } else {
  107. expect(testFunc).toThrow();
  108. }
  109. });
  110. it("should produce a reasonably small svg:image", async function () {
  111. if (!_is_node.isNodeJS) {
  112. pending("zlib.deflateSync is not supported in non-Node environments.");
  113. }
  114. const svgImg = await withZlib(true, getSVGImage);
  115. expect(svgImg.nodeName).toBe("svg:image");
  116. expect(svgImg.getAttributeNS(null, "width")).toBe("200px");
  117. expect(svgImg.getAttributeNS(null, "height")).toBe("100px");
  118. const imgUrl = svgImg.getAttributeNS(XLINK_NS, "href");
  119. expect(imgUrl).toMatch(/^data:image\/png;base64,/);
  120. expect(imgUrl.length).toBeLessThan(367);
  121. });
  122. it("should be able to produce a svg:image without zlib", async function () {
  123. const svgImg = await withZlib(false, getSVGImage);
  124. expect(svgImg.nodeName).toBe("svg:image");
  125. expect(svgImg.getAttributeNS(null, "width")).toBe("200px");
  126. expect(svgImg.getAttributeNS(null, "height")).toBe("100px");
  127. const imgUrl = svgImg.getAttributeNS(XLINK_NS, "href");
  128. expect(imgUrl).toMatch(/^data:image\/png;base64,/);
  129. expect(imgUrl.length).toBe(80246);
  130. });
  131. });
  132. });