UNPKG

3.44 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * JavaScript code in this page
4 *
5 * Copyright 2022 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.CFFFont = void 0;
28
29var _cff_parser = require("./cff_parser.js");
30
31var _fonts_utils = require("./fonts_utils.js");
32
33var _util = require("../shared/util.js");
34
35class CFFFont {
36 constructor(file, properties) {
37 this.properties = properties;
38 const parser = new _cff_parser.CFFParser(file, properties, _fonts_utils.SEAC_ANALYSIS_ENABLED);
39 this.cff = parser.parse();
40 this.cff.duplicateFirstGlyph();
41 const compiler = new _cff_parser.CFFCompiler(this.cff);
42 this.seacs = this.cff.seacs;
43
44 try {
45 this.data = compiler.compile();
46 } catch (e) {
47 (0, _util.warn)("Failed to compile font " + properties.loadedName);
48 this.data = file;
49 }
50
51 this._createBuiltInEncoding();
52 }
53
54 get numGlyphs() {
55 return this.cff.charStrings.count;
56 }
57
58 getCharset() {
59 return this.cff.charset.charset;
60 }
61
62 getGlyphMapping() {
63 const cff = this.cff;
64 const properties = this.properties;
65 const charsets = cff.charset.charset;
66 let charCodeToGlyphId;
67 let glyphId;
68
69 if (properties.composite) {
70 charCodeToGlyphId = Object.create(null);
71 let charCode;
72
73 if (cff.isCIDFont) {
74 for (glyphId = 0; glyphId < charsets.length; glyphId++) {
75 const cid = charsets[glyphId];
76 charCode = properties.cMap.charCodeOf(cid);
77 charCodeToGlyphId[charCode] = glyphId;
78 }
79 } else {
80 for (glyphId = 0; glyphId < cff.charStrings.count; glyphId++) {
81 charCode = properties.cMap.charCodeOf(glyphId);
82 charCodeToGlyphId[charCode] = glyphId;
83 }
84 }
85
86 return charCodeToGlyphId;
87 }
88
89 let encoding = cff.encoding ? cff.encoding.encoding : null;
90
91 if (properties.isInternalFont) {
92 encoding = properties.defaultEncoding;
93 }
94
95 charCodeToGlyphId = (0, _fonts_utils.type1FontGlyphMapping)(properties, encoding, charsets);
96 return charCodeToGlyphId;
97 }
98
99 hasGlyphId(id) {
100 return this.cff.hasGlyphId(id);
101 }
102
103 _createBuiltInEncoding() {
104 const {
105 charset,
106 encoding
107 } = this.cff;
108
109 if (!charset || !encoding) {
110 return;
111 }
112
113 const charsets = charset.charset,
114 encodings = encoding.encoding;
115 const map = [];
116
117 for (const charCode in encodings) {
118 const glyphId = encodings[charCode];
119
120 if (glyphId >= 0) {
121 const glyphName = charsets[glyphId];
122
123 if (glyphName) {
124 map[charCode] = glyphName;
125 }
126 }
127 }
128
129 if (map.length > 0) {
130 this.properties.builtInEncoding = map;
131 }
132 }
133
134}
135
136exports.CFFFont = CFFFont;
\No newline at end of file