postext 0.3.4 → 0.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__tests__/exports.test.js +1 -0
- package/dist/__tests__/exports.test.js.map +1 -1
- package/dist/__tests__/math.test.js +13 -0
- package/dist/__tests__/math.test.js.map +1 -1
- package/dist/canvas-backend.d.ts.map +1 -1
- package/dist/canvas-backend.js +6 -0
- package/dist/canvas-backend.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/math/engine.d.ts.map +1 -1
- package/dist/math/engine.js +5 -2
- package/dist/math/engine.js.map +1 -1
- package/dist/math/pathTransform.d.ts.map +1 -1
- package/dist/math/pathTransform.js +48 -2
- package/dist/math/pathTransform.js.map +1 -1
- package/dist/math/rasterCache.d.ts +5 -0
- package/dist/math/rasterCache.d.ts.map +1 -0
- package/dist/math/rasterCache.js +69 -0
- package/dist/math/rasterCache.js.map +1 -0
- package/dist/parse.d.ts.map +1 -1
- package/dist/parse.js +28 -0
- package/dist/parse.js.map +1 -1
- package/dist/pipeline/build.d.ts +13 -1
- package/dist/pipeline/build.d.ts.map +1 -1
- package/dist/pipeline/build.js +14 -4
- package/dist/pipeline/build.js.map +1 -1
- package/dist/pipeline/index.d.ts +2 -1
- package/dist/pipeline/index.d.ts.map +1 -1
- package/dist/pipeline/index.js +1 -1
- package/dist/pipeline/index.js.map +1 -1
- package/dist/worker/client.d.ts +21 -0
- package/dist/worker/client.d.ts.map +1 -0
- package/dist/worker/client.js +98 -0
- package/dist/worker/client.js.map +1 -0
- package/dist/worker/layout.worker.d.ts +2 -0
- package/dist/worker/layout.worker.d.ts.map +1 -0
- package/dist/worker/layout.worker.js +121 -0
- package/dist/worker/layout.worker.js.map +1 -0
- package/dist/worker/protocol.d.ts +43 -0
- package/dist/worker/protocol.d.ts.map +1 -0
- package/dist/worker/protocol.js +2 -0
- package/dist/worker/protocol.js.map +1 -0
- package/package.json +9 -1
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/// <reference lib="webworker" />
|
|
2
|
+
import { buildDocument, BuildCancelledError } from '../pipeline';
|
|
3
|
+
import { initMathEngine, isMathReady } from '../math';
|
|
4
|
+
import { createMeasurementCache, clearMeasurementCache } from '../measure';
|
|
5
|
+
const ctx = self;
|
|
6
|
+
const registeredFaces = new Set();
|
|
7
|
+
let measurementCache = createMeasurementCache();
|
|
8
|
+
let currentBuildId = null;
|
|
9
|
+
let cancelRequestedFor = null;
|
|
10
|
+
function faceKey(p) {
|
|
11
|
+
return `${p.family}|${p.weight}|${p.style}|${p.unicodeRange ?? ''}`;
|
|
12
|
+
}
|
|
13
|
+
async function registerFonts(faces) {
|
|
14
|
+
const fontSet = ctx.fonts;
|
|
15
|
+
if (!fontSet)
|
|
16
|
+
return;
|
|
17
|
+
let addedAny = false;
|
|
18
|
+
await Promise.all(faces.map(async (face) => {
|
|
19
|
+
const key = faceKey(face);
|
|
20
|
+
if (registeredFaces.has(key))
|
|
21
|
+
return;
|
|
22
|
+
try {
|
|
23
|
+
const ff = new FontFace(face.family, face.buffer, {
|
|
24
|
+
weight: face.weight,
|
|
25
|
+
style: face.style,
|
|
26
|
+
unicodeRange: face.unicodeRange,
|
|
27
|
+
});
|
|
28
|
+
await ff.load();
|
|
29
|
+
fontSet.add(ff);
|
|
30
|
+
registeredFaces.add(key);
|
|
31
|
+
addedAny = true;
|
|
32
|
+
}
|
|
33
|
+
catch (err) {
|
|
34
|
+
console.warn('[postext/worker] failed to register font', face.family, err);
|
|
35
|
+
}
|
|
36
|
+
}));
|
|
37
|
+
// Any build that measured text before these faces landed used fallback
|
|
38
|
+
// metrics. Drop the block-level cache and pretext's glyph cache so the
|
|
39
|
+
// next build re-measures with the real glyphs.
|
|
40
|
+
if (addedAny) {
|
|
41
|
+
measurementCache = createMeasurementCache();
|
|
42
|
+
clearMeasurementCache();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function post(msg) {
|
|
46
|
+
ctx.postMessage(msg);
|
|
47
|
+
}
|
|
48
|
+
function serializeError(err) {
|
|
49
|
+
if (err instanceof Error)
|
|
50
|
+
return { message: err.message, stack: err.stack };
|
|
51
|
+
return { message: String(err) };
|
|
52
|
+
}
|
|
53
|
+
ctx.addEventListener('message', async (event) => {
|
|
54
|
+
const msg = event.data;
|
|
55
|
+
switch (msg.kind) {
|
|
56
|
+
case 'registerFonts': {
|
|
57
|
+
try {
|
|
58
|
+
await registerFonts(msg.faces);
|
|
59
|
+
post({ kind: 'fontsRegistered', id: msg.id });
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
post({ kind: 'error', id: msg.id, ...serializeError(err) });
|
|
63
|
+
}
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
case 'build': {
|
|
67
|
+
currentBuildId = msg.id;
|
|
68
|
+
// Yield once so a cancel posted right after the build can be observed
|
|
69
|
+
// before we start the CPU-bound work.
|
|
70
|
+
await Promise.resolve();
|
|
71
|
+
try {
|
|
72
|
+
// Bring MathJax up before the (sync) build path calls renderMath —
|
|
73
|
+
// otherwise the VDT receives placeholder MathRenders and inline
|
|
74
|
+
// formulas paint as grey boxes instead of glyphs. Skipped for docs
|
|
75
|
+
// that contain no `$…$`.
|
|
76
|
+
if (!isMathReady() && /\$/.test(msg.content.markdown)) {
|
|
77
|
+
await initMathEngine();
|
|
78
|
+
if (cancelRequestedFor === msg.id) {
|
|
79
|
+
post({ kind: 'cancelled', id: msg.id });
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const doc = buildDocument(msg.content, msg.config, measurementCache, {
|
|
84
|
+
shouldCancel: () => cancelRequestedFor === msg.id,
|
|
85
|
+
});
|
|
86
|
+
if (cancelRequestedFor === msg.id) {
|
|
87
|
+
cancelRequestedFor = null;
|
|
88
|
+
post({ kind: 'cancelled', id: msg.id });
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
post({ kind: 'built', id: msg.id, doc });
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
catch (err) {
|
|
95
|
+
if (err instanceof BuildCancelledError) {
|
|
96
|
+
post({ kind: 'cancelled', id: msg.id });
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
post({ kind: 'error', id: msg.id, ...serializeError(err) });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
finally {
|
|
103
|
+
if (currentBuildId === msg.id)
|
|
104
|
+
currentBuildId = null;
|
|
105
|
+
if (cancelRequestedFor === msg.id)
|
|
106
|
+
cancelRequestedFor = null;
|
|
107
|
+
}
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
case 'cancel': {
|
|
111
|
+
if (currentBuildId === msg.id)
|
|
112
|
+
cancelRequestedFor = msg.id;
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
case 'dispose': {
|
|
116
|
+
ctx.close();
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
//# sourceMappingURL=layout.worker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"layout.worker.js","sourceRoot":"","sources":["../../src/worker/layout.worker.ts"],"names":[],"mappings":"AAAA,iCAAiC;AAEjC,OAAO,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AACjE,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACtD,OAAO,EAAE,sBAAsB,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAI3E,MAAM,GAAG,GAA+B,IAA6C,CAAC;AAEtF,MAAM,eAAe,GAAG,IAAI,GAAG,EAAU,CAAC;AAC1C,IAAI,gBAAgB,GAAqB,sBAAsB,EAAE,CAAC;AAClE,IAAI,cAAc,GAAkB,IAAI,CAAC;AACzC,IAAI,kBAAkB,GAAkB,IAAI,CAAC;AAE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AAE;oBAAE,cAAc,GAAG,IAAI,CAAC;gBACrD,IAAI,kBAAkB,KAAK,GAAG,CAAC,EAAE;oBAAE,kBAAkB,GAAG,IAAI,CAAC;YAC/D,CAAC;YACD,OAAO;QACT,CAAC;QACD,KAAK,QAAQ,CAAC,CAAC,CAAC;YACd,IAAI,cAAc,KAAK,GAAG,CAAC,EAAE;gBAAE,kBAAkB,GAAG,GAAG,CAAC,EAAE,CAAC;YAC3D,OAAO;QACT,CAAC;QACD,KAAK,SAAS,CAAC,CAAC,CAAC;YACf,GAAG,CAAC,KAAK,EAAE,CAAC;YACZ,OAAO;QACT,CAAC;IACH,CAAC;AACH,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { PostextContent, PostextConfig } from '../types';
|
|
2
|
+
import type { VDTDocument } from '../vdt';
|
|
3
|
+
export interface FontPayload {
|
|
4
|
+
family: string;
|
|
5
|
+
weight: string;
|
|
6
|
+
style: string;
|
|
7
|
+
unicodeRange?: string;
|
|
8
|
+
/** Transferred to the worker. Becomes detached on the sender side. */
|
|
9
|
+
buffer: ArrayBuffer;
|
|
10
|
+
}
|
|
11
|
+
export type RequestMessage = {
|
|
12
|
+
kind: 'registerFonts';
|
|
13
|
+
id: number;
|
|
14
|
+
faces: FontPayload[];
|
|
15
|
+
} | {
|
|
16
|
+
kind: 'build';
|
|
17
|
+
id: number;
|
|
18
|
+
content: PostextContent;
|
|
19
|
+
config?: PostextConfig;
|
|
20
|
+
} | {
|
|
21
|
+
kind: 'cancel';
|
|
22
|
+
/** Id of the in-flight build to cancel. */
|
|
23
|
+
id: number;
|
|
24
|
+
} | {
|
|
25
|
+
kind: 'dispose';
|
|
26
|
+
};
|
|
27
|
+
export type ResponseMessage = {
|
|
28
|
+
kind: 'built';
|
|
29
|
+
id: number;
|
|
30
|
+
doc: VDTDocument;
|
|
31
|
+
} | {
|
|
32
|
+
kind: 'fontsRegistered';
|
|
33
|
+
id: number;
|
|
34
|
+
} | {
|
|
35
|
+
kind: 'cancelled';
|
|
36
|
+
id: number;
|
|
37
|
+
} | {
|
|
38
|
+
kind: 'error';
|
|
39
|
+
id: number;
|
|
40
|
+
message: string;
|
|
41
|
+
stack?: string;
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=protocol.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../../src/worker/protocol.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAC9D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAE1C,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,sEAAsE;IACtE,MAAM,EAAE,WAAW,CAAC;CACrB;AAED,MAAM,MAAM,cAAc,GACtB;IACE,IAAI,EAAE,eAAe,CAAC;IACtB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,WAAW,EAAE,CAAC;CACtB,GACD;IACE,IAAI,EAAE,OAAO,CAAC;IACd,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,cAAc,CAAC;IACxB,MAAM,CAAC,EAAE,aAAa,CAAC;CACxB,GACD;IACE,IAAI,EAAE,QAAQ,CAAC;IACf,2CAA2C;IAC3C,EAAE,EAAE,MAAM,CAAC;CACZ,GACD;IACE,IAAI,EAAE,SAAS,CAAC;CACjB,CAAC;AAEN,MAAM,MAAM,eAAe,GACvB;IACE,IAAI,EAAE,OAAO,CAAC;IACd,EAAE,EAAE,MAAM,CAAC;IACX,GAAG,EAAE,WAAW,CAAC;CAClB,GACD;IACE,IAAI,EAAE,iBAAiB,CAAC;IACxB,EAAE,EAAE,MAAM,CAAC;CACZ,GACD;IACE,IAAI,EAAE,WAAW,CAAC;IAClB,EAAE,EAAE,MAAM,CAAC;CACZ,GACD;IACE,IAAI,EAAE,OAAO,CAAC;IACd,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protocol.js","sourceRoot":"","sources":["../../src/worker/protocol.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postext",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.6",
|
|
4
4
|
"description": "A programmable typesetter for the web. Layout engine that applies professional editorial rules — columns, orphan/widow prevention, resource placement, footnotes — to semantic content. Built on @chenglou/pretext for DOM-free text measurement.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -18,6 +18,14 @@
|
|
|
18
18
|
"types": "./dist/index.d.ts",
|
|
19
19
|
"default": "./dist/index.js"
|
|
20
20
|
},
|
|
21
|
+
"./worker": {
|
|
22
|
+
"types": "./dist/worker/client.d.ts",
|
|
23
|
+
"default": "./dist/worker/client.js"
|
|
24
|
+
},
|
|
25
|
+
"./worker/entry": {
|
|
26
|
+
"types": "./dist/worker/layout.worker.d.ts",
|
|
27
|
+
"default": "./dist/worker/layout.worker.js"
|
|
28
|
+
},
|
|
21
29
|
"./package.json": "./package.json"
|
|
22
30
|
},
|
|
23
31
|
"peerDependencies": {
|