display_svg_spec.js 4.7 KB

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