Skip to content

Commit bdc16f8

Browse files
Merge pull request #20868 from Snuffleupagus/exportData-compileFontInfo
Move the `compileFontInfo` call into the `Font.prototype.exportData` method (PR 20197 follow-up)
2 parents 0ee557c + 7d963dd commit bdc16f8

7 files changed

Lines changed: 35 additions & 42 deletions

File tree

src/core/evaluator.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import {
3232
import { CMapFactory, IdentityCMap } from "./cmap.js";
3333
import { Cmd, Dict, EOF, isName, Name, Ref, RefSet } from "./primitives.js";
3434
import {
35-
compileFontInfo,
3635
compileFontPathInfo,
3736
compilePatternInfo,
3837
} from "./obj_bin_transform_core.js";
@@ -4801,16 +4800,11 @@ class TranslatedFont {
48014800
return;
48024801
}
48034802
this.#sent = true;
4804-
const fontData = this.font.exportData();
4805-
const transfer = [];
4806-
if (fontData.data) {
4807-
if (fontData.data.charProcOperatorList) {
4808-
fontData.charProcOperatorList = fontData.data.charProcOperatorList;
4809-
}
4810-
fontData.data = compileFontInfo(fontData.data);
4811-
transfer.push(fontData.data);
4812-
}
4813-
handler.send("commonobj", [this.loadedName, "Font", fontData], transfer);
4803+
4804+
const fontData = this.font.exportData(),
4805+
transfers = fontData.buffer ? [fontData.buffer] : null;
4806+
4807+
handler.send("commonobj", [this.loadedName, "Font", fontData], transfers);
48144808
// future path: switch to a SharedArrayBuffer
48154809
// const sab = new SharedArrayBuffer(data.byteLength);
48164810
// const view = new Uint8Array(sab);

src/core/fonts.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import {
5757
} from "./standard_fonts.js";
5858
import { IdentityToUnicodeMap, ToUnicodeMap } from "./to_unicode_map.js";
5959
import { CFFFont } from "./cff_font.js";
60+
import { compileFontInfo } from "./obj_bin_transform_core.js";
6061
import { FontRendererFactory } from "./font_renderer.js";
6162
import { getFontBasicMetrics } from "./metrics.js";
6263
import { GlyfTable } from "./glyf.js";
@@ -80,7 +81,7 @@ const EXPORT_DATA_PROPERTIES = [
8081
"bbox",
8182
"black",
8283
"bold",
83-
"charProcOperatorList",
84+
// "charProcOperatorList" is handled separately, since it's not compiled.
8485
"cssFontInfo",
8586
"data",
8687
"defaultVMetrics",
@@ -973,6 +974,8 @@ class Font {
973974

974975
#glyphCache = new Map();
975976

977+
charProcOperatorList;
978+
976979
constructor(name, file, properties, evaluatorOptions) {
977980
this.name = name;
978981
this.psName = null;
@@ -1147,28 +1150,26 @@ class Font {
11471150
return shadow(this, "renderer", renderer);
11481151
}
11491152

1150-
exportData() {
1153+
#getExportData(props) {
11511154
const data = Object.create(null);
1152-
for (const prop of EXPORT_DATA_PROPERTIES) {
1155+
for (const prop of props) {
11531156
const value = this[prop];
11541157
// Ignore properties that haven't been explicitly set.
11551158
if (value !== undefined) {
11561159
data[prop] = value;
11571160
}
11581161
}
1162+
return data;
1163+
}
11591164

1160-
if (!this.fontExtraProperties) {
1161-
return { data };
1162-
}
1163-
1164-
const extra = Object.create(null);
1165-
for (const prop of EXPORT_DATA_EXTRA_PROPERTIES) {
1166-
const value = this[prop];
1167-
if (value !== undefined) {
1168-
extra[prop] = value;
1169-
}
1170-
}
1171-
return { data, extra };
1165+
exportData() {
1166+
return {
1167+
buffer: compileFontInfo(this.#getExportData(EXPORT_DATA_PROPERTIES)),
1168+
charProcOperatorList: this.charProcOperatorList,
1169+
extra: this.fontExtraProperties
1170+
? this.#getExportData(EXPORT_DATA_EXTRA_PROPERTIES)
1171+
: undefined,
1172+
};
11721173
}
11731174

11741175
fallbackToSystemFont(properties) {

src/core/obj_bin_transform_core.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ function compileFontInfo(font) {
169169
view.setUint8(offset++, 0);
170170
offset += 2 * 4; // TODO: optimize this padding away
171171
}
172-
173172
assert(
174173
offset === FONT_INFO.OFFSET_FONT_MATRIX,
175174
"compileFontInfo: BBox properties offset mismatch"
@@ -185,7 +184,6 @@ function compileFontInfo(font) {
185184
view.setUint8(offset++, 0);
186185
offset += 8 * 6; // TODO: optimize this padding away
187186
}
188-
189187
assert(
190188
offset === FONT_INFO.OFFSET_DEFAULT_VMETRICS,
191189
"compileFontInfo: FontMatrix properties offset mismatch"
@@ -201,7 +199,6 @@ function compileFontInfo(font) {
201199
view.setUint8(offset++, 0);
202200
offset += 3 * 2; // TODO: optimize this padding away
203201
}
204-
205202
assert(
206203
offset === FONT_INFO.OFFSET_STRINGS,
207204
"compileFontInfo: DefaultVMetrics properties offset mismatch"

src/display/api.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2893,8 +2893,8 @@ class WorkerTransport {
28932893
const font = new FontFaceObject(
28942894
fontData,
28952895
inspectFont,
2896-
exportedData.extra,
2897-
exportedData.charProcOperatorList
2896+
exportedData.charProcOperatorList,
2897+
exportedData.extra
28982898
);
28992899

29002900
this.fontLoader

src/display/font_loader.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ class FontFaceObject {
359359

360360
#fontData;
361361

362-
constructor(translatedData, inspectFont = null, extra, charProcOperatorList) {
362+
constructor(translatedData, inspectFont = null, charProcOperatorList, extra) {
363363
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
364364
assert(
365365
typeof translatedData.disableFontFace === "boolean",
@@ -372,12 +372,13 @@ class FontFaceObject {
372372
}
373373
this.#fontData = translatedData;
374374
this._inspectFont = inspectFont;
375-
if (extra) {
376-
Object.assign(this, extra);
377-
}
375+
378376
if (charProcOperatorList) {
379377
this.charProcOperatorList = charProcOperatorList;
380378
}
379+
if (extra) {
380+
Object.assign(this, extra);
381+
}
381382
}
382383

383384
createNativeFontFace() {

src/display/obj_bin_transform_display.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class CssFontInfo {
3030

3131
constructor(buffer) {
3232
this.#buffer = buffer;
33-
this.#view = new DataView(this.#buffer);
33+
this.#view = new DataView(buffer);
3434
}
3535

3636
#readString(index) {
@@ -67,7 +67,7 @@ class SystemFontInfo {
6767

6868
constructor(buffer) {
6969
this.#buffer = buffer;
70-
this.#view = new DataView(this.#buffer);
70+
this.#view = new DataView(buffer);
7171
}
7272

7373
get guessFallback() {
@@ -125,9 +125,9 @@ class FontInfo {
125125

126126
#view;
127127

128-
constructor({ data, extra }) {
129-
this.#buffer = data;
130-
this.#view = new DataView(this.#buffer);
128+
constructor({ buffer, extra }) {
129+
this.#buffer = buffer;
130+
this.#view = new DataView(buffer);
131131
if (extra) {
132132
Object.assign(this, extra);
133133
}

test/unit/obj_bin_transform_spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ describe("obj_bin_transform", function () {
133133
sizeEstimate += 4 + fontInfo.data.length;
134134
const buffer = compileFontInfo(fontInfo);
135135
expect(buffer.byteLength).toEqual(sizeEstimate);
136-
const deserialized = new FontInfo({ data: buffer });
136+
const deserialized = new FontInfo({ buffer });
137137
expect(deserialized.black).toEqual(true);
138138
expect(deserialized.bold).toEqual(true);
139139
expect(deserialized.disableFontFace).toEqual(true);
@@ -168,7 +168,7 @@ describe("obj_bin_transform", function () {
168168
cssFontInfo,
169169
systemFontInfo,
170170
});
171-
const deserialized = new FontInfo({ data: buffer });
171+
const deserialized = new FontInfo({ buffer });
172172
expect(deserialized.cssFontInfo.fontWeight).toEqual("not a number");
173173
expect(deserialized.systemFontInfo.src).toEqual("source");
174174
});

0 commit comments

Comments
 (0)