display_svg_spec.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. function withZlib(isZlibRequired, callback) {
  22. if (isZlibRequired) {
  23. if (!(0, _util.isNodeJS)()) {
  24. throw new Error('zlib test can only be run in Node.js');
  25. }
  26. return callback();
  27. }
  28. if (!(0, _util.isNodeJS)()) {
  29. return callback();
  30. }
  31. var zlib = require('zlib');
  32. var deflateSync = zlib.deflateSync;
  33. zlib.deflateSync = function () {
  34. throw new Error('zlib.deflateSync is explicitly disabled for testing.');
  35. };
  36. try {
  37. return callback();
  38. } finally {
  39. zlib.deflateSync = deflateSync;
  40. }
  41. }
  42. describe('SVGGraphics', function () {
  43. var loadingTask;
  44. var page;
  45. beforeAll(function (done) {
  46. loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('xobject-image.pdf', { nativeImageDecoderSupport: _util.NativeImageDecoding.DISPLAY }));
  47. loadingTask.promise.then(function (doc) {
  48. doc.getPage(1).then(function (firstPage) {
  49. page = firstPage;
  50. done();
  51. });
  52. });
  53. });
  54. afterAll(function (done) {
  55. loadingTask.destroy().then(done);
  56. });
  57. describe('paintImageXObject', function () {
  58. function getSVGImage() {
  59. var svgGfx;
  60. return page.getOperatorList().then(function (opList) {
  61. var forceDataSchema = true;
  62. svgGfx = new _svg.SVGGraphics(page.commonObjs, page.objs, forceDataSchema);
  63. return svgGfx.loadDependencies(opList);
  64. }).then(function () {
  65. var svgImg;
  66. var elementContainer = {
  67. appendChild: function appendChild(element) {
  68. svgImg = element;
  69. }
  70. };
  71. var xobjectObjId = {
  72. ref: 4,
  73. gen: 0
  74. };
  75. if ((0, _util.isNodeJS)()) {
  76. (0, _domstubs.setStubs)(global);
  77. }
  78. try {
  79. svgGfx.paintImageXObject(xobjectObjId, elementContainer);
  80. } finally {
  81. if ((0, _util.isNodeJS)()) {
  82. (0, _domstubs.unsetStubs)(global);
  83. }
  84. }
  85. return svgImg;
  86. });
  87. }
  88. it('should produce a reasonably small svg:image', function () {
  89. if (!(0, _util.isNodeJS)()) {
  90. pending('zlib.deflateSync is not supported in non-Node environments.');
  91. }
  92. withZlib(true, getSVGImage).then(function (svgImg) {
  93. expect(svgImg.nodeName).toBe('svg:image');
  94. expect(svgImg.getAttribute('width')).toBe('200px');
  95. expect(svgImg.getAttribute('height')).toBe('100px');
  96. var imgUrl = svgImg.getAttribute('xlink:href');
  97. expect(imgUrl).toMatch(/^data:image\/png;base64,/);
  98. expect(imgUrl.length).toBeLessThan(367);
  99. });
  100. });
  101. it('should produce a svg:image even if zlib is unavailable', function () {
  102. withZlib(false, getSVGImage).then(function (svgImg) {
  103. expect(svgImg.nodeName).toBe('svg:image');
  104. expect(svgImg.getAttribute('width')).toBe('200px');
  105. expect(svgImg.getAttribute('height')).toBe('100px');
  106. var imgUrl = svgImg.getAttribute('xlink:href');
  107. expect(imgUrl).toMatch(/^data:image\/png;base64,/);
  108. expect(imgUrl.length).toBe(80247);
  109. });
  110. });
  111. });
  112. });