Merge pull request #20856 from Snuffleupagus/fix-font-clearData

Fix the `FontInfo.prototype.clearData` method to actually remove the data as intended (PR 20197 follow-up)
This commit is contained in:
Tim van der Meij 2026-03-12 20:39:02 +01:00 committed by GitHub
commit decbce7b9b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 29 deletions

View File

@ -2903,7 +2903,7 @@ class WorkerTransport {
.bind(font) .bind(font)
.catch(() => messageHandler.sendWithPromise("FontFallback", { id })) .catch(() => messageHandler.sendWithPromise("FontFallback", { id }))
.finally(() => { .finally(() => {
if (!font.fontExtraProperties && font.data) { if (!font.fontExtraProperties) {
// Immediately release the `font.data` property once the font // Immediately release the `font.data` property once the font
// has been attached to the DOM, since it's no longer needed, // has been attached to the DOM, since it's no longer needed,
// rather than waiting for a `PDFDocumentProxy.cleanup` call. // rather than waiting for a `PDFDocumentProxy.cleanup` call.

View File

@ -18,9 +18,9 @@ import { assert, FeatureTest, MeshFigureType } from "./util.js";
class CssFontInfo { class CssFontInfo {
#buffer; #buffer;
#view; #decoder = new TextDecoder();
#decoder; #view;
static strings = ["fontFamily", "fontWeight", "italicAngle"]; static strings = ["fontFamily", "fontWeight", "italicAngle"];
@ -53,7 +53,6 @@ class CssFontInfo {
constructor(buffer) { constructor(buffer) {
this.#buffer = buffer; this.#buffer = buffer;
this.#view = new DataView(this.#buffer); this.#view = new DataView(this.#buffer);
this.#decoder = new TextDecoder();
} }
#readString(index) { #readString(index) {
@ -84,9 +83,9 @@ class CssFontInfo {
class SystemFontInfo { class SystemFontInfo {
#buffer; #buffer;
#view; #decoder = new TextDecoder();
#decoder; #view;
static strings = ["css", "loadedName", "baseFontName", "src"]; static strings = ["css", "loadedName", "baseFontName", "src"];
@ -147,7 +146,6 @@ class SystemFontInfo {
constructor(buffer) { constructor(buffer) {
this.#buffer = buffer; this.#buffer = buffer;
this.#view = new DataView(this.#buffer); this.#view = new DataView(this.#buffer);
this.#decoder = new TextDecoder();
} }
get guessFallback() { get guessFallback() {
@ -228,13 +226,12 @@ class FontInfo {
#buffer; #buffer;
#decoder; #decoder = new TextDecoder();
#view; #view;
constructor({ data, extra }) { constructor({ data, extra }) {
this.#buffer = data; this.#buffer = data;
this.#decoder = new TextDecoder();
this.#view = new DataView(this.#buffer); this.#view = new DataView(this.#buffer);
if (extra) { if (extra) {
Object.assign(this, extra); Object.assign(this, extra);
@ -379,7 +376,7 @@ class FontInfo {
return this.#readString(3); return this.#readString(3);
} }
get data() { #getDataOffsets() {
let offset = FontInfo.#OFFSET_STRINGS; let offset = FontInfo.#OFFSET_STRINGS;
const stringsLength = this.#view.getUint32(offset); const stringsLength = this.#view.getUint32(offset);
offset += 4 + stringsLength; offset += 4 + stringsLength;
@ -388,25 +385,27 @@ class FontInfo {
const cssFontInfoLength = this.#view.getUint32(offset); const cssFontInfoLength = this.#view.getUint32(offset);
offset += 4 + cssFontInfoLength; offset += 4 + cssFontInfoLength;
const length = this.#view.getUint32(offset); const length = this.#view.getUint32(offset);
if (length === 0) {
return undefined; return { offset, length };
} }
return new Uint8Array(this.#buffer, offset + 4, length);
get data() {
const { offset, length } = this.#getDataOffsets();
return length === 0
? undefined
: new Uint8Array(this.#buffer, offset + 4, length);
} }
clearData() { clearData() {
let offset = FontInfo.#OFFSET_STRINGS; const { offset, length } = this.#getDataOffsets();
const stringsLength = this.#view.getUint32(offset); if (length === 0) {
offset += 4 + stringsLength; return; // The data is either not present, or it was previously cleared.
const systemFontInfoLength = this.#view.getUint32(offset); }
offset += 4 + systemFontInfoLength; this.#view.setUint32(offset, 0); // Zero the data-length.
const cssFontInfoLength = this.#view.getUint32(offset);
offset += 4 + cssFontInfoLength; // Replace the buffer/view with only its contents *before* the data-block.
const length = this.#view.getUint32(offset); this.#buffer = new Uint8Array(this.#buffer, 0, offset + 4).slice().buffer;
const data = new Uint8Array(this.#buffer, offset + 4, length); this.#view = new DataView(this.#buffer);
data.fill(0);
this.#view.setUint32(offset, 0);
// this.#buffer.resize(offset);
} }
get cssFontInfo() { get cssFontInfo() {
@ -462,11 +461,11 @@ class FontInfo {
4 + 4 +
stringsLength + stringsLength +
4 + 4 +
(systemFontInfoBuffer ? systemFontInfoBuffer.byteLength : 0) + (systemFontInfoBuffer?.byteLength ?? 0) +
4 + 4 +
(cssFontInfoBuffer ? cssFontInfoBuffer.byteLength : 0) + (cssFontInfoBuffer?.byteLength ?? 0) +
4 + 4 +
(font.data ? font.data.length : 0); (font.data?.length ?? 0);
const buffer = new ArrayBuffer(lengthEstimate); const buffer = new ArrayBuffer(lengthEstimate);
const data = new Uint8Array(buffer); const data = new Uint8Array(buffer);