display_svg_spec.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. var _util = require('../../shared/util');
  17. var _domstubs = require('../../examples/node/domstubs');
  18. var _test_utils = require('./test_utils');
  19. var _api = require('../../display/api');
  20. var _svg = require('../../display/svg');
  21. var XLINK_NS = 'http://www.w3.org/1999/xlink';
  22. function withZlib(isZlibRequired, callback) {
  23. if (isZlibRequired) {
  24. if (!(0, _util.isNodeJS)()) {
  25. throw new Error('zlib test can only be run in Node.js');
  26. }
  27. return callback();
  28. }
  29. if (!(0, _util.isNodeJS)()) {
  30. return callback();
  31. }
  32. var zlib = require('zlib');
  33. var deflateSync = zlib.deflateSync;
  34. zlib.deflateSync = disabledDeflateSync;
  35. function disabledDeflateSync() {
  36. throw new Error('zlib.deflateSync is explicitly disabled for testing.');
  37. }
  38. function restoreDeflateSync() {
  39. if (zlib.deflateSync === disabledDeflateSync) {
  40. zlib.deflateSync = deflateSync;
  41. }
  42. }
  43. var promise = callback();
  44. promise.then(restoreDeflateSync, restoreDeflateSync);
  45. return promise;
  46. }
  47. describe('SVGGraphics', function () {
  48. var loadingTask;
  49. var page;
  50. beforeAll(function (done) {
  51. loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('xobject-image.pdf', { nativeImageDecoderSupport: _util.NativeImageDecoding.DISPLAY }));
  52. loadingTask.promise.then(function (doc) {
  53. doc.getPage(1).then(function (firstPage) {
  54. page = firstPage;
  55. done();
  56. });
  57. });
  58. });
  59. afterAll(function (done) {
  60. loadingTask.destroy().then(done);
  61. });
  62. describe('paintImageXObject', function () {
  63. function getSVGImage() {
  64. var svgGfx;
  65. return page.getOperatorList().then(function (opList) {
  66. var forceDataSchema = true;
  67. svgGfx = new _svg.SVGGraphics(page.commonObjs, page.objs, forceDataSchema);
  68. return svgGfx.loadDependencies(opList);
  69. }).then(function () {
  70. var svgImg;
  71. var elementContainer = {
  72. appendChild: function appendChild(element) {
  73. svgImg = element;
  74. }
  75. };
  76. var xobjectObjId = 'img_p0_1';
  77. if ((0, _util.isNodeJS)()) {
  78. (0, _domstubs.setStubs)(global);
  79. }
  80. try {
  81. var imgData = svgGfx.objs.get(xobjectObjId);
  82. svgGfx.paintInlineImageXObject(imgData, elementContainer);
  83. } finally {
  84. if ((0, _util.isNodeJS)()) {
  85. (0, _domstubs.unsetStubs)(global);
  86. }
  87. }
  88. return svgImg;
  89. });
  90. }
  91. it('should fail require("zlib") unless in Node.js', function () {
  92. function testFunc() {
  93. require('zlib');
  94. }
  95. expect(testFunc.toString()).toMatch(/\srequire\(["']zlib["']\)/);
  96. if ((0, _util.isNodeJS)()) {
  97. expect(testFunc).not.toThrow();
  98. } else {
  99. expect(testFunc).toThrow();
  100. }
  101. });
  102. it('should produce a reasonably small svg:image', function (done) {
  103. if (!(0, _util.isNodeJS)()) {
  104. pending('zlib.deflateSync is not supported in non-Node environments.');
  105. }
  106. withZlib(true, getSVGImage).then(function (svgImg) {
  107. expect(svgImg.nodeName).toBe('svg:image');
  108. expect(svgImg.getAttributeNS(null, 'width')).toBe('200px');
  109. expect(svgImg.getAttributeNS(null, 'height')).toBe('100px');
  110. var imgUrl = svgImg.getAttributeNS(XLINK_NS, 'href');
  111. expect(imgUrl).toMatch(/^data:image\/png;base64,/);
  112. expect(imgUrl.length).toBeLessThan(367);
  113. }).then(done, done.fail);
  114. });
  115. it('should be able to produce a svg:image without zlib', function (done) {
  116. withZlib(false, 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).toBe(80246);
  123. }).then(done, done.fail);
  124. });
  125. });
  126. });