Merge pull request #13327 from Snuffleupagus/split-fonts
Split the functionality in `src/core/fonts.js` into multiple files, and use standard classes
This commit is contained in:
commit
afb8c4fd25
113
src/core/cff_font.js
Normal file
113
src/core/cff_font.js
Normal file
@ -0,0 +1,113 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
import { CFFCompiler, CFFParser } from "./cff_parser.js";
|
||||
import { SEAC_ANALYSIS_ENABLED, type1FontGlyphMapping } from "./fonts_utils.js";
|
||||
import { warn } from "../shared/util.js";
|
||||
|
||||
class CFFFont {
|
||||
constructor(file, properties) {
|
||||
this.properties = properties;
|
||||
|
||||
const parser = new CFFParser(file, properties, SEAC_ANALYSIS_ENABLED);
|
||||
this.cff = parser.parse();
|
||||
this.cff.duplicateFirstGlyph();
|
||||
const 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();
|
||||
}
|
||||
|
||||
get numGlyphs() {
|
||||
return this.cff.charStrings.count;
|
||||
}
|
||||
|
||||
getCharset() {
|
||||
return this.cff.charset.charset;
|
||||
}
|
||||
|
||||
getGlyphMapping() {
|
||||
const cff = this.cff;
|
||||
const properties = this.properties;
|
||||
const charsets = cff.charset.charset;
|
||||
let charCodeToGlyphId;
|
||||
let 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++) {
|
||||
const 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;
|
||||
}
|
||||
|
||||
const encoding = cff.encoding ? cff.encoding.encoding : null;
|
||||
charCodeToGlyphId = type1FontGlyphMapping(properties, encoding, charsets);
|
||||
return charCodeToGlyphId;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export { CFFFont };
|
@ -47,14 +47,8 @@ import {
|
||||
Ref,
|
||||
RefSet,
|
||||
} from "./primitives.js";
|
||||
import {
|
||||
ErrorFont,
|
||||
Font,
|
||||
FontFlags,
|
||||
getFontType,
|
||||
IdentityToUnicodeMap,
|
||||
ToUnicodeMap,
|
||||
} from "./fonts.js";
|
||||
import { ErrorFont, Font } from "./fonts.js";
|
||||
import { FontFlags, getFontType } from "./fonts_utils.js";
|
||||
import {
|
||||
getEncoding,
|
||||
MacRomanEncoding,
|
||||
@ -74,6 +68,7 @@ import {
|
||||
getSymbolsFonts,
|
||||
} from "./standard_fonts.js";
|
||||
import { getTilingPatternIR, Pattern } from "./pattern.js";
|
||||
import { IdentityToUnicodeMap, ToUnicodeMap } from "./to_unicode_map.js";
|
||||
import { isPDFFunction, PDFFunctionFactory } from "./function.js";
|
||||
import { Lexer, Parser } from "./parser.js";
|
||||
import {
|
||||
|
6176
src/core/fonts.js
6176
src/core/fonts.js
File diff suppressed because it is too large
Load Diff
203
src/core/fonts_utils.js
Normal file
203
src/core/fonts_utils.js
Normal file
@ -0,0 +1,203 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
import { FontType, info } from "../shared/util.js";
|
||||
import { getEncoding, StandardEncoding } from "./encodings.js";
|
||||
import { getGlyphsUnicode } from "./glyphlist.js";
|
||||
import { getUnicodeForGlyph } from "./unicode.js";
|
||||
|
||||
// Accented characters have issues on Windows and Linux. When this flag is
|
||||
// enabled glyphs that use seac and seac style endchar operators are truncated
|
||||
// and we instead just store the glyph id's of the base glyph and its accent to
|
||||
// be drawn individually.
|
||||
// Linux (freetype) requires that when a seac style endchar is used
|
||||
// that the charset must be a predefined one, however we build a
|
||||
// custom one. Windows just refuses to draw glyphs with seac operators.
|
||||
const SEAC_ANALYSIS_ENABLED = true;
|
||||
|
||||
const FontFlags = {
|
||||
FixedPitch: 1,
|
||||
Serif: 2,
|
||||
Symbolic: 4,
|
||||
Script: 8,
|
||||
Nonsymbolic: 32,
|
||||
Italic: 64,
|
||||
AllCap: 65536,
|
||||
SmallCap: 131072,
|
||||
ForceBold: 262144,
|
||||
};
|
||||
|
||||
// prettier-ignore
|
||||
const MacStandardGlyphOrdering = [
|
||||
".notdef", ".null", "nonmarkingreturn", "space", "exclam", "quotedbl",
|
||||
"numbersign", "dollar", "percent", "ampersand", "quotesingle", "parenleft",
|
||||
"parenright", "asterisk", "plus", "comma", "hyphen", "period", "slash",
|
||||
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
|
||||
"nine", "colon", "semicolon", "less", "equal", "greater", "question", "at",
|
||||
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
|
||||
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "bracketleft",
|
||||
"backslash", "bracketright", "asciicircum", "underscore", "grave", "a", "b",
|
||||
"c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q",
|
||||
"r", "s", "t", "u", "v", "w", "x", "y", "z", "braceleft", "bar", "braceright",
|
||||
"asciitilde", "Adieresis", "Aring", "Ccedilla", "Eacute", "Ntilde",
|
||||
"Odieresis", "Udieresis", "aacute", "agrave", "acircumflex", "adieresis",
|
||||
"atilde", "aring", "ccedilla", "eacute", "egrave", "ecircumflex", "edieresis",
|
||||
"iacute", "igrave", "icircumflex", "idieresis", "ntilde", "oacute", "ograve",
|
||||
"ocircumflex", "odieresis", "otilde", "uacute", "ugrave", "ucircumflex",
|
||||
"udieresis", "dagger", "degree", "cent", "sterling", "section", "bullet",
|
||||
"paragraph", "germandbls", "registered", "copyright", "trademark", "acute",
|
||||
"dieresis", "notequal", "AE", "Oslash", "infinity", "plusminus", "lessequal",
|
||||
"greaterequal", "yen", "mu", "partialdiff", "summation", "product", "pi",
|
||||
"integral", "ordfeminine", "ordmasculine", "Omega", "ae", "oslash",
|
||||
"questiondown", "exclamdown", "logicalnot", "radical", "florin",
|
||||
"approxequal", "Delta", "guillemotleft", "guillemotright", "ellipsis",
|
||||
"nonbreakingspace", "Agrave", "Atilde", "Otilde", "OE", "oe", "endash",
|
||||
"emdash", "quotedblleft", "quotedblright", "quoteleft", "quoteright",
|
||||
"divide", "lozenge", "ydieresis", "Ydieresis", "fraction", "currency",
|
||||
"guilsinglleft", "guilsinglright", "fi", "fl", "daggerdbl", "periodcentered",
|
||||
"quotesinglbase", "quotedblbase", "perthousand", "Acircumflex",
|
||||
"Ecircumflex", "Aacute", "Edieresis", "Egrave", "Iacute", "Icircumflex",
|
||||
"Idieresis", "Igrave", "Oacute", "Ocircumflex", "apple", "Ograve", "Uacute",
|
||||
"Ucircumflex", "Ugrave", "dotlessi", "circumflex", "tilde", "macron",
|
||||
"breve", "dotaccent", "ring", "cedilla", "hungarumlaut", "ogonek", "caron",
|
||||
"Lslash", "lslash", "Scaron", "scaron", "Zcaron", "zcaron", "brokenbar",
|
||||
"Eth", "eth", "Yacute", "yacute", "Thorn", "thorn", "minus", "multiply",
|
||||
"onesuperior", "twosuperior", "threesuperior", "onehalf", "onequarter",
|
||||
"threequarters", "franc", "Gbreve", "gbreve", "Idotaccent", "Scedilla",
|
||||
"scedilla", "Cacute", "cacute", "Ccaron", "ccaron", "dcroat"];
|
||||
|
||||
function getFontType(type, subtype) {
|
||||
switch (type) {
|
||||
case "Type1":
|
||||
return subtype === "Type1C" ? FontType.TYPE1C : FontType.TYPE1;
|
||||
case "CIDFontType0":
|
||||
return subtype === "CIDFontType0C"
|
||||
? FontType.CIDFONTTYPE0C
|
||||
: FontType.CIDFONTTYPE0;
|
||||
case "OpenType":
|
||||
return FontType.OPENTYPE;
|
||||
case "TrueType":
|
||||
return FontType.TRUETYPE;
|
||||
case "CIDFontType2":
|
||||
return FontType.CIDFONTTYPE2;
|
||||
case "MMType1":
|
||||
return FontType.MMTYPE1;
|
||||
case "Type0":
|
||||
return FontType.TYPE0;
|
||||
default:
|
||||
return FontType.UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
// Some bad PDF generators, e.g. Scribus PDF, include glyph names
|
||||
// in a 'uniXXXX' format -- attempting to recover proper ones.
|
||||
function recoverGlyphName(name, glyphsUnicodeMap) {
|
||||
if (glyphsUnicodeMap[name] !== undefined) {
|
||||
return name;
|
||||
}
|
||||
// The glyph name is non-standard, trying to recover.
|
||||
const unicode = getUnicodeForGlyph(name, glyphsUnicodeMap);
|
||||
if (unicode !== -1) {
|
||||
for (const key in glyphsUnicodeMap) {
|
||||
if (glyphsUnicodeMap[key] === unicode) {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
}
|
||||
info("Unable to recover a standard glyph name for: " + name);
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shared logic for building a char code to glyph id mapping for Type1 and
|
||||
* simple CFF fonts. See section 9.6.6.2 of the spec.
|
||||
* @param {Object} properties Font properties object.
|
||||
* @param {Object} builtInEncoding The encoding contained within the actual font
|
||||
* data.
|
||||
* @param {Array} glyphNames Array of glyph names where the index is the
|
||||
* glyph ID.
|
||||
* @returns {Object} A char code to glyph ID map.
|
||||
*/
|
||||
function type1FontGlyphMapping(properties, builtInEncoding, glyphNames) {
|
||||
const charCodeToGlyphId = Object.create(null);
|
||||
let glyphId, charCode, baseEncoding;
|
||||
const isSymbolicFont = !!(properties.flags & FontFlags.Symbolic);
|
||||
|
||||
if (properties.baseEncodingName) {
|
||||
// If a valid base encoding name was used, the mapping is initialized with
|
||||
// that.
|
||||
baseEncoding = getEncoding(properties.baseEncodingName);
|
||||
for (charCode = 0; charCode < baseEncoding.length; charCode++) {
|
||||
glyphId = glyphNames.indexOf(baseEncoding[charCode]);
|
||||
if (glyphId >= 0) {
|
||||
charCodeToGlyphId[charCode] = glyphId;
|
||||
} else {
|
||||
charCodeToGlyphId[charCode] = 0; // notdef
|
||||
}
|
||||
}
|
||||
} else if (isSymbolicFont) {
|
||||
// For a symbolic font the encoding should be the fonts built-in encoding.
|
||||
for (charCode in builtInEncoding) {
|
||||
charCodeToGlyphId[charCode] = builtInEncoding[charCode];
|
||||
}
|
||||
} else {
|
||||
// For non-symbolic fonts that don't have a base encoding the standard
|
||||
// encoding should be used.
|
||||
baseEncoding = StandardEncoding;
|
||||
for (charCode = 0; charCode < baseEncoding.length; charCode++) {
|
||||
glyphId = glyphNames.indexOf(baseEncoding[charCode]);
|
||||
if (glyphId >= 0) {
|
||||
charCodeToGlyphId[charCode] = glyphId;
|
||||
} else {
|
||||
charCodeToGlyphId[charCode] = 0; // notdef
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Lastly, merge in the differences.
|
||||
const differences = properties.differences;
|
||||
let glyphsUnicodeMap;
|
||||
if (differences) {
|
||||
for (charCode in differences) {
|
||||
const glyphName = differences[charCode];
|
||||
glyphId = glyphNames.indexOf(glyphName);
|
||||
|
||||
if (glyphId === -1) {
|
||||
if (!glyphsUnicodeMap) {
|
||||
glyphsUnicodeMap = getGlyphsUnicode();
|
||||
}
|
||||
const standardGlyphName = recoverGlyphName(glyphName, glyphsUnicodeMap);
|
||||
if (standardGlyphName !== glyphName) {
|
||||
glyphId = glyphNames.indexOf(standardGlyphName);
|
||||
}
|
||||
}
|
||||
if (glyphId >= 0) {
|
||||
charCodeToGlyphId[charCode] = glyphId;
|
||||
} else {
|
||||
charCodeToGlyphId[charCode] = 0; // notdef
|
||||
}
|
||||
}
|
||||
}
|
||||
return charCodeToGlyphId;
|
||||
}
|
||||
|
||||
export {
|
||||
FontFlags,
|
||||
getFontType,
|
||||
MacStandardGlyphOrdering,
|
||||
recoverGlyphName,
|
||||
SEAC_ANALYSIS_ENABLED,
|
||||
type1FontGlyphMapping,
|
||||
};
|
154
src/core/opentype_file_builder.js
Normal file
154
src/core/opentype_file_builder.js
Normal file
@ -0,0 +1,154 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
import { readUint32 } from "./core_utils.js";
|
||||
import { string32 } from "../shared/util.js";
|
||||
|
||||
function writeInt16(dest, offset, num) {
|
||||
dest[offset] = (num >> 8) & 0xff;
|
||||
dest[offset + 1] = num & 0xff;
|
||||
}
|
||||
|
||||
function writeInt32(dest, offset, num) {
|
||||
dest[offset] = (num >> 24) & 0xff;
|
||||
dest[offset + 1] = (num >> 16) & 0xff;
|
||||
dest[offset + 2] = (num >> 8) & 0xff;
|
||||
dest[offset + 3] = num & 0xff;
|
||||
}
|
||||
|
||||
function writeData(dest, offset, data) {
|
||||
if (data instanceof Uint8Array) {
|
||||
dest.set(data, offset);
|
||||
} else if (typeof data === "string") {
|
||||
for (let i = 0, ii = data.length; i < ii; i++) {
|
||||
dest[offset++] = data.charCodeAt(i) & 0xff;
|
||||
}
|
||||
} else {
|
||||
// treating everything else as array
|
||||
for (let i = 0, ii = data.length; i < ii; i++) {
|
||||
dest[offset++] = data[i] & 0xff;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const OTF_HEADER_SIZE = 12;
|
||||
const OTF_TABLE_ENTRY_SIZE = 16;
|
||||
|
||||
class OpenTypeFileBuilder {
|
||||
constructor(sfnt) {
|
||||
this.sfnt = sfnt;
|
||||
this.tables = Object.create(null);
|
||||
}
|
||||
|
||||
static getSearchParams(entriesCount, entrySize) {
|
||||
let maxPower2 = 1,
|
||||
log2 = 0;
|
||||
while ((maxPower2 ^ entriesCount) > maxPower2) {
|
||||
maxPower2 <<= 1;
|
||||
log2++;
|
||||
}
|
||||
const searchRange = maxPower2 * entrySize;
|
||||
return {
|
||||
range: searchRange,
|
||||
entry: log2,
|
||||
rangeShift: entrySize * entriesCount - searchRange,
|
||||
};
|
||||
}
|
||||
|
||||
toArray() {
|
||||
let sfnt = this.sfnt;
|
||||
|
||||
// Tables needs to be written by ascendant alphabetic order
|
||||
const tables = this.tables;
|
||||
const tablesNames = Object.keys(tables);
|
||||
tablesNames.sort();
|
||||
const numTables = tablesNames.length;
|
||||
|
||||
let i, j, jj, table, tableName;
|
||||
// layout the tables data
|
||||
let offset = OTF_HEADER_SIZE + numTables * OTF_TABLE_ENTRY_SIZE;
|
||||
const tableOffsets = [offset];
|
||||
for (i = 0; i < numTables; i++) {
|
||||
table = tables[tablesNames[i]];
|
||||
const paddedLength = ((table.length + 3) & ~3) >>> 0;
|
||||
offset += paddedLength;
|
||||
tableOffsets.push(offset);
|
||||
}
|
||||
|
||||
const file = new Uint8Array(offset);
|
||||
// write the table data first (mostly for checksum)
|
||||
for (i = 0; i < numTables; i++) {
|
||||
table = tables[tablesNames[i]];
|
||||
writeData(file, tableOffsets[i], table);
|
||||
}
|
||||
|
||||
// sfnt version (4 bytes)
|
||||
if (sfnt === "true") {
|
||||
// Windows hates the Mac TrueType sfnt version number
|
||||
sfnt = string32(0x00010000);
|
||||
}
|
||||
file[0] = sfnt.charCodeAt(0) & 0xff;
|
||||
file[1] = sfnt.charCodeAt(1) & 0xff;
|
||||
file[2] = sfnt.charCodeAt(2) & 0xff;
|
||||
file[3] = sfnt.charCodeAt(3) & 0xff;
|
||||
|
||||
// numTables (2 bytes)
|
||||
writeInt16(file, 4, numTables);
|
||||
|
||||
const searchParams = OpenTypeFileBuilder.getSearchParams(numTables, 16);
|
||||
|
||||
// searchRange (2 bytes)
|
||||
writeInt16(file, 6, searchParams.range);
|
||||
// entrySelector (2 bytes)
|
||||
writeInt16(file, 8, searchParams.entry);
|
||||
// rangeShift (2 bytes)
|
||||
writeInt16(file, 10, searchParams.rangeShift);
|
||||
|
||||
offset = OTF_HEADER_SIZE;
|
||||
// writing table entries
|
||||
for (i = 0; i < numTables; i++) {
|
||||
tableName = tablesNames[i];
|
||||
file[offset] = tableName.charCodeAt(0) & 0xff;
|
||||
file[offset + 1] = tableName.charCodeAt(1) & 0xff;
|
||||
file[offset + 2] = tableName.charCodeAt(2) & 0xff;
|
||||
file[offset + 3] = tableName.charCodeAt(3) & 0xff;
|
||||
|
||||
// checksum
|
||||
let checksum = 0;
|
||||
for (j = tableOffsets[i], jj = tableOffsets[i + 1]; j < jj; j += 4) {
|
||||
const quad = readUint32(file, j);
|
||||
checksum = (checksum + quad) >>> 0;
|
||||
}
|
||||
writeInt32(file, offset + 4, checksum);
|
||||
|
||||
// offset
|
||||
writeInt32(file, offset + 8, tableOffsets[i]);
|
||||
// length
|
||||
writeInt32(file, offset + 12, tables[tableName].length);
|
||||
|
||||
offset += OTF_TABLE_ENTRY_SIZE;
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
addTable(tag, data) {
|
||||
if (tag in this.tables) {
|
||||
throw new Error("Table " + tag + " already exists");
|
||||
}
|
||||
this.tables[tag] = data;
|
||||
}
|
||||
}
|
||||
|
||||
export { OpenTypeFileBuilder };
|
103
src/core/to_unicode_map.js
Normal file
103
src/core/to_unicode_map.js
Normal file
@ -0,0 +1,103 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
import { unreachable } from "../shared/util.js";
|
||||
|
||||
class ToUnicodeMap {
|
||||
constructor(cmap = []) {
|
||||
// The elements of this._map can be integers or strings, depending on how
|
||||
// `cmap` was created.
|
||||
this._map = cmap;
|
||||
}
|
||||
|
||||
get length() {
|
||||
return this._map.length;
|
||||
}
|
||||
|
||||
forEach(callback) {
|
||||
for (const charCode in this._map) {
|
||||
callback(charCode, this._map[charCode].charCodeAt(0));
|
||||
}
|
||||
}
|
||||
|
||||
has(i) {
|
||||
return this._map[i] !== undefined;
|
||||
}
|
||||
|
||||
get(i) {
|
||||
return this._map[i];
|
||||
}
|
||||
|
||||
charCodeOf(value) {
|
||||
// `Array.prototype.indexOf` is *extremely* inefficient for arrays which
|
||||
// are both very sparse and very large (see issue8372.pdf).
|
||||
const map = this._map;
|
||||
if (map.length <= 0x10000) {
|
||||
return map.indexOf(value);
|
||||
}
|
||||
for (const charCode in map) {
|
||||
if (map[charCode] === value) {
|
||||
return charCode | 0;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
amend(map) {
|
||||
for (const charCode in map) {
|
||||
this._map[charCode] = map[charCode];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class IdentityToUnicodeMap {
|
||||
constructor(firstChar, lastChar) {
|
||||
this.firstChar = firstChar;
|
||||
this.lastChar = lastChar;
|
||||
}
|
||||
|
||||
get length() {
|
||||
return this.lastChar + 1 - this.firstChar;
|
||||
}
|
||||
|
||||
forEach(callback) {
|
||||
for (let i = this.firstChar, ii = this.lastChar; i <= ii; i++) {
|
||||
callback(i, i);
|
||||
}
|
||||
}
|
||||
|
||||
has(i) {
|
||||
return this.firstChar <= i && i <= this.lastChar;
|
||||
}
|
||||
|
||||
get(i) {
|
||||
if (this.firstChar <= i && i <= this.lastChar) {
|
||||
return String.fromCharCode(i);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
charCodeOf(v) {
|
||||
return Number.isInteger(v) && v >= this.firstChar && v <= this.lastChar
|
||||
? v
|
||||
: -1;
|
||||
}
|
||||
|
||||
amend(map) {
|
||||
unreachable("Should not call amend()");
|
||||
}
|
||||
}
|
||||
|
||||
export { IdentityToUnicodeMap, ToUnicodeMap };
|
421
src/core/type1_font.js
Normal file
421
src/core/type1_font.js
Normal file
@ -0,0 +1,421 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
import {
|
||||
CFF,
|
||||
CFFCharset,
|
||||
CFFCompiler,
|
||||
CFFHeader,
|
||||
CFFIndex,
|
||||
CFFPrivateDict,
|
||||
CFFStandardStrings,
|
||||
CFFStrings,
|
||||
CFFTopDict,
|
||||
} from "./cff_parser.js";
|
||||
import { SEAC_ANALYSIS_ENABLED, type1FontGlyphMapping } from "./fonts_utils.js";
|
||||
import { isWhiteSpace } from "./core_utils.js";
|
||||
import { Stream } from "./stream.js";
|
||||
import { Type1Parser } from "./type1_parser.js";
|
||||
import { warn } from "../shared/util.js";
|
||||
|
||||
function findBlock(streamBytes, signature, startIndex) {
|
||||
const streamBytesLength = streamBytes.length;
|
||||
const signatureLength = signature.length;
|
||||
const scanLength = streamBytesLength - signatureLength;
|
||||
|
||||
let i = startIndex,
|
||||
found = false;
|
||||
while (i < scanLength) {
|
||||
let j = 0;
|
||||
while (j < signatureLength && streamBytes[i + j] === signature[j]) {
|
||||
j++;
|
||||
}
|
||||
if (j >= signatureLength) {
|
||||
// `signature` found, skip over whitespace.
|
||||
i += j;
|
||||
while (i < streamBytesLength && isWhiteSpace(streamBytes[i])) {
|
||||
i++;
|
||||
}
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return {
|
||||
found,
|
||||
length: i,
|
||||
};
|
||||
}
|
||||
|
||||
function getHeaderBlock(stream, suggestedLength) {
|
||||
const EEXEC_SIGNATURE = [0x65, 0x65, 0x78, 0x65, 0x63];
|
||||
|
||||
const streamStartPos = stream.pos; // Save the initial stream position.
|
||||
let headerBytes, headerBytesLength, block;
|
||||
try {
|
||||
headerBytes = stream.getBytes(suggestedLength);
|
||||
headerBytesLength = headerBytes.length;
|
||||
} catch (ex) {
|
||||
// Ignore errors if the `suggestedLength` is huge enough that a Uint8Array
|
||||
// cannot hold the result of `getBytes`, and fallback to simply checking
|
||||
// the entire stream (fixes issue3928.pdf).
|
||||
}
|
||||
|
||||
if (headerBytesLength === suggestedLength) {
|
||||
// Most of the time `suggestedLength` is correct, so to speed things up we
|
||||
// initially only check the last few bytes to see if the header was found.
|
||||
// Otherwise we (potentially) check the entire stream to prevent errors in
|
||||
// `Type1Parser` (fixes issue5686.pdf).
|
||||
block = findBlock(
|
||||
headerBytes,
|
||||
EEXEC_SIGNATURE,
|
||||
suggestedLength - 2 * EEXEC_SIGNATURE.length
|
||||
);
|
||||
|
||||
if (block.found && block.length === suggestedLength) {
|
||||
return {
|
||||
stream: new Stream(headerBytes),
|
||||
length: suggestedLength,
|
||||
};
|
||||
}
|
||||
}
|
||||
warn('Invalid "Length1" property in Type1 font -- trying to recover.');
|
||||
stream.pos = streamStartPos; // Reset the stream position.
|
||||
|
||||
const SCAN_BLOCK_LENGTH = 2048;
|
||||
let actualLength;
|
||||
while (true) {
|
||||
const scanBytes = stream.peekBytes(SCAN_BLOCK_LENGTH);
|
||||
block = findBlock(scanBytes, EEXEC_SIGNATURE, 0);
|
||||
|
||||
if (block.length === 0) {
|
||||
break;
|
||||
}
|
||||
stream.pos += block.length; // Update the stream position.
|
||||
|
||||
if (block.found) {
|
||||
actualLength = stream.pos - streamStartPos;
|
||||
break;
|
||||
}
|
||||
}
|
||||
stream.pos = streamStartPos; // Reset the stream position.
|
||||
|
||||
if (actualLength) {
|
||||
return {
|
||||
stream: new Stream(stream.getBytes(actualLength)),
|
||||
length: actualLength,
|
||||
};
|
||||
}
|
||||
warn('Unable to recover "Length1" property in Type1 font -- using as is.');
|
||||
return {
|
||||
stream: new Stream(stream.getBytes(suggestedLength)),
|
||||
length: suggestedLength,
|
||||
};
|
||||
}
|
||||
|
||||
function getEexecBlock(stream, suggestedLength) {
|
||||
// We should ideally parse the eexec block to ensure that `suggestedLength`
|
||||
// is correct, so we don't truncate the block data if it's too small.
|
||||
// However, this would also require checking if the fixed-content portion
|
||||
// exists (using the 'Length3' property), and ensuring that it's valid.
|
||||
//
|
||||
// Given that `suggestedLength` almost always is correct, all the validation
|
||||
// would require a great deal of unnecessary parsing for most fonts.
|
||||
// To save time, we always fetch the entire stream instead, which also avoid
|
||||
// issues if `suggestedLength` is huge (see comment in `getHeaderBlock`).
|
||||
//
|
||||
// NOTE: This means that the function can include the fixed-content portion
|
||||
// in the returned eexec block. In practice this does *not* seem to matter,
|
||||
// since `Type1Parser_extractFontProgram` will skip over any non-commands.
|
||||
const eexecBytes = stream.getBytes();
|
||||
return {
|
||||
stream: new Stream(eexecBytes),
|
||||
length: eexecBytes.length,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Type1Font is also a CIDFontType0.
|
||||
*/
|
||||
class Type1Font {
|
||||
constructor(name, file, properties) {
|
||||
// Some bad generators embed pfb file as is, we have to strip 6-byte header.
|
||||
// Also, length1 and length2 might be off by 6 bytes as well.
|
||||
// http://www.math.ubc.ca/~cass/piscript/type1.pdf
|
||||
const PFB_HEADER_SIZE = 6;
|
||||
let headerBlockLength = properties.length1;
|
||||
let eexecBlockLength = properties.length2;
|
||||
let pfbHeader = file.peekBytes(PFB_HEADER_SIZE);
|
||||
const pfbHeaderPresent = pfbHeader[0] === 0x80 && pfbHeader[1] === 0x01;
|
||||
if (pfbHeaderPresent) {
|
||||
file.skip(PFB_HEADER_SIZE);
|
||||
headerBlockLength =
|
||||
(pfbHeader[5] << 24) |
|
||||
(pfbHeader[4] << 16) |
|
||||
(pfbHeader[3] << 8) |
|
||||
pfbHeader[2];
|
||||
}
|
||||
|
||||
// Get the data block containing glyphs and subrs information
|
||||
const headerBlock = getHeaderBlock(file, headerBlockLength);
|
||||
const headerBlockParser = new Type1Parser(
|
||||
headerBlock.stream,
|
||||
false,
|
||||
SEAC_ANALYSIS_ENABLED
|
||||
);
|
||||
headerBlockParser.extractFontHeader(properties);
|
||||
|
||||
if (pfbHeaderPresent) {
|
||||
pfbHeader = file.getBytes(PFB_HEADER_SIZE);
|
||||
eexecBlockLength =
|
||||
(pfbHeader[5] << 24) |
|
||||
(pfbHeader[4] << 16) |
|
||||
(pfbHeader[3] << 8) |
|
||||
pfbHeader[2];
|
||||
}
|
||||
|
||||
// Decrypt the data blocks and retrieve it's content
|
||||
const eexecBlock = getEexecBlock(file, eexecBlockLength);
|
||||
const eexecBlockParser = new Type1Parser(
|
||||
eexecBlock.stream,
|
||||
true,
|
||||
SEAC_ANALYSIS_ENABLED
|
||||
);
|
||||
const data = eexecBlockParser.extractFontProgram(properties);
|
||||
for (const key in data.properties) {
|
||||
properties[key] = data.properties[key];
|
||||
}
|
||||
|
||||
const charstrings = data.charstrings;
|
||||
const type2Charstrings = this.getType2Charstrings(charstrings);
|
||||
const subrs = this.getType2Subrs(data.subrs);
|
||||
|
||||
this.charstrings = charstrings;
|
||||
this.data = this.wrap(
|
||||
name,
|
||||
type2Charstrings,
|
||||
this.charstrings,
|
||||
subrs,
|
||||
properties
|
||||
);
|
||||
this.seacs = this.getSeacs(data.charstrings);
|
||||
}
|
||||
|
||||
get numGlyphs() {
|
||||
return this.charstrings.length + 1;
|
||||
}
|
||||
|
||||
getCharset() {
|
||||
const charset = [".notdef"];
|
||||
const charstrings = this.charstrings;
|
||||
for (let glyphId = 0; glyphId < charstrings.length; glyphId++) {
|
||||
charset.push(charstrings[glyphId].glyphName);
|
||||
}
|
||||
return charset;
|
||||
}
|
||||
|
||||
getGlyphMapping(properties) {
|
||||
const charstrings = this.charstrings;
|
||||
|
||||
if (properties.composite) {
|
||||
const charCodeToGlyphId = Object.create(null);
|
||||
// Map CIDs directly to GIDs.
|
||||
for (
|
||||
let glyphId = 0, charstringsLen = charstrings.length;
|
||||
glyphId < charstringsLen;
|
||||
glyphId++
|
||||
) {
|
||||
const charCode = properties.cMap.charCodeOf(glyphId);
|
||||
// Add 1 because glyph 0 is duplicated.
|
||||
charCodeToGlyphId[charCode] = glyphId + 1;
|
||||
}
|
||||
return charCodeToGlyphId;
|
||||
}
|
||||
|
||||
const glyphNames = [".notdef"];
|
||||
let builtInEncoding, glyphId;
|
||||
for (glyphId = 0; glyphId < charstrings.length; glyphId++) {
|
||||
glyphNames.push(charstrings[glyphId].glyphName);
|
||||
}
|
||||
const encoding = properties.builtInEncoding;
|
||||
if (encoding) {
|
||||
builtInEncoding = Object.create(null);
|
||||
for (const charCode in encoding) {
|
||||
glyphId = glyphNames.indexOf(encoding[charCode]);
|
||||
if (glyphId >= 0) {
|
||||
builtInEncoding[charCode] = glyphId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return type1FontGlyphMapping(properties, builtInEncoding, glyphNames);
|
||||
}
|
||||
|
||||
hasGlyphId(id) {
|
||||
if (id < 0 || id >= this.numGlyphs) {
|
||||
return false;
|
||||
}
|
||||
if (id === 0) {
|
||||
// notdef is always defined.
|
||||
return true;
|
||||
}
|
||||
const glyph = this.charstrings[id - 1];
|
||||
return glyph.charstring.length > 0;
|
||||
}
|
||||
|
||||
getSeacs(charstrings) {
|
||||
const seacMap = [];
|
||||
for (let i = 0, ii = charstrings.length; i < ii; i++) {
|
||||
const charstring = charstrings[i];
|
||||
if (charstring.seac) {
|
||||
// Offset by 1 for .notdef
|
||||
seacMap[i + 1] = charstring.seac;
|
||||
}
|
||||
}
|
||||
return seacMap;
|
||||
}
|
||||
|
||||
getType2Charstrings(type1Charstrings) {
|
||||
const type2Charstrings = [];
|
||||
for (let i = 0, ii = type1Charstrings.length; i < ii; i++) {
|
||||
type2Charstrings.push(type1Charstrings[i].charstring);
|
||||
}
|
||||
return type2Charstrings;
|
||||
}
|
||||
|
||||
getType2Subrs(type1Subrs) {
|
||||
let bias = 0;
|
||||
const count = type1Subrs.length;
|
||||
if (count < 1133) {
|
||||
bias = 107;
|
||||
} else if (count < 33769) {
|
||||
bias = 1131;
|
||||
} else {
|
||||
bias = 32768;
|
||||
}
|
||||
|
||||
// Add a bunch of empty subrs to deal with the Type2 bias
|
||||
const type2Subrs = [];
|
||||
let i;
|
||||
for (i = 0; i < bias; i++) {
|
||||
type2Subrs.push([0x0b]);
|
||||
}
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
type2Subrs.push(type1Subrs[i]);
|
||||
}
|
||||
|
||||
return type2Subrs;
|
||||
}
|
||||
|
||||
wrap(name, glyphs, charstrings, subrs, properties) {
|
||||
const cff = new CFF();
|
||||
cff.header = new CFFHeader(1, 0, 4, 4);
|
||||
|
||||
cff.names = [name];
|
||||
|
||||
const topDict = new CFFTopDict();
|
||||
// CFF strings IDs 0...390 are predefined names, so refering
|
||||
// to entries in our own String INDEX starts at SID 391.
|
||||
topDict.setByName("version", 391);
|
||||
topDict.setByName("Notice", 392);
|
||||
topDict.setByName("FullName", 393);
|
||||
topDict.setByName("FamilyName", 394);
|
||||
topDict.setByName("Weight", 395);
|
||||
topDict.setByName("Encoding", null); // placeholder
|
||||
topDict.setByName("FontMatrix", properties.fontMatrix);
|
||||
topDict.setByName("FontBBox", properties.bbox);
|
||||
topDict.setByName("charset", null); // placeholder
|
||||
topDict.setByName("CharStrings", null); // placeholder
|
||||
topDict.setByName("Private", null); // placeholder
|
||||
cff.topDict = topDict;
|
||||
|
||||
const strings = new CFFStrings();
|
||||
strings.add("Version 0.11"); // Version
|
||||
strings.add("See original notice"); // Notice
|
||||
strings.add(name); // FullName
|
||||
strings.add(name); // FamilyName
|
||||
strings.add("Medium"); // Weight
|
||||
cff.strings = strings;
|
||||
|
||||
cff.globalSubrIndex = new CFFIndex();
|
||||
|
||||
const count = glyphs.length;
|
||||
const charsetArray = [".notdef"];
|
||||
let i, ii;
|
||||
for (i = 0; i < count; i++) {
|
||||
const glyphName = charstrings[i].glyphName;
|
||||
const index = CFFStandardStrings.indexOf(glyphName);
|
||||
if (index === -1) {
|
||||
strings.add(glyphName);
|
||||
}
|
||||
charsetArray.push(glyphName);
|
||||
}
|
||||
cff.charset = new CFFCharset(false, 0, charsetArray);
|
||||
|
||||
const charStringsIndex = new CFFIndex();
|
||||
charStringsIndex.add([0x8b, 0x0e]); // .notdef
|
||||
for (i = 0; i < count; i++) {
|
||||
charStringsIndex.add(glyphs[i]);
|
||||
}
|
||||
cff.charStrings = charStringsIndex;
|
||||
|
||||
const privateDict = new CFFPrivateDict();
|
||||
privateDict.setByName("Subrs", null); // placeholder
|
||||
const fields = [
|
||||
"BlueValues",
|
||||
"OtherBlues",
|
||||
"FamilyBlues",
|
||||
"FamilyOtherBlues",
|
||||
"StemSnapH",
|
||||
"StemSnapV",
|
||||
"BlueShift",
|
||||
"BlueFuzz",
|
||||
"BlueScale",
|
||||
"LanguageGroup",
|
||||
"ExpansionFactor",
|
||||
"ForceBold",
|
||||
"StdHW",
|
||||
"StdVW",
|
||||
];
|
||||
for (i = 0, ii = fields.length; i < ii; i++) {
|
||||
const field = fields[i];
|
||||
if (!(field in properties.privateData)) {
|
||||
continue;
|
||||
}
|
||||
const value = properties.privateData[field];
|
||||
if (Array.isArray(value)) {
|
||||
// All of the private dictionary array data in CFF must be stored as
|
||||
// "delta-encoded" numbers.
|
||||
for (let j = value.length - 1; j > 0; j--) {
|
||||
value[j] -= value[j - 1]; // ... difference from previous value
|
||||
}
|
||||
}
|
||||
privateDict.setByName(field, value);
|
||||
}
|
||||
cff.topDict.privateDict = privateDict;
|
||||
|
||||
const subrIndex = new CFFIndex();
|
||||
for (i = 0, ii = subrs.length; i < ii; i++) {
|
||||
subrIndex.add(subrs[i]);
|
||||
}
|
||||
privateDict.subrsIndex = subrIndex;
|
||||
|
||||
const compiler = new CFFCompiler(cff);
|
||||
return compiler.compile();
|
||||
}
|
||||
}
|
||||
|
||||
export { Type1Font };
|
@ -1,8 +1,9 @@
|
||||
import { decodeFontData, ttx, verifyTtxOutput } from "./fontutils.js";
|
||||
import { Font, ToUnicodeMap } from "../../src/core/fonts.js";
|
||||
import { CMapFactory } from "../../src/core/cmap.js";
|
||||
import { Font } from "../../src/core/fonts.js";
|
||||
import { Name } from "../../src/core/primitives.js";
|
||||
import { Stream } from "../../src/core/stream.js";
|
||||
import { ToUnicodeMap } from "../../src/core/to_unicode_map.js";
|
||||
|
||||
describe("font_fpgm", function () {
|
||||
const font2324 = decodeFontData(
|
||||
|
@ -1,8 +1,9 @@
|
||||
import { decodeFontData, ttx, verifyTtxOutput } from "./fontutils.js";
|
||||
import { Font, ToUnicodeMap } from "../../src/core/fonts.js";
|
||||
import { CMapFactory } from "../../src/core/cmap.js";
|
||||
import { Font } from "../../src/core/fonts.js";
|
||||
import { Name } from "../../src/core/primitives.js";
|
||||
import { Stream } from "../../src/core/stream.js";
|
||||
import { ToUnicodeMap } from "../../src/core/to_unicode_map.js";
|
||||
|
||||
describe("font_post", function () {
|
||||
const font2154 = decodeFontData(
|
||||
|
@ -1,8 +1,9 @@
|
||||
import { decodeFontData, ttx, verifyTtxOutput } from "./fontutils.js";
|
||||
import { Font, ToUnicodeMap } from "../../src/core/fonts.js";
|
||||
import { CMapFactory } from "../../src/core/cmap.js";
|
||||
import { Font } from "../../src/core/fonts.js";
|
||||
import { Name } from "../../src/core/primitives.js";
|
||||
import { Stream } from "../../src/core/stream.js";
|
||||
import { ToUnicodeMap } from "../../src/core/to_unicode_map.js";
|
||||
|
||||
describe("font_post", function () {
|
||||
const font2109 = decodeFontData(
|
||||
|
@ -20,7 +20,7 @@ import {
|
||||
CFFParser,
|
||||
CFFStrings,
|
||||
} from "../../src/core/cff_parser.js";
|
||||
import { SEAC_ANALYSIS_ENABLED } from "../../src/core/fonts.js";
|
||||
import { SEAC_ANALYSIS_ENABLED } from "../../src/core/fonts_utils.js";
|
||||
import { Stream } from "../../src/core/stream.js";
|
||||
|
||||
describe("CFFParser", function () {
|
||||
|
@ -13,7 +13,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { SEAC_ANALYSIS_ENABLED } from "../../src/core/fonts.js";
|
||||
import { SEAC_ANALYSIS_ENABLED } from "../../src/core/fonts_utils.js";
|
||||
import { StringStream } from "../../src/core/stream.js";
|
||||
import { Type1Parser } from "../../src/core/type1_parser.js";
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user