Skip to content

Commit 02027ba

Browse files
committed
Combine the getPathJs and hasBuiltPath methods in the CompiledFont class
This avoids duplicating the charCode/glyphId lookup, and (slightly) shortens the code. In particular: - Given that the path-data is returned as a TypedArray, it's easy enough to instead return `null` to indicate that the glyph was previously compiled. - The charCode-cache can be changed into a Set, since we only need to track the "seen" charCodes and not their relation to the glyphIds. - The `compileFontPathInfo` call is moved into `CompiledFont` class, since that simplifies the `src/core/evaluator.js` code a tiny bit.
1 parent eddd70a commit 02027ba

2 files changed

Lines changed: 17 additions & 20 deletions

File tree

src/core/evaluator.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ import {
3333
import { CheckedOperatorList, OperatorList } from "./operator_list.js";
3434
import { CMapFactory, IdentityCMap } from "./cmap.js";
3535
import { Cmd, Dict, EOF, isName, Name, Ref, RefSet } from "./primitives.js";
36-
import {
37-
compileFontPathInfo,
38-
compilePatternInfo,
39-
} from "./obj_bin_transform_core.js";
4036
import {
4137
compileType3Glyph,
4238
FontFlags,
@@ -83,6 +79,7 @@ import { BaseStream } from "./base_stream.js";
8379
import { bidi } from "./bidi.js";
8480
import { ColorSpace } from "./colorspace.js";
8581
import { ColorSpaceUtils } from "./colorspace_utils.js";
82+
import { compilePatternInfo } from "./obj_bin_transform_core.js";
8683
import { getFontSubstitution } from "./font_substitutions.js";
8784
import { getGlyphsUnicode } from "./glyphlist.js";
8885
import { getMetrics } from "./metrics.js";
@@ -4809,10 +4806,10 @@ class PartialEvaluator {
48094806
function buildPath(fontChar) {
48104807
const glyphName = `${font.loadedName}_path_${fontChar}`;
48114808
try {
4812-
if (font.renderer.hasBuiltPath(fontChar)) {
4813-
return;
4809+
const buffer = font.renderer.getPath(fontChar);
4810+
if (!buffer) {
4811+
return; // Previously compiled, and sent to the main-thread.
48144812
}
4815-
const buffer = compileFontPathInfo(font.renderer.getPathJs(fontChar));
48164813
handler.send("commonobj", [glyphName, "FontPath", buffer], [buffer]);
48174814
} catch (reason) {
48184815
if (evaluatorOptions.ignoreErrors) {

src/core/font_renderer.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
warn,
2727
} from "../shared/util.js";
2828
import { CFFParser } from "./cff_parser.js";
29+
import { compileFontPathInfo } from "./obj_bin_transform_core.js";
2930
import { getGlyphsUnicode } from "./glyphlist.js";
3031
import { isNumberArray } from "./core_utils.js";
3132
import { StandardEncoding } from "./encodings.js";
@@ -781,9 +782,9 @@ class Commands {
781782
}
782783

783784
class CompiledFont {
784-
#compiledGlyphs = new Map();
785+
#compiledCharCodes = new Set();
785786

786-
#compiledCharCodeToGlyphId = new Map();
787+
#compiledGlyphs = new Map();
787788

788789
constructor(fontMatrix) {
789790
if (
@@ -806,22 +807,29 @@ class CompiledFont {
806807
);
807808
}
808809

809-
getPathJs(unicode) {
810+
getPath(unicode) {
810811
const { charCode, glyphId } = lookupCmap(this.cmap, unicode);
811812

813+
if (
814+
this.#compiledGlyphs.has(glyphId) &&
815+
this.#compiledCharCodes.has(charCode)
816+
) {
817+
return null; // Previously compiled.
818+
}
819+
812820
const path = this.#compiledGlyphs.getOrInsertComputed(glyphId, () => {
813821
try {
814822
return this.compileGlyph(this.glyphs[glyphId], glyphId);
815823
} catch (ex) {
816824
return ex; // Avoid attempting to re-compile a corrupt glyph.
817825
}
818826
});
819-
this.#compiledCharCodeToGlyphId.getOrInsert(charCode, glyphId);
827+
this.#compiledCharCodes.add(charCode);
820828

821829
if (path instanceof Error) {
822830
throw path;
823831
}
824-
return path;
832+
return compileFontPathInfo(path);
825833
}
826834

827835
compileGlyph(code, glyphId) {
@@ -854,14 +862,6 @@ class CompiledFont {
854862
compileGlyphImpl() {
855863
unreachable("Children classes should implement this.");
856864
}
857-
858-
hasBuiltPath(unicode) {
859-
const { charCode, glyphId } = lookupCmap(this.cmap, unicode);
860-
return (
861-
this.#compiledGlyphs.has(glyphId) &&
862-
this.#compiledCharCodeToGlyphId.has(charCode)
863-
);
864-
}
865865
}
866866

867867
class TrueTypeCompiled extends CompiledFont {

0 commit comments

Comments
 (0)