From 6912bb5e0acf4bc1239075af0151b64c68e01ee8 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 2 May 2021 12:04:34 +0200 Subject: [PATCH] Move the `IdentityToUnicodeMap`/`ToUnicodeMap` from `src/core/fonts.js` and into its own file --- src/core/evaluator.js | 10 +--- src/core/fonts.js | 107 +-------------------------------- src/core/to_unicode_map.js | 114 ++++++++++++++++++++++++++++++++++++ test/font/font_fpgm_spec.js | 3 +- test/font/font_os2_spec.js | 3 +- test/font/font_post_spec.js | 3 +- 6 files changed, 124 insertions(+), 116 deletions(-) create mode 100644 src/core/to_unicode_map.js diff --git a/src/core/evaluator.js b/src/core/evaluator.js index 5c67ce3c6..18d7923ec 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -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 { diff --git a/src/core/fonts.js b/src/core/fonts.js index 3da8eabb2..b0992820c 100644 --- a/src/core/fonts.js +++ b/src/core/fonts.js @@ -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 }; diff --git a/src/core/to_unicode_map.js b/src/core/to_unicode_map.js new file mode 100644 index 000000000..36f84b4f9 --- /dev/null +++ b/src/core/to_unicode_map.js @@ -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 }; diff --git a/test/font/font_fpgm_spec.js b/test/font/font_fpgm_spec.js index cf2698738..7c748ed47 100644 --- a/test/font/font_fpgm_spec.js +++ b/test/font/font_fpgm_spec.js @@ -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( diff --git a/test/font/font_os2_spec.js b/test/font/font_os2_spec.js index eef69ed06..b344bc90b 100644 --- a/test/font/font_os2_spec.js +++ b/test/font/font_os2_spec.js @@ -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( diff --git a/test/font/font_post_spec.js b/test/font/font_post_spec.js index 5b29e5147..6cb92705a 100644 --- a/test/font/font_post_spec.js +++ b/test/font/font_post_spec.js @@ -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(