Encode FontPath data into an ArrayBuffer

Serialize FontPath commands into a binary format
and store it in an ArrayBuffer so that it can
eventually be stored in a SharedArrayBuffer.
This commit is contained in:
Ujjwal Sharma 2025-09-30 13:37:39 +05:30 committed by Aditi
parent ec5330f78c
commit 4fed542e3f
5 changed files with 490 additions and 414 deletions

View File

@ -40,7 +40,11 @@ import {
lookupMatrix,
lookupNormalRect,
} from "./core_utils.js";
import { FontInfo, PatternInfo } from "../shared/obj-bin-transform.js";
import {
FontInfo,
FontPathInfo,
PatternInfo,
} from "../shared/obj-bin-transform.js";
import {
getEncoding,
MacRomanEncoding,
@ -4663,11 +4667,8 @@ class PartialEvaluator {
if (font.renderer.hasBuiltPath(fontChar)) {
return;
}
handler.send("commonobj", [
glyphName,
"FontPath",
font.renderer.getPathJs(fontChar),
]);
const buffer = FontPathInfo.write(font.renderer.getPathJs(fontChar));
handler.send("commonobj", [glyphName, "FontPath", buffer], [buffer]);
} catch (reason) {
if (evaluatorOptions.ignoreErrors) {
warn(`buildFontPaths - ignoring ${glyphName} glyph: "${reason}".`);

View File

@ -45,7 +45,11 @@ import {
StatTimer,
} from "./display_utils.js";
import { FontFaceObject, FontLoader } from "./font_loader.js";
import { FontInfo, PatternInfo } from "../shared/obj-bin-transform.js";
import {
FontInfo,
FontPathInfo,
PatternInfo,
} from "../shared/obj-bin-transform.js";
import {
getDataProp,
getFactoryUrlProp,
@ -2821,6 +2825,8 @@ class WorkerTransport {
}
break;
case "FontPath":
this.commonObjs.resolve(id, new FontPathInfo(exportedData));
break;
case "Image":
this.commonObjs.resolve(id, exportedData);
break;

View File

@ -436,7 +436,7 @@ class FontFaceObject {
} catch (ex) {
warn(`getPathGenerator - ignoring character: "${ex}".`);
}
const path = makePathFromDrawOPS(cmds);
const path = makePathFromDrawOPS(cmds.path);
if (!this.fontExtraProperties) {
// Remove the raw path-string, since we don't need it anymore.

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
import { assert, MeshFigureType } from "./util.js";
import { assert, FeatureTest, MeshFigureType } from "./util.js";
class CssFontInfo {
#buffer;
@ -881,4 +881,40 @@ class PatternInfo {
throw new Error(`Unsupported pattern kind: ${kind}`);
}
}
export { CssFontInfo, FontInfo, PatternInfo, SystemFontInfo };
class FontPathInfo {
static write(path) {
let data;
let buffer;
if (
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
FeatureTest.isFloat16ArraySupported
) {
buffer = new ArrayBuffer(path.length * 2);
data = new Float16Array(buffer);
} else {
buffer = new ArrayBuffer(path.length * 4);
data = new Float32Array(buffer);
}
data.set(path);
return buffer;
}
#buffer;
constructor(buffer) {
this.#buffer = buffer;
}
get path() {
if (
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
FeatureTest.isFloat16ArraySupported
) {
return new Float16Array(this.#buffer);
}
return new Float32Array(this.#buffer);
}
}
export { CssFontInfo, FontInfo, FontPathInfo, PatternInfo, SystemFontInfo };

View File

@ -16,11 +16,14 @@
import {
CssFontInfo,
FontInfo,
FontPathInfo,
PatternInfo,
SystemFontInfo,
} from "../../src/shared/obj-bin-transform.js";
import { MeshFigureType } from "../../src/shared/util.js";
import { FeatureTest, MeshFigureType } from "../../src/shared/util.js";
describe("obj-bin-transform", function () {
describe("Font data", function () {
const cssFontInfo = {
fontFamily: "Sample Family",
fontWeight: "not a number",
@ -164,7 +167,9 @@ describe("font data serialization and deserialization", function () {
});
});
});
});
describe("Pattern data", function () {
const axialPatternIR = [
"RadialAxial",
"axial",
@ -203,8 +208,8 @@ const meshPatternIR = [
0, 0, 50, 0, 100, 0, 0, 50, 50, 50, 100, 50, 0, 100, 50, 100, 100, 100,
]),
new Uint8Array([
255, 0, 0, 0, 255, 0, 0, 0, 255, 255, 255, 0, 128, 128, 128, 255, 0, 255, 0,
255, 255, 255, 128, 0, 128, 0, 128,
255, 0, 0, 0, 255, 0, 0, 0, 255, 255, 255, 0, 128, 128, 128, 255, 0,
255, 0, 255, 255, 255, 128, 0, 128, 0, 128,
]),
[
{
@ -295,9 +300,13 @@ describe("Pattern serialization and deserialization", function () {
const fig1 = reconstructedIR[4][0];
expect(fig1.type).toEqual(MeshFigureType.TRIANGLES);
expect(fig1.coords).toBeInstanceOf(Int32Array);
expect(Array.from(fig1.coords)).toEqual([0, 2, 4, 6, 8, 10, 12, 14, 16]);
expect(Array.from(fig1.coords)).toEqual([
0, 2, 4, 6, 8, 10, 12, 14, 16,
]);
expect(fig1.colors).toBeInstanceOf(Int32Array);
expect(Array.from(fig1.colors)).toEqual([0, 2, 4, 6, 8, 10, 12, 14, 16]);
expect(Array.from(fig1.colors)).toEqual([
0, 2, 4, 6, 8, 10, 12, 14, 16,
]);
expect(fig1.verticesPerRow).toBeUndefined();
const fig2 = reconstructedIR[4][1];
@ -450,3 +459,27 @@ describe("Pattern serialization and deserialization", function () {
expect(reconstructedIR[7]).toBeNull();
});
});
});
describe("FontPath data", function () {
const path = FeatureTest.isFloat16ArraySupported
? new Float16Array([
0.214, 0.27, 0.23, 0.33, 0.248, 0.395, 0.265, 0.471, 0.281, 0.54,
0.285, 0.54, 0.302, 0.472, 0.32, 0.395, 0.338, 0.33, 0.353, 0.27,
0.214, 0.27, 0.423, 0, 0.579, 0, 0.375, 0.652, 0.198, 0.652, -0.006,
0, 0.144, 0, 0.184, 0.155, 0.383, 0.155,
])
: new Float32Array([
0.214, 0.27, 0.23, 0.33, 0.248, 0.395, 0.265, 0.471, 0.281, 0.54,
0.285, 0.54, 0.302, 0.472, 0.32, 0.395, 0.338, 0.33, 0.353, 0.27,
0.214, 0.27, 0.423, 0, 0.579, 0, 0.375, 0.652, 0.198, 0.652, -0.006,
0, 0.144, 0, 0.184, 0.155, 0.383, 0.155,
]);
it("should create a FontPathInfo instance from an array of path commands", function () {
const buffer = FontPathInfo.write(path);
const fontPathInfo = new FontPathInfo(buffer);
expect(fontPathInfo.path).toEqual(path);
});
});
});