UNPKG

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