Move the IdentityToUnicodeMap/ToUnicodeMap from src/core/fonts.js and into its own file

This commit is contained in:
Jonas Jenwald 2021-05-02 12:04:34 +02:00
parent 8c1d1a58f7
commit 6912bb5e0a
6 changed files with 124 additions and 116 deletions

View File

@ -47,14 +47,7 @@ import {
Ref,
RefSet,
} from "./primitives.js";
import {
ErrorFont,
Font,
FontFlags,
getFontType,
IdentityToUnicodeMap,
ToUnicodeMap,
} from "./fonts.js";
import { ErrorFont, Font, FontFlags, getFontType } from "./fonts.js";
import {
getEncoding,
MacRomanEncoding,
@ -74,6 +67,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 {

View File

@ -24,7 +24,6 @@ import {
isNum,
shadow,
string32,
unreachable,
warn,
} from "../shared/util.js";
import {
@ -59,6 +58,7 @@ import {
getUnicodeRangeFor,
mapSpecialUnicodeValues,
} from "./unicode.js";
import { IdentityToUnicodeMap, ToUnicodeMap } from "./to_unicode_map.js";
import {
isWhiteSpace,
MissingDataException,
@ -314,101 +314,6 @@ var Glyph = (function GlyphClosure() {
return Glyph;
})();
var ToUnicodeMap = (function ToUnicodeMapClosure() {
// eslint-disable-next-line no-shadow
function ToUnicodeMap(cmap = []) {
// The elements of this._map can be integers or strings, depending on how
// `cmap` was created.
this._map = cmap;
}
ToUnicodeMap.prototype = {
get length() {
return this._map.length;
},
forEach(callback) {
for (var 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 (var charCode in map) {
this._map[charCode] = map[charCode];
}
},
};
return ToUnicodeMap;
})();
var IdentityToUnicodeMap = (function IdentityToUnicodeMapClosure() {
// eslint-disable-next-line no-shadow
function IdentityToUnicodeMap(firstChar, lastChar) {
this.firstChar = firstChar;
this.lastChar = lastChar;
}
IdentityToUnicodeMap.prototype = {
get length() {
return this.lastChar + 1 - this.firstChar;
},
forEach(callback) {
for (var 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()");
},
};
return IdentityToUnicodeMap;
})();
/**
* 'Font' is the class the outside world should use, it encapsulate all the font
* decoding logics whatever type it is (assuming the font type is supported).
@ -3949,12 +3854,4 @@ var CFFFont = (function CFFFontClosure() {
return CFFFont;
})();
export {
ErrorFont,
Font,
FontFlags,
getFontType,
IdentityToUnicodeMap,
SEAC_ANALYSIS_ENABLED,
ToUnicodeMap,
};
export { ErrorFont, Font, FontFlags, getFontType, SEAC_ANALYSIS_ENABLED };

114
src/core/to_unicode_map.js Normal file
View File

@ -0,0 +1,114 @@
/* 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 { unreachable } from "../shared/util.js";
var ToUnicodeMap = (function ToUnicodeMapClosure() {
// eslint-disable-next-line no-shadow
function ToUnicodeMap(cmap = []) {
// The elements of this._map can be integers or strings, depending on how
// `cmap` was created.
this._map = cmap;
}
ToUnicodeMap.prototype = {
get length() {
return this._map.length;
},
forEach(callback) {
for (var 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 (var charCode in map) {
this._map[charCode] = map[charCode];
}
},
};
return ToUnicodeMap;
})();
var IdentityToUnicodeMap = (function IdentityToUnicodeMapClosure() {
// eslint-disable-next-line no-shadow
function IdentityToUnicodeMap(firstChar, lastChar) {
this.firstChar = firstChar;
this.lastChar = lastChar;
}
IdentityToUnicodeMap.prototype = {
get length() {
return this.lastChar + 1 - this.firstChar;
},
forEach(callback) {
for (var 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()");
},
};
return IdentityToUnicodeMap;
})();
export { IdentityToUnicodeMap, ToUnicodeMap };

View File

@ -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(

View File

@ -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(

View File

@ -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(