display_svg_spec.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 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 = require('../../shared/is_node');
  27. var _is_node2 = _interopRequireDefault(_is_node);
  28. var _util = require('../../shared/util');
  29. var _svg = require('../../display/svg');
  30. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  31. var XLINK_NS = 'http://www.w3.org/1999/xlink';
  32. function withZlib(isZlibRequired, callback) {
  33. if (isZlibRequired) {
  34. if (!(0, _is_node2.default)()) {
  35. throw new Error('zlib test can only be run in Node.js');
  36. }
  37. return callback();
  38. }
  39. if (!(0, _is_node2.default)()) {
  40. return callback();
  41. }
  42. var zlib = require('zlib');
  43. var deflateSync = zlib.deflateSync;
  44. zlib.deflateSync = disabledDeflateSync;
  45. function disabledDeflateSync() {
  46. throw new Error('zlib.deflateSync is explicitly disabled for testing.');
  47. }
  48. function restoreDeflateSync() {
  49. if (zlib.deflateSync === disabledDeflateSync) {
  50. zlib.deflateSync = deflateSync;
  51. }
  52. }
  53. var promise = callback();
  54. promise.then(restoreDeflateSync, restoreDeflateSync);
  55. return promise;
  56. }
  57. describe('SVGGraphics', function () {
  58. var loadingTask;
  59. var page;
  60. beforeAll(function (done) {
  61. loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('xobject-image.pdf', { nativeImageDecoderSupport: _util.NativeImageDecoding.DISPLAY }));
  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: function appendChild(element) {
  83. svgImg = element;
  84. }
  85. };
  86. var xobjectObjId = 'img_p0_1';
  87. if ((0, _is_node2.default)()) {
  88. (0, _domstubs.setStubs)(global);
  89. }
  90. try {
  91. var imgData = svgGfx.objs.get(xobjectObjId);
  92. svgGfx.paintInlineImageXObject(imgData, elementContainer);
  93. } finally {
  94. if ((0, _is_node2.default)()) {
  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 ((0, _is_node2.default)()) {
  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 (!(0, _is_node2.default)()) {
  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. });