UNPKG

20.2 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * Javascript code in this page
4 *
5 * Copyright 2019 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
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.FontLoader = exports.FontFaceObject = void 0;
28
29var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
30
31var _util = require("../shared/util");
32
33function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
34
35function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
36
37function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
38
39function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
40
41function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
42
43function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
44
45function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
46
47function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
48
49function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
50
51function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
52
53function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
54
55function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
56
57var BaseFontLoader =
58/*#__PURE__*/
59function () {
60 function BaseFontLoader(_ref) {
61 var docId = _ref.docId,
62 onUnsupportedFeature = _ref.onUnsupportedFeature;
63
64 _classCallCheck(this, BaseFontLoader);
65
66 if (this.constructor === BaseFontLoader) {
67 (0, _util.unreachable)('Cannot initialize BaseFontLoader.');
68 }
69
70 this.docId = docId;
71 this._onUnsupportedFeature = onUnsupportedFeature;
72 this.nativeFontFaces = [];
73 this.styleElement = null;
74 }
75
76 _createClass(BaseFontLoader, [{
77 key: "addNativeFontFace",
78 value: function addNativeFontFace(nativeFontFace) {
79 this.nativeFontFaces.push(nativeFontFace);
80 document.fonts.add(nativeFontFace);
81 }
82 }, {
83 key: "insertRule",
84 value: function insertRule(rule) {
85 var styleElement = this.styleElement;
86
87 if (!styleElement) {
88 styleElement = this.styleElement = document.createElement('style');
89 styleElement.id = "PDFJS_FONT_STYLE_TAG_".concat(this.docId);
90 document.documentElement.getElementsByTagName('head')[0].appendChild(styleElement);
91 }
92
93 var styleSheet = styleElement.sheet;
94 styleSheet.insertRule(rule, styleSheet.cssRules.length);
95 }
96 }, {
97 key: "clear",
98 value: function clear() {
99 this.nativeFontFaces.forEach(function (nativeFontFace) {
100 document.fonts["delete"](nativeFontFace);
101 });
102 this.nativeFontFaces.length = 0;
103
104 if (this.styleElement) {
105 this.styleElement.remove();
106 this.styleElement = null;
107 }
108 }
109 }, {
110 key: "bind",
111 value: function () {
112 var _bind = _asyncToGenerator(
113 /*#__PURE__*/
114 _regenerator["default"].mark(function _callee(font) {
115 var _this = this;
116
117 var nativeFontFace, rule;
118 return _regenerator["default"].wrap(function _callee$(_context) {
119 while (1) {
120 switch (_context.prev = _context.next) {
121 case 0:
122 if (!(font.attached || font.missingFile)) {
123 _context.next = 2;
124 break;
125 }
126
127 return _context.abrupt("return", undefined);
128
129 case 2:
130 font.attached = true;
131
132 if (!this.isFontLoadingAPISupported) {
133 _context.next = 19;
134 break;
135 }
136
137 nativeFontFace = font.createNativeFontFace();
138
139 if (!nativeFontFace) {
140 _context.next = 18;
141 break;
142 }
143
144 this.addNativeFontFace(nativeFontFace);
145 _context.prev = 7;
146 _context.next = 10;
147 return nativeFontFace.loaded;
148
149 case 10:
150 _context.next = 18;
151 break;
152
153 case 12:
154 _context.prev = 12;
155 _context.t0 = _context["catch"](7);
156
157 this._onUnsupportedFeature({
158 featureId: _util.UNSUPPORTED_FEATURES.font
159 });
160
161 (0, _util.warn)("Failed to load font '".concat(nativeFontFace.family, "': '").concat(_context.t0, "'."));
162 font.disableFontFace = true;
163 throw _context.t0;
164
165 case 18:
166 return _context.abrupt("return", undefined);
167
168 case 19:
169 rule = font.createFontFaceRule();
170
171 if (!rule) {
172 _context.next = 25;
173 break;
174 }
175
176 this.insertRule(rule);
177
178 if (!this.isSyncFontLoadingSupported) {
179 _context.next = 24;
180 break;
181 }
182
183 return _context.abrupt("return", undefined);
184
185 case 24:
186 return _context.abrupt("return", new Promise(function (resolve) {
187 var request = _this._queueLoadingCallback(resolve);
188
189 _this._prepareFontLoadEvent([rule], [font], request);
190 }));
191
192 case 25:
193 return _context.abrupt("return", undefined);
194
195 case 26:
196 case "end":
197 return _context.stop();
198 }
199 }
200 }, _callee, this, [[7, 12]]);
201 }));
202
203 function bind(_x) {
204 return _bind.apply(this, arguments);
205 }
206
207 return bind;
208 }()
209 }, {
210 key: "_queueLoadingCallback",
211 value: function _queueLoadingCallback(callback) {
212 (0, _util.unreachable)('Abstract method `_queueLoadingCallback`.');
213 }
214 }, {
215 key: "_prepareFontLoadEvent",
216 value: function _prepareFontLoadEvent(rules, fontsToLoad, request) {
217 (0, _util.unreachable)('Abstract method `_prepareFontLoadEvent`.');
218 }
219 }, {
220 key: "isFontLoadingAPISupported",
221 get: function get() {
222 (0, _util.unreachable)('Abstract method `isFontLoadingAPISupported`.');
223 }
224 }, {
225 key: "isSyncFontLoadingSupported",
226 get: function get() {
227 (0, _util.unreachable)('Abstract method `isSyncFontLoadingSupported`.');
228 }
229 }, {
230 key: "_loadTestFont",
231 get: function get() {
232 (0, _util.unreachable)('Abstract method `_loadTestFont`.');
233 }
234 }]);
235
236 return BaseFontLoader;
237}();
238
239var FontLoader;
240exports.FontLoader = FontLoader;
241{
242 exports.FontLoader = FontLoader =
243 /*#__PURE__*/
244 function (_BaseFontLoader) {
245 _inherits(GenericFontLoader, _BaseFontLoader);
246
247 function GenericFontLoader(docId) {
248 var _this2;
249
250 _classCallCheck(this, GenericFontLoader);
251
252 _this2 = _possibleConstructorReturn(this, _getPrototypeOf(GenericFontLoader).call(this, docId));
253 _this2.loadingContext = {
254 requests: [],
255 nextRequestId: 0
256 };
257 _this2.loadTestFontId = 0;
258 return _this2;
259 }
260
261 _createClass(GenericFontLoader, [{
262 key: "_queueLoadingCallback",
263 value: function _queueLoadingCallback(callback) {
264 function completeRequest() {
265 (0, _util.assert)(!request.done, 'completeRequest() cannot be called twice.');
266 request.done = true;
267
268 while (context.requests.length > 0 && context.requests[0].done) {
269 var otherRequest = context.requests.shift();
270 setTimeout(otherRequest.callback, 0);
271 }
272 }
273
274 var context = this.loadingContext;
275 var request = {
276 id: "pdfjs-font-loading-".concat(context.nextRequestId++),
277 done: false,
278 complete: completeRequest,
279 callback: callback
280 };
281 context.requests.push(request);
282 return request;
283 }
284 }, {
285 key: "_prepareFontLoadEvent",
286 value: function _prepareFontLoadEvent(rules, fonts, request) {
287 function int32(data, offset) {
288 return data.charCodeAt(offset) << 24 | data.charCodeAt(offset + 1) << 16 | data.charCodeAt(offset + 2) << 8 | data.charCodeAt(offset + 3) & 0xff;
289 }
290
291 function spliceString(s, offset, remove, insert) {
292 var chunk1 = s.substring(0, offset);
293 var chunk2 = s.substring(offset + remove);
294 return chunk1 + insert + chunk2;
295 }
296
297 var i, ii;
298 var canvas = document.createElement('canvas');
299 canvas.width = 1;
300 canvas.height = 1;
301 var ctx = canvas.getContext('2d');
302 var called = 0;
303
304 function isFontReady(name, callback) {
305 called++;
306
307 if (called > 30) {
308 (0, _util.warn)('Load test font never loaded.');
309 callback();
310 return;
311 }
312
313 ctx.font = '30px ' + name;
314 ctx.fillText('.', 0, 20);
315 var imageData = ctx.getImageData(0, 0, 1, 1);
316
317 if (imageData.data[3] > 0) {
318 callback();
319 return;
320 }
321
322 setTimeout(isFontReady.bind(null, name, callback));
323 }
324
325 var loadTestFontId = "lt".concat(Date.now()).concat(this.loadTestFontId++);
326 var data = this._loadTestFont;
327 var COMMENT_OFFSET = 976;
328 data = spliceString(data, COMMENT_OFFSET, loadTestFontId.length, loadTestFontId);
329 var CFF_CHECKSUM_OFFSET = 16;
330 var XXXX_VALUE = 0x58585858;
331 var checksum = int32(data, CFF_CHECKSUM_OFFSET);
332
333 for (i = 0, ii = loadTestFontId.length - 3; i < ii; i += 4) {
334 checksum = checksum - XXXX_VALUE + int32(loadTestFontId, i) | 0;
335 }
336
337 if (i < loadTestFontId.length) {
338 checksum = checksum - XXXX_VALUE + int32(loadTestFontId + 'XXX', i) | 0;
339 }
340
341 data = spliceString(data, CFF_CHECKSUM_OFFSET, 4, (0, _util.string32)(checksum));
342 var url = "url(data:font/opentype;base64,".concat(btoa(data), ");");
343 var rule = "@font-face {font-family:\"".concat(loadTestFontId, "\";src:").concat(url, "}");
344 this.insertRule(rule);
345 var names = [];
346
347 for (i = 0, ii = fonts.length; i < ii; i++) {
348 names.push(fonts[i].loadedName);
349 }
350
351 names.push(loadTestFontId);
352 var div = document.createElement('div');
353 div.setAttribute('style', 'visibility: hidden;' + 'width: 10px; height: 10px;' + 'position: absolute; top: 0px; left: 0px;');
354
355 for (i = 0, ii = names.length; i < ii; ++i) {
356 var span = document.createElement('span');
357 span.textContent = 'Hi';
358 span.style.fontFamily = names[i];
359 div.appendChild(span);
360 }
361
362 document.body.appendChild(div);
363 isFontReady(loadTestFontId, function () {
364 document.body.removeChild(div);
365 request.complete();
366 });
367 }
368 }, {
369 key: "isFontLoadingAPISupported",
370 get: function get() {
371 var supported = typeof document !== 'undefined' && !!document.fonts;
372
373 if (supported && typeof navigator !== 'undefined') {
374 var m = /Mozilla\/5.0.*?rv:(\d+).*? Gecko/.exec(navigator.userAgent);
375
376 if (m && m[1] < 63) {
377 supported = false;
378 }
379 }
380
381 return (0, _util.shadow)(this, 'isFontLoadingAPISupported', supported);
382 }
383 }, {
384 key: "isSyncFontLoadingSupported",
385 get: function get() {
386 var supported = false;
387
388 if (typeof navigator === 'undefined') {
389 supported = true;
390 } else {
391 var m = /Mozilla\/5.0.*?rv:(\d+).*? Gecko/.exec(navigator.userAgent);
392
393 if (m && m[1] >= 14) {
394 supported = true;
395 }
396 }
397
398 return (0, _util.shadow)(this, 'isSyncFontLoadingSupported', supported);
399 }
400 }, {
401 key: "_loadTestFont",
402 get: function get() {
403 var getLoadTestFont = function getLoadTestF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AAAAAAAAAAAAAAAAAAAAAAAA' + 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' + 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' + 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAAAAAD/gwAyAAAAAQAAAAAAAAAAAAAAAAAA' + 'AAABAAQEAAEBAQJYAAEBASH4DwD4GwHEAvgcA/gXBIwMAYuL+nz5tQXkD5j3CBLnEQAC' + 'AQEBIVhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYAAABAQAADwACAQEEE/t3' + 'Dov6fAH6fAT+fPp8+nwHDosMCvm1Cvm1DAz6fBQAAAAAAAABAAAAAMmJbzEAAAAAzgTj' + 'FQAAAADOBOQpAAEAAAAAAAAADAAUAAQAAAABAAAAAgABAAAAAAAAAAAD6AAAAAAAAA==');
405 };
406
407 return (0, _util.shadow)(this, '_loadTestFont', getLoadTestFont());
408 }
409 }]);
410
411 return GenericFontLoader;
412 }(BaseFontLoader);
413}
414var IsEvalSupportedCached = {
415 get value() {
416 return (0, _util.shadow)(this, 'value', (0, _util.isEvalSupported)());
417 }
418
419};
420
421var FontFaceObject =
422/*#__PURE__*/
423function () {
424 function FontFaceObject(translatedData, _ref2) {
425 var _ref2$isEvalSupported = _ref2.isEvalSupported,
426 isEvalSupported = _ref2$isEvalSupported === void 0 ? true : _ref2$isEvalSupported,
427 _ref2$disableFontFace = _ref2.disableFontFace,
428 disableFontFace = _ref2$disableFontFace === void 0 ? false : _ref2$disableFontFace,
429 _ref2$ignoreErrors = _ref2.ignoreErrors,
430 ignoreErrors = _ref2$ignoreErrors === void 0 ? false : _ref2$ignoreErrors,
431 _ref2$onUnsupportedFe = _ref2.onUnsupportedFeature,
432 onUnsupportedFeature = _ref2$onUnsupportedFe === void 0 ? null : _ref2$onUnsupportedFe,
433 _ref2$fontRegistry = _ref2.fontRegistry,
434 fontRegistry = _ref2$fontRegistry === void 0 ? null : _ref2$fontRegistry;
435
436 _classCallCheck(this, FontFaceObject);
437
438 this.compiledGlyphs = Object.create(null);
439
440 for (var i in translatedData) {
441 this[i] = translatedData[i];
442 }
443
444 this.isEvalSupported = isEvalSupported !== false;
445 this.disableFontFace = disableFontFace === true;
446 this.ignoreErrors = ignoreErrors === true;
447 this._onUnsupportedFeature = onUnsupportedFeature;
448 this.fontRegistry = fontRegistry;
449 }
450
451 _createClass(FontFaceObject, [{
452 key: "createNativeFontFace",
453 value: function createNativeFontFace() {
454 if (!this.data || this.disableFontFace) {
455 return null;
456 }
457
458 var nativeFontFace = new FontFace(this.loadedName, this.data, {});
459
460 if (this.fontRegistry) {
461 this.fontRegistry.registerFont(this);
462 }
463
464 return nativeFontFace;
465 }
466 }, {
467 key: "createFontFaceRule",
468 value: function createFontFaceRule() {
469 if (!this.data || this.disableFontFace) {
470 return null;
471 }
472
473 var data = (0, _util.bytesToString)(new Uint8Array(this.data));
474 var url = "url(data:".concat(this.mimetype, ";base64,").concat(btoa(data), ");");
475 var rule = "@font-face {font-family:\"".concat(this.loadedName, "\";src:").concat(url, "}");
476
477 if (this.fontRegistry) {
478 this.fontRegistry.registerFont(this, url);
479 }
480
481 return rule;
482 }
483 }, {
484 key: "getPathGenerator",
485 value: function getPathGenerator(objs, character) {
486 if (this.compiledGlyphs[character] !== undefined) {
487 return this.compiledGlyphs[character];
488 }
489
490 var cmds, current;
491
492 try {
493 cmds = objs.get(this.loadedName + '_path_' + character);
494 } catch (ex) {
495 if (!this.ignoreErrors) {
496 throw ex;
497 }
498
499 if (this._onUnsupportedFeature) {
500 this._onUnsupportedFeature({
501 featureId: _util.UNSUPPORTED_FEATURES.font
502 });
503 }
504
505 (0, _util.warn)("getPathGenerator - ignoring character: \"".concat(ex, "\"."));
506 return this.compiledGlyphs[character] = function (c, size) {};
507 }
508
509 if (this.isEvalSupported && IsEvalSupportedCached.value) {
510 var args,
511 js = '';
512
513 for (var i = 0, ii = cmds.length; i < ii; i++) {
514 current = cmds[i];
515
516 if (current.args !== undefined) {
517 args = current.args.join(',');
518 } else {
519 args = '';
520 }
521
522 js += 'c.' + current.cmd + '(' + args + ');\n';
523 }
524
525 return this.compiledGlyphs[character] = new Function('c', 'size', js);
526 }
527
528 return this.compiledGlyphs[character] = function (c, size) {
529 for (var _i = 0, _ii = cmds.length; _i < _ii; _i++) {
530 current = cmds[_i];
531
532 if (current.cmd === 'scale') {
533 current.args = [size, -size];
534 }
535
536 c[current.cmd].apply(c, current.args);
537 }
538 };
539 }
540 }]);
541
542 return FontFaceObject;
543}();
544
545exports.FontFaceObject = FontFaceObject;
\No newline at end of file