Move the CFFFont
from src/core/fonts.js
and into its own file
This commit is contained in:
parent
d4606712f2
commit
d5d73e3168
116
src/core/cff_font.js
Normal file
116
src/core/cff_font.js
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
/* Copyright 2012 Mozilla Foundation
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
/* eslint-disable no-var */
|
||||||
|
|
||||||
|
import { CFFCompiler, CFFParser } from "./cff_parser.js";
|
||||||
|
import { SEAC_ANALYSIS_ENABLED, type1FontGlyphMapping } from "./fonts_utils.js";
|
||||||
|
import { warn } from "../shared/util.js";
|
||||||
|
|
||||||
|
var CFFFont = (function CFFFontClosure() {
|
||||||
|
// eslint-disable-next-line no-shadow
|
||||||
|
function CFFFont(file, properties) {
|
||||||
|
this.properties = properties;
|
||||||
|
|
||||||
|
var parser = new CFFParser(file, properties, SEAC_ANALYSIS_ENABLED);
|
||||||
|
this.cff = parser.parse();
|
||||||
|
this.cff.duplicateFirstGlyph();
|
||||||
|
var compiler = new CFFCompiler(this.cff);
|
||||||
|
this.seacs = this.cff.seacs;
|
||||||
|
try {
|
||||||
|
this.data = compiler.compile();
|
||||||
|
} catch (e) {
|
||||||
|
warn("Failed to compile font " + properties.loadedName);
|
||||||
|
// There may have just been an issue with the compiler, set the data
|
||||||
|
// anyway and hope the font loaded.
|
||||||
|
this.data = file;
|
||||||
|
}
|
||||||
|
this._createBuiltInEncoding();
|
||||||
|
}
|
||||||
|
|
||||||
|
CFFFont.prototype = {
|
||||||
|
get numGlyphs() {
|
||||||
|
return this.cff.charStrings.count;
|
||||||
|
},
|
||||||
|
getCharset: function CFFFont_getCharset() {
|
||||||
|
return this.cff.charset.charset;
|
||||||
|
},
|
||||||
|
getGlyphMapping: function CFFFont_getGlyphMapping() {
|
||||||
|
var cff = this.cff;
|
||||||
|
var properties = this.properties;
|
||||||
|
var charsets = cff.charset.charset;
|
||||||
|
var charCodeToGlyphId;
|
||||||
|
var glyphId;
|
||||||
|
|
||||||
|
if (properties.composite) {
|
||||||
|
charCodeToGlyphId = Object.create(null);
|
||||||
|
let charCode;
|
||||||
|
if (cff.isCIDFont) {
|
||||||
|
// If the font is actually a CID font then we should use the charset
|
||||||
|
// to map CIDs to GIDs.
|
||||||
|
for (glyphId = 0; glyphId < charsets.length; glyphId++) {
|
||||||
|
var cid = charsets[glyphId];
|
||||||
|
charCode = properties.cMap.charCodeOf(cid);
|
||||||
|
charCodeToGlyphId[charCode] = glyphId;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// If it is NOT actually a CID font then CIDs should be mapped
|
||||||
|
// directly to GIDs.
|
||||||
|
for (glyphId = 0; glyphId < cff.charStrings.count; glyphId++) {
|
||||||
|
charCode = properties.cMap.charCodeOf(glyphId);
|
||||||
|
charCodeToGlyphId[charCode] = glyphId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return charCodeToGlyphId;
|
||||||
|
}
|
||||||
|
|
||||||
|
var encoding = cff.encoding ? cff.encoding.encoding : null;
|
||||||
|
charCodeToGlyphId = type1FontGlyphMapping(properties, encoding, charsets);
|
||||||
|
return charCodeToGlyphId;
|
||||||
|
},
|
||||||
|
hasGlyphId: function CFFFont_hasGlyphID(id) {
|
||||||
|
return this.cff.hasGlyphId(id);
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
_createBuiltInEncoding() {
|
||||||
|
const { charset, encoding } = this.cff;
|
||||||
|
if (!charset || !encoding) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const charsets = charset.charset,
|
||||||
|
encodings = encoding.encoding;
|
||||||
|
const map = [];
|
||||||
|
|
||||||
|
for (const charCode in encodings) {
|
||||||
|
const glyphId = encodings[charCode];
|
||||||
|
if (glyphId >= 0) {
|
||||||
|
const glyphName = charsets[glyphId];
|
||||||
|
if (glyphName) {
|
||||||
|
map[charCode] = glyphName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (map.length > 0) {
|
||||||
|
this.properties.builtInEncoding = map;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
return CFFFont;
|
||||||
|
})();
|
||||||
|
|
||||||
|
export { CFFFont };
|
@ -72,6 +72,7 @@ import {
|
|||||||
MissingDataException,
|
MissingDataException,
|
||||||
readUint32,
|
readUint32,
|
||||||
} from "./core_utils.js";
|
} from "./core_utils.js";
|
||||||
|
import { CFFFont } from "./cff_font.js";
|
||||||
import { FontRendererFactory } from "./font_renderer.js";
|
import { FontRendererFactory } from "./font_renderer.js";
|
||||||
import { IdentityCMap } from "./cmap.js";
|
import { IdentityCMap } from "./cmap.js";
|
||||||
import { OpenTypeFileBuilder } from "./opentype_file_builder.js";
|
import { OpenTypeFileBuilder } from "./opentype_file_builder.js";
|
||||||
@ -3592,99 +3593,4 @@ var Type1Font = (function Type1FontClosure() {
|
|||||||
return Type1Font;
|
return Type1Font;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
var CFFFont = (function CFFFontClosure() {
|
|
||||||
// eslint-disable-next-line no-shadow
|
|
||||||
function CFFFont(file, properties) {
|
|
||||||
this.properties = properties;
|
|
||||||
|
|
||||||
var parser = new CFFParser(file, properties, SEAC_ANALYSIS_ENABLED);
|
|
||||||
this.cff = parser.parse();
|
|
||||||
this.cff.duplicateFirstGlyph();
|
|
||||||
var compiler = new CFFCompiler(this.cff);
|
|
||||||
this.seacs = this.cff.seacs;
|
|
||||||
try {
|
|
||||||
this.data = compiler.compile();
|
|
||||||
} catch (e) {
|
|
||||||
warn("Failed to compile font " + properties.loadedName);
|
|
||||||
// There may have just been an issue with the compiler, set the data
|
|
||||||
// anyway and hope the font loaded.
|
|
||||||
this.data = file;
|
|
||||||
}
|
|
||||||
this._createBuiltInEncoding();
|
|
||||||
}
|
|
||||||
|
|
||||||
CFFFont.prototype = {
|
|
||||||
get numGlyphs() {
|
|
||||||
return this.cff.charStrings.count;
|
|
||||||
},
|
|
||||||
getCharset: function CFFFont_getCharset() {
|
|
||||||
return this.cff.charset.charset;
|
|
||||||
},
|
|
||||||
getGlyphMapping: function CFFFont_getGlyphMapping() {
|
|
||||||
var cff = this.cff;
|
|
||||||
var properties = this.properties;
|
|
||||||
var charsets = cff.charset.charset;
|
|
||||||
var charCodeToGlyphId;
|
|
||||||
var glyphId;
|
|
||||||
|
|
||||||
if (properties.composite) {
|
|
||||||
charCodeToGlyphId = Object.create(null);
|
|
||||||
let charCode;
|
|
||||||
if (cff.isCIDFont) {
|
|
||||||
// If the font is actually a CID font then we should use the charset
|
|
||||||
// to map CIDs to GIDs.
|
|
||||||
for (glyphId = 0; glyphId < charsets.length; glyphId++) {
|
|
||||||
var cid = charsets[glyphId];
|
|
||||||
charCode = properties.cMap.charCodeOf(cid);
|
|
||||||
charCodeToGlyphId[charCode] = glyphId;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// If it is NOT actually a CID font then CIDs should be mapped
|
|
||||||
// directly to GIDs.
|
|
||||||
for (glyphId = 0; glyphId < cff.charStrings.count; glyphId++) {
|
|
||||||
charCode = properties.cMap.charCodeOf(glyphId);
|
|
||||||
charCodeToGlyphId[charCode] = glyphId;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return charCodeToGlyphId;
|
|
||||||
}
|
|
||||||
|
|
||||||
var encoding = cff.encoding ? cff.encoding.encoding : null;
|
|
||||||
charCodeToGlyphId = type1FontGlyphMapping(properties, encoding, charsets);
|
|
||||||
return charCodeToGlyphId;
|
|
||||||
},
|
|
||||||
hasGlyphId: function CFFFont_hasGlyphID(id) {
|
|
||||||
return this.cff.hasGlyphId(id);
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
_createBuiltInEncoding() {
|
|
||||||
const { charset, encoding } = this.cff;
|
|
||||||
if (!charset || !encoding) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const charsets = charset.charset,
|
|
||||||
encodings = encoding.encoding;
|
|
||||||
const map = [];
|
|
||||||
|
|
||||||
for (const charCode in encodings) {
|
|
||||||
const glyphId = encodings[charCode];
|
|
||||||
if (glyphId >= 0) {
|
|
||||||
const glyphName = charsets[glyphId];
|
|
||||||
if (glyphName) {
|
|
||||||
map[charCode] = glyphName;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (map.length > 0) {
|
|
||||||
this.properties.builtInEncoding = map;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
return CFFFont;
|
|
||||||
})();
|
|
||||||
|
|
||||||
export { ErrorFont, Font };
|
export { ErrorFont, Font };
|
||||||
|
Loading…
x
Reference in New Issue
Block a user