2
0

display_svg_spec.js 4.6 KB

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