@embedpdf/plugin-annotation 3.0.0-next.19 → 3.0.0-next.20
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/contract/host.d.cts +2 -2
- package/dist/contract/host.d.ts +2 -2
- package/dist/contract-CPzpRG3h.cjs.map +1 -1
- package/dist/{contract-D6a0ANBk.d.ts → contract-Cm0cfAzG.d.cts} +17 -4
- package/dist/{contract-CX4PcAoR.d.cts → contract-QAvnfm2b.d.ts} +17 -4
- package/dist/contract-b2e_LZvu.js.map +1 -1
- package/dist/contract.d.cts +1 -1
- package/dist/contract.d.ts +1 -1
- package/dist/{host-contract-lQ1quQNY.d.cts → host-contract-BDwyBNrs.d.cts} +14 -3
- package/dist/{host-contract-PNwb5DEw.d.ts → host-contract-BUub1Un-.d.ts} +14 -3
- package/dist/host-contract-CxrQPcPj.js.map +1 -1
- package/dist/host-contract-DY9jFPPF.cjs.map +1 -1
- package/dist/index.cjs +287 -75
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +289 -77
- package/dist/index.js.map +1 -1
- package/package.json +12 -10
package/dist/index.cjs
CHANGED
|
@@ -4,6 +4,7 @@ const require_host_contract = require("./host-contract-DY9jFPPF.cjs");
|
|
|
4
4
|
require("./authoring.cjs");
|
|
5
5
|
let _embedpdf_core = require("@embedpdf/core");
|
|
6
6
|
let _embedpdf_plugin_actions_contract = require("@embedpdf/plugin-actions/contract");
|
|
7
|
+
let _embedpdf_plugin_fonts_contract = require("@embedpdf/plugin-fonts/contract");
|
|
7
8
|
let _embedpdf_plugin_interaction_contract_host = require("@embedpdf/plugin-interaction/contract/host");
|
|
8
9
|
let _embedpdf_plugin_render_contract = require("@embedpdf/plugin-render/contract");
|
|
9
10
|
let _embedpdf_plugin_selection_contract_host = require("@embedpdf/plugin-selection/contract/host");
|
|
@@ -11,6 +12,7 @@ let _embedpdf_engine_core_runtime = require("@embedpdf/engine-core/runtime");
|
|
|
11
12
|
let _embedpdf_core_annotation = require("@embedpdf/core-annotation");
|
|
12
13
|
let _embedpdf_plugin_actions_contract_host = require("@embedpdf/plugin-actions/contract/host");
|
|
13
14
|
let _embedpdf_plugin_render_contract_host = require("@embedpdf/plugin-render/contract/host");
|
|
15
|
+
let _embedpdf_core_fonts = require("@embedpdf/core-fonts");
|
|
14
16
|
let _embedpdf_plugin_interaction_contract = require("@embedpdf/plugin-interaction/contract");
|
|
15
17
|
let _embedpdf_plugin_selection_contract = require("@embedpdf/plugin-selection/contract");
|
|
16
18
|
let _embedpdf_core_acrojs = require("@embedpdf/core-acrojs");
|
|
@@ -1137,7 +1139,7 @@ function createChromeReads(ctx, { store, geometry, behaviors, settings }) {
|
|
|
1137
1139
|
* • a flat props patch → the run delta it means for a text range (font,
|
|
1138
1140
|
* size, colour, bold/italic/underline) and the keys left for the body
|
|
1139
1141
|
* • what a range reads back for those keys (agree → the value, else mixed)
|
|
1140
|
-
* • faces: the DTO font a face names
|
|
1142
|
+
* • faces: the DTO font a face names
|
|
1141
1143
|
* • the commit rule: a document nothing overrides on a plain annotation
|
|
1142
1144
|
* commits as `contents`; anything else as `richText`
|
|
1143
1145
|
*/
|
|
@@ -1150,65 +1152,29 @@ const RANGE_KEYS = [
|
|
|
1150
1152
|
"italic",
|
|
1151
1153
|
"underline"
|
|
1152
1154
|
];
|
|
1153
|
-
/** Family names compare without case, spaces, hyphens, underscores and
|
|
1154
|
-
* quotes — the engine's own rule. */
|
|
1155
|
-
function familyKey(family) {
|
|
1156
|
-
return family.toLowerCase().replace(/[\s\-_'"]/g, "");
|
|
1157
|
-
}
|
|
1158
|
-
const STANDARD_FAMILY_KEYS = {
|
|
1159
|
-
helvetica: "Helvetica",
|
|
1160
|
-
arial: "Helvetica",
|
|
1161
|
-
arialmt: "Helvetica",
|
|
1162
|
-
sansserif: "Helvetica",
|
|
1163
|
-
times: "Times",
|
|
1164
|
-
timesroman: "Times",
|
|
1165
|
-
timesnewroman: "Times",
|
|
1166
|
-
timesnewromanpsmt: "Times",
|
|
1167
|
-
serif: "Times",
|
|
1168
|
-
courier: "Courier",
|
|
1169
|
-
couriernew: "Courier",
|
|
1170
|
-
couriernewpsmt: "Courier",
|
|
1171
|
-
monospace: "Courier",
|
|
1172
|
-
symbol: "Symbol",
|
|
1173
|
-
zapfdingbats: "ZapfDingbats",
|
|
1174
|
-
dingbats: "ZapfDingbats"
|
|
1175
|
-
};
|
|
1176
1155
|
/** The DTO font for a face: the registered key whose identity matches (the
|
|
1177
1156
|
* closest weight, italic first), else the standard kebab name, else the
|
|
1178
1157
|
* family itself. The inverse of {@link faceForFont}. */
|
|
1179
1158
|
function fontForFace(face, fonts) {
|
|
1180
|
-
const wanted = familyKey(face.family);
|
|
1181
1159
|
const weight = face.weight ?? 400;
|
|
1182
1160
|
const italic = face.italic ?? false;
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
}
|
|
1201
|
-
/** The CSS family list for a face family: a standard family's web stack, a
|
|
1202
|
-
* registered family's mounted `@font-face` ({@link mountedFontFamily}), else
|
|
1203
|
-
* the family itself with a generic fallback. A DTO font's list is
|
|
1204
|
-
* `cssFontFamilyForFont`'s, in `@embedpdf/core-annotation`. */
|
|
1205
|
-
function cssFontFamilyForFace(family, fonts) {
|
|
1206
|
-
const standard = STANDARD_FAMILY_KEYS[familyKey(family)];
|
|
1207
|
-
if (standard) return _embedpdf_core_annotation.STANDARD_FONT_STACKS[standard];
|
|
1208
|
-
const wanted = familyKey(family);
|
|
1209
|
-
const registered = fonts?.().find((handle) => familyKey(handle.familyName) === wanted);
|
|
1210
|
-
if (registered) return (0, _embedpdf_core_annotation.mountedFontFamily)(registered.key);
|
|
1211
|
-
return `"${family}", sans-serif`;
|
|
1161
|
+
return (0, _embedpdf_engine_core_runtime.fontNameForFace)({
|
|
1162
|
+
family: face.family,
|
|
1163
|
+
weight,
|
|
1164
|
+
italic
|
|
1165
|
+
}, (family) => {
|
|
1166
|
+
const wanted = (0, _embedpdf_engine_core_runtime.familyKey)(family);
|
|
1167
|
+
let best;
|
|
1168
|
+
for (const handle of fonts?.() ?? []) {
|
|
1169
|
+
if ((0, _embedpdf_engine_core_runtime.familyKey)(handle.familyName) !== wanted) continue;
|
|
1170
|
+
const score = Math.abs(handle.weight - weight) + (handle.italic === italic ? 0 : 1e3);
|
|
1171
|
+
if (!best || score < best.score) best = {
|
|
1172
|
+
key: handle.key,
|
|
1173
|
+
score
|
|
1174
|
+
};
|
|
1175
|
+
}
|
|
1176
|
+
return best?.key;
|
|
1177
|
+
});
|
|
1212
1178
|
}
|
|
1213
1179
|
const hex = (css) => css.trim().toUpperCase();
|
|
1214
1180
|
/**
|
|
@@ -1447,36 +1413,49 @@ function createPropertyReads(ctx, { store, fonts }) {
|
|
|
1447
1413
|
* The free-text presentation projection: the core's geometry-only `textBoxes`
|
|
1448
1414
|
* joined with the DTO-derived font + CSS into render-ready {@link TextItem}s. This
|
|
1449
1415
|
* is the text analogue of the core's `scene()` "paint" for shapes, shared across
|
|
1450
|
-
* every web framework
|
|
1416
|
+
* every web framework. The fonts are the text fonts' (the programs the engine
|
|
1417
|
+
* draws with), else the standard font the engine falls back to.
|
|
1451
1418
|
*/
|
|
1452
1419
|
/** Project the model's free-text boxes into render-ready {@link TextItem}s — the
|
|
1453
1420
|
* core geometry (`textBoxes`) joined with the DTO-derived CSS. Pure; memoized by
|
|
1454
1421
|
* model identity at the call site so selectors get a stable reference. */
|
|
1455
|
-
function buildTextItems(model, page, view) {
|
|
1456
|
-
return (0, _embedpdf_core_annotation.textBoxes)(model, page, view).map((tb) => textItemOf(model, tb));
|
|
1422
|
+
function buildTextItems(model, page, view, fonts) {
|
|
1423
|
+
return (0, _embedpdf_core_annotation.textBoxes)(model, page, view).map((tb) => textItemOf(model, tb, fonts));
|
|
1457
1424
|
}
|
|
1458
1425
|
/** One free text's {@link TextItem}, live or baked (`textBoxOf`): what a renderer
|
|
1459
1426
|
* that draws the text box itself edits. `null` for a record that isn't one. */
|
|
1460
|
-
function buildTextItem(model, id, view) {
|
|
1427
|
+
function buildTextItem(model, id, view, fonts) {
|
|
1461
1428
|
const tb = (0, _embedpdf_core_annotation.textBoxOf)(model, id, view);
|
|
1462
|
-
return tb && textItemOf(model, tb);
|
|
1429
|
+
return tb && textItemOf(model, tb, fonts);
|
|
1463
1430
|
}
|
|
1464
|
-
/** A text box joined with its record's text, style and plate inset. */
|
|
1465
|
-
function textItemOf(model, tb) {
|
|
1431
|
+
/** A text box joined with its record's text, style, faces and plate inset. */
|
|
1432
|
+
function textItemOf(model, tb, fonts) {
|
|
1466
1433
|
const record = model.byId[tb.id];
|
|
1467
1434
|
const style = (record && (0, _embedpdf_core_annotation.textOf)(record.annotation)) ?? _embedpdf_core_annotation.initialTextStyle;
|
|
1468
1435
|
const sw = record ? (0, _embedpdf_core_annotation.styleOf)(record.annotation).strokeWidth : 0;
|
|
1469
|
-
const doc = record ? (0, _embedpdf_core_annotation.richDocOf)(record.annotation) : null;
|
|
1436
|
+
const doc = record ? (0, _embedpdf_core_annotation.richDocOf)(record.annotation, fonts?.lookup) : null;
|
|
1437
|
+
const face = doc ? {
|
|
1438
|
+
family: doc.body.family,
|
|
1439
|
+
weight: doc.body.weight,
|
|
1440
|
+
italic: doc.body.italic
|
|
1441
|
+
} : {
|
|
1442
|
+
family: "Helvetica",
|
|
1443
|
+
weight: 400,
|
|
1444
|
+
italic: false
|
|
1445
|
+
};
|
|
1446
|
+
const bodyCss = fonts?.text.bodyCss(tb.id, style, face) ?? (0, _embedpdf_core_fonts.standardFontCss)((0, _embedpdf_core_fonts.standardFontOf)(style.fontFamily));
|
|
1470
1447
|
return {
|
|
1471
1448
|
id: tb.id,
|
|
1472
1449
|
ref: (0, _embedpdf_core_annotation.refOf)(record),
|
|
1473
1450
|
box: tb.box,
|
|
1474
1451
|
contents: record?.annotation.contents ?? "",
|
|
1475
1452
|
richText: { paragraphs: doc ? stripBodyDefaults(doc.paragraphs, doc.body) : [{ runs: [{ text: "" }] }] },
|
|
1453
|
+
face,
|
|
1454
|
+
programs: fonts && doc ? programsOf(tb.id, doc, face, bodyCss.programs, fonts.text) : bodyCss.programs,
|
|
1476
1455
|
editing: tb.editing,
|
|
1477
1456
|
...tb.rot ? { rot: tb.rot } : {},
|
|
1478
1457
|
css: {
|
|
1479
|
-
fontFamily:
|
|
1458
|
+
fontFamily: bodyCss.fontFamily,
|
|
1480
1459
|
fontSize: style.fontSize,
|
|
1481
1460
|
color: style.fontColor,
|
|
1482
1461
|
fontWeight: style.bold ? 700 : 400,
|
|
@@ -1487,6 +1466,21 @@ function textItemOf(model, tb) {
|
|
|
1487
1466
|
}
|
|
1488
1467
|
};
|
|
1489
1468
|
}
|
|
1469
|
+
/** The programs a box's text is drawn in: the body's, then those of each face a run names. */
|
|
1470
|
+
function programsOf(id, doc, body, bodyPrograms, text) {
|
|
1471
|
+
const programs = [...bodyPrograms];
|
|
1472
|
+
for (const paragraph of doc.paragraphs) for (const run of paragraph.runs) {
|
|
1473
|
+
const own = run.style;
|
|
1474
|
+
if (own?.family === void 0 && own?.weight === void 0 && own?.italic === void 0) continue;
|
|
1475
|
+
const css = text.faceCss(id, {
|
|
1476
|
+
family: own.family ?? body.family,
|
|
1477
|
+
weight: own.weight ?? body.weight,
|
|
1478
|
+
italic: own.italic ?? body.italic
|
|
1479
|
+
});
|
|
1480
|
+
for (const program of css.programs) if (!programs.some((known) => known.hash === program.hash)) programs.push(program);
|
|
1481
|
+
}
|
|
1482
|
+
return programs;
|
|
1483
|
+
}
|
|
1490
1484
|
//#endregion
|
|
1491
1485
|
//#region src/read/render.ts
|
|
1492
1486
|
/**
|
|
@@ -1498,23 +1492,42 @@ function textItemOf(model, tb) {
|
|
|
1498
1492
|
* engine's picture of it exists, and an annotation an undo brings back the
|
|
1499
1493
|
* picture it had.
|
|
1500
1494
|
*/
|
|
1501
|
-
function createRenderReads(ctx, { view: { pageModel }, lending }, ghost, stamps) {
|
|
1495
|
+
function createRenderReads(ctx, { view: { pageModel }, lending, fonts: fontLookup, textFonts }, ghost, stamps) {
|
|
1502
1496
|
const itemsCache = /* @__PURE__ */ new Map();
|
|
1497
|
+
const fonts = () => ctx.tryGet(_embedpdf_plugin_fonts_contract.FontsToken);
|
|
1498
|
+
/**
|
|
1499
|
+
* A hovered redaction's label with the fonts its preview draws it in, as
|
|
1500
|
+
* the engine picks them for the label's font; without the fonts plugin, or
|
|
1501
|
+
* while it finds them, the label's standard font draws it.
|
|
1502
|
+
*/
|
|
1503
|
+
const withLabelFonts = (item) => {
|
|
1504
|
+
if (!item.hovered || !item.label) return item;
|
|
1505
|
+
const labelFonts = fonts()?.getPaintFonts(item.text?.fontFamily ?? "helvetica", item.label.text);
|
|
1506
|
+
return labelFonts ? {
|
|
1507
|
+
...item,
|
|
1508
|
+
label: {
|
|
1509
|
+
...item.label,
|
|
1510
|
+
fonts: labelFonts
|
|
1511
|
+
}
|
|
1512
|
+
} : item;
|
|
1513
|
+
};
|
|
1503
1514
|
const pageItemsOf = (page, view) => {
|
|
1504
1515
|
const pageObjectNumber = page.objectNumber;
|
|
1505
1516
|
const model = pageModel(pageObjectNumber);
|
|
1506
1517
|
const state = ctx.state.get();
|
|
1507
1518
|
const ghostAt = state.ghostAt?.page.objectNumber === pageObjectNumber ? state.ghostAt : null;
|
|
1508
1519
|
const placing = state.placing?.page.objectNumber === pageObjectNumber ? state.placing : null;
|
|
1520
|
+
const fontsEpoch = fonts()?.getPaintFontsEpoch() ?? 0;
|
|
1509
1521
|
const cached = itemsCache.get(pageObjectNumber);
|
|
1510
|
-
if (cached && cached.model === model && cached.ghostAt === ghostAt && cached.placing === placing && cached.zoom === view?.zoom && cached.rotation === view?.rotation) return cached.v;
|
|
1511
|
-
const items = [...(0, _embedpdf_core_annotation.pageItems)(model, page, view), ...ghost.itemsOn(pageObjectNumber, model, view)];
|
|
1522
|
+
if (cached && cached.model === model && cached.ghostAt === ghostAt && cached.placing === placing && cached.zoom === view?.zoom && cached.rotation === view?.rotation && cached.fontsEpoch === fontsEpoch) return cached.v;
|
|
1523
|
+
const items = [...(0, _embedpdf_core_annotation.pageItems)(model, page, view).map(withLabelFonts), ...ghost.itemsOn(pageObjectNumber, model, view)];
|
|
1512
1524
|
itemsCache.set(pageObjectNumber, {
|
|
1513
1525
|
model,
|
|
1514
1526
|
ghostAt,
|
|
1515
1527
|
placing,
|
|
1516
1528
|
zoom: view?.zoom,
|
|
1517
1529
|
rotation: view?.rotation,
|
|
1530
|
+
fontsEpoch,
|
|
1518
1531
|
v: items
|
|
1519
1532
|
});
|
|
1520
1533
|
return items;
|
|
@@ -1523,13 +1536,18 @@ function createRenderReads(ctx, { view: { pageModel }, lending }, ghost, stamps)
|
|
|
1523
1536
|
const textItemsOf = (page, view) => {
|
|
1524
1537
|
const pageObjectNumber = page.objectNumber;
|
|
1525
1538
|
const model = pageModel(pageObjectNumber);
|
|
1539
|
+
const fonts = textFonts.version();
|
|
1526
1540
|
const cached = textsCache.get(pageObjectNumber);
|
|
1527
|
-
if (cached && cached.model === model && cached.zoom === view?.zoom && cached.rotation === view?.rotation) return cached.v;
|
|
1528
|
-
const items = buildTextItems(model, page, view
|
|
1541
|
+
if (cached && cached.model === model && cached.zoom === view?.zoom && cached.rotation === view?.rotation && cached.fonts === fonts) return cached.v;
|
|
1542
|
+
const items = buildTextItems(model, page, view, {
|
|
1543
|
+
lookup: fontLookup,
|
|
1544
|
+
text: textFonts
|
|
1545
|
+
});
|
|
1529
1546
|
textsCache.set(pageObjectNumber, {
|
|
1530
1547
|
model,
|
|
1531
1548
|
zoom: view?.zoom,
|
|
1532
1549
|
rotation: view?.rotation,
|
|
1550
|
+
fonts,
|
|
1533
1551
|
v: items
|
|
1534
1552
|
});
|
|
1535
1553
|
return items;
|
|
@@ -1538,13 +1556,18 @@ function createRenderReads(ctx, { view: { pageModel }, lending }, ghost, stamps)
|
|
|
1538
1556
|
const textItemOf = (ref, view) => {
|
|
1539
1557
|
const key = (0, _embedpdf_engine_core_runtime.annotationKey)(ref);
|
|
1540
1558
|
const model = pageModel(ref.page.objectNumber);
|
|
1559
|
+
const fonts = textFonts.version();
|
|
1541
1560
|
const cached = textCache.get(key);
|
|
1542
|
-
if (cached && cached.model === model && cached.zoom === view?.zoom && cached.rotation === view?.rotation) return cached.v;
|
|
1543
|
-
const item = buildTextItem(model, key, view
|
|
1561
|
+
if (cached && cached.model === model && cached.zoom === view?.zoom && cached.rotation === view?.rotation && cached.fonts === fonts) return cached.v;
|
|
1562
|
+
const item = buildTextItem(model, key, view, {
|
|
1563
|
+
lookup: fontLookup,
|
|
1564
|
+
text: textFonts
|
|
1565
|
+
});
|
|
1544
1566
|
textCache.set(key, {
|
|
1545
1567
|
model,
|
|
1546
1568
|
zoom: view?.zoom,
|
|
1547
1569
|
rotation: view?.rotation,
|
|
1570
|
+
fonts,
|
|
1548
1571
|
v: item
|
|
1549
1572
|
});
|
|
1550
1573
|
return item;
|
|
@@ -3055,6 +3078,171 @@ function createStore(ctx, view, records, events, lending) {
|
|
|
3055
3078
|
};
|
|
3056
3079
|
}
|
|
3057
3080
|
//#endregion
|
|
3081
|
+
//#region src/read/text-fonts.ts
|
|
3082
|
+
/**
|
|
3083
|
+
* The fonts a free text box's text is drawn in by the browser, so it shows the
|
|
3084
|
+
* program the engine writes with. A box someone types in is resolved the way
|
|
3085
|
+
* its write will be (the fonts plugin's `resolve`, with the box, so its own
|
|
3086
|
+
* font counts): when the edit starts, and again after a pause in typing once
|
|
3087
|
+
* the text has a character it wasn't resolved with. Any other box is drawn in
|
|
3088
|
+
* the fonts its names say. Without the fonts plugin, a box is drawn in the
|
|
3089
|
+
* standard font the engine falls back to for its font or face, in that
|
|
3090
|
+
* font's built-in program (`@embedpdf/core-fonts`).
|
|
3091
|
+
*/
|
|
3092
|
+
/** How long typing pauses before a box with new characters is resolved again. */
|
|
3093
|
+
const RESOLVE_DELAY_MS = 250;
|
|
3094
|
+
const faceKey = (face) => `${(0, _embedpdf_engine_core_runtime.familyKey)(face.family)}|${face.weight}|${face.italic}`;
|
|
3095
|
+
/** The key of a box's body in a resolved box's faces. */
|
|
3096
|
+
const BODY = "body";
|
|
3097
|
+
/** Every code point of a text. */
|
|
3098
|
+
const codePointsOf = (text) => Array.from(text, (char) => char.codePointAt(0));
|
|
3099
|
+
function createTextFonts(ctx, store, lookup) {
|
|
3100
|
+
const fonts = () => ctx.tryGet(_embedpdf_plugin_fonts_contract.FontsToken);
|
|
3101
|
+
/**
|
|
3102
|
+
* What the engine answered per box: the run each face its text was resolved with drew in, its
|
|
3103
|
+
* CSS asked for on each read (it changes as the programs reach the browser).
|
|
3104
|
+
*/
|
|
3105
|
+
const answers = /* @__PURE__ */ new Map();
|
|
3106
|
+
/** The box being typed in, and the characters its last resolve asked about. */
|
|
3107
|
+
let asked = null;
|
|
3108
|
+
let resolving = null;
|
|
3109
|
+
let cancelPause = () => {};
|
|
3110
|
+
let answered = 0;
|
|
3111
|
+
let token = {
|
|
3112
|
+
list: null,
|
|
3113
|
+
answered,
|
|
3114
|
+
epoch: 0
|
|
3115
|
+
};
|
|
3116
|
+
/** The body's face named by its font alone: then the box's font is the one drawn. */
|
|
3117
|
+
const bodyIsItsFont = (style, body) => {
|
|
3118
|
+
const face = (0, _embedpdf_engine_core_runtime.faceForFontName)(style.fontFamily, (key) => lookup().find((font) => font.key === key));
|
|
3119
|
+
return (face.weight ?? 400) === body.weight && (face.italic ?? false) === body.italic;
|
|
3120
|
+
};
|
|
3121
|
+
/** Ask the engine what the box's text will be drawn in, and keep the answer. */
|
|
3122
|
+
const resolveBox = async (id) => {
|
|
3123
|
+
const api = fonts();
|
|
3124
|
+
const record = store.model().byId[id];
|
|
3125
|
+
const style = record && (0, _embedpdf_core_annotation.textOf)(record.annotation);
|
|
3126
|
+
if (!api || !record || !style || record.annotation.subtype !== "free-text") return;
|
|
3127
|
+
const ref = (0, _embedpdf_core_annotation.refOf)(record) ?? void 0;
|
|
3128
|
+
const doc = (0, _embedpdf_core_annotation.richDocOf)(record.annotation, lookup);
|
|
3129
|
+
const body = doc.body;
|
|
3130
|
+
const faces = [BODY];
|
|
3131
|
+
const runs = [bodyRun(style, body, bodyIsItsFont(style, body))];
|
|
3132
|
+
let text = "";
|
|
3133
|
+
for (const paragraph of doc.paragraphs) for (const run of paragraph.runs) {
|
|
3134
|
+
text += run.text;
|
|
3135
|
+
const own = run.style;
|
|
3136
|
+
if (own?.family === void 0 && own?.weight === void 0 && own?.italic === void 0) {
|
|
3137
|
+
faces.push(BODY);
|
|
3138
|
+
runs.push({
|
|
3139
|
+
...bodyRun(style, body, bodyIsItsFont(style, body)),
|
|
3140
|
+
text: run.text
|
|
3141
|
+
});
|
|
3142
|
+
continue;
|
|
3143
|
+
}
|
|
3144
|
+
const face = {
|
|
3145
|
+
family: own.family ?? body.family,
|
|
3146
|
+
weight: own.weight ?? body.weight,
|
|
3147
|
+
italic: own.italic ?? body.italic
|
|
3148
|
+
};
|
|
3149
|
+
faces.push(faceKey(face));
|
|
3150
|
+
runs.push({
|
|
3151
|
+
text: run.text,
|
|
3152
|
+
...face
|
|
3153
|
+
});
|
|
3154
|
+
}
|
|
3155
|
+
asked = {
|
|
3156
|
+
id,
|
|
3157
|
+
codePoints: new Set(codePointsOf(text))
|
|
3158
|
+
};
|
|
3159
|
+
resolving?.abort();
|
|
3160
|
+
const request = new AbortController();
|
|
3161
|
+
resolving = request;
|
|
3162
|
+
try {
|
|
3163
|
+
await api.ensureLoaded();
|
|
3164
|
+
const answer = await api.resolve({
|
|
3165
|
+
...ref ? { annotation: ref } : {},
|
|
3166
|
+
runs
|
|
3167
|
+
}, { signal: request.signal });
|
|
3168
|
+
if (resolving !== request) return;
|
|
3169
|
+
const byFace = /* @__PURE__ */ new Map();
|
|
3170
|
+
answer.runs.forEach((run, index) => {
|
|
3171
|
+
const face = faces[index];
|
|
3172
|
+
if (!byFace.has(face)) byFace.set(face, run);
|
|
3173
|
+
});
|
|
3174
|
+
if (!byFace.has(faceKey(body))) byFace.set(faceKey(body), byFace.get(BODY));
|
|
3175
|
+
answers.set(id, byFace);
|
|
3176
|
+
answered++;
|
|
3177
|
+
ctx.notify();
|
|
3178
|
+
} catch {}
|
|
3179
|
+
};
|
|
3180
|
+
let editing = null;
|
|
3181
|
+
ctx.state.onChange(() => {
|
|
3182
|
+
const next = store.model().editing;
|
|
3183
|
+
if (next === editing) return;
|
|
3184
|
+
editing = next;
|
|
3185
|
+
cancelPause();
|
|
3186
|
+
asked = null;
|
|
3187
|
+
if (next !== null) resolveBox(next);
|
|
3188
|
+
});
|
|
3189
|
+
return {
|
|
3190
|
+
bodyCss: (id, style, body) => {
|
|
3191
|
+
const api = fonts();
|
|
3192
|
+
const byFont = bodyIsItsFont(style, body);
|
|
3193
|
+
if (!api) return (0, _embedpdf_core_fonts.standardFontCss)(byFont ? (0, _embedpdf_core_fonts.standardFontOf)(style.fontFamily) : (0, _embedpdf_core_fonts.standardFontForFace)(body));
|
|
3194
|
+
const run = answers.get(id)?.get(BODY);
|
|
3195
|
+
if (run) return api.getRunCss(run);
|
|
3196
|
+
return byFont ? api.getFontCss(style.fontFamily) : api.getFaceCss(body);
|
|
3197
|
+
},
|
|
3198
|
+
faceCss: (id, face) => {
|
|
3199
|
+
const api = fonts();
|
|
3200
|
+
if (!api) return (0, _embedpdf_core_fonts.standardFontCss)((0, _embedpdf_core_fonts.standardFontForFace)(face));
|
|
3201
|
+
const run = id !== null ? answers.get(id)?.get(faceKey(face)) : void 0;
|
|
3202
|
+
return run ? api.getRunCss(run) : api.getFaceCss(face);
|
|
3203
|
+
},
|
|
3204
|
+
familyOfCss: (cssFamily) => {
|
|
3205
|
+
const face = fonts()?.getFaceOfCssFamily(cssFamily);
|
|
3206
|
+
if (face) return face.family;
|
|
3207
|
+
const standard = (0, _embedpdf_core_fonts.standardFontOfProgramFamily)(cssFamily);
|
|
3208
|
+
return standard && _embedpdf_engine_core_runtime.STANDARD_FACES[standard].family;
|
|
3209
|
+
},
|
|
3210
|
+
version: () => {
|
|
3211
|
+
const api = fonts();
|
|
3212
|
+
const list = api?.getFontList() ?? null;
|
|
3213
|
+
const epoch = api?.getPaintFontsEpoch() ?? 0;
|
|
3214
|
+
if (token.list !== list || token.answered !== answered || token.epoch !== epoch) token = {
|
|
3215
|
+
list,
|
|
3216
|
+
answered,
|
|
3217
|
+
epoch
|
|
3218
|
+
};
|
|
3219
|
+
return token;
|
|
3220
|
+
},
|
|
3221
|
+
typed: (id) => {
|
|
3222
|
+
if (editing !== id) return;
|
|
3223
|
+
const record = store.model().byId[id];
|
|
3224
|
+
if (!record) return;
|
|
3225
|
+
const text = (0, _embedpdf_core_annotation.richDocOf)(record.annotation, lookup).paragraphs.flatMap((paragraph) => paragraph.runs.map((run) => run.text)).join("");
|
|
3226
|
+
const known = asked?.id === id ? asked.codePoints : null;
|
|
3227
|
+
if (known && codePointsOf(text).every((point) => known.has(point))) return;
|
|
3228
|
+
cancelPause();
|
|
3229
|
+
cancelPause = ctx.clock.after(RESOLVE_DELAY_MS, () => void resolveBox(id));
|
|
3230
|
+
}
|
|
3231
|
+
};
|
|
3232
|
+
}
|
|
3233
|
+
/** The body as a run: by the box's font when its face is that font's, else by face. */
|
|
3234
|
+
function bodyRun(style, body, byFont) {
|
|
3235
|
+
return byFont ? {
|
|
3236
|
+
text: "",
|
|
3237
|
+
fontFamily: style.fontFamily
|
|
3238
|
+
} : {
|
|
3239
|
+
text: "",
|
|
3240
|
+
family: body.family,
|
|
3241
|
+
weight: body.weight,
|
|
3242
|
+
italic: body.italic
|
|
3243
|
+
};
|
|
3244
|
+
}
|
|
3245
|
+
//#endregion
|
|
3058
3246
|
//#region src/tools/behaviors.ts
|
|
3059
3247
|
/**
|
|
3060
3248
|
* Behaviors: a sibling plugin (forms, links) or an interactive renderer marks
|
|
@@ -3309,6 +3497,7 @@ function createServices(ctx) {
|
|
|
3309
3497
|
const view = createView(ctx, records, lending);
|
|
3310
3498
|
const store = createStore(ctx, view, records, events, lending);
|
|
3311
3499
|
const tools = createToolRegistry(ctx, settings.get().tools, store, events);
|
|
3500
|
+
const fonts = createFontLookup(ctx);
|
|
3312
3501
|
return {
|
|
3313
3502
|
settings,
|
|
3314
3503
|
events,
|
|
@@ -3319,7 +3508,8 @@ function createServices(ctx) {
|
|
|
3319
3508
|
geometry,
|
|
3320
3509
|
authority: createAuthority(ctx, store),
|
|
3321
3510
|
filePicker: createFilePickerPort(ctx),
|
|
3322
|
-
fonts
|
|
3511
|
+
fonts,
|
|
3512
|
+
textFonts: createTextFonts(ctx, store, fonts),
|
|
3323
3513
|
tools,
|
|
3324
3514
|
behaviors: createBehaviors(store),
|
|
3325
3515
|
afterCreate: createAfterCreate(ctx, {
|
|
@@ -3329,6 +3519,24 @@ function createServices(ctx) {
|
|
|
3329
3519
|
})
|
|
3330
3520
|
};
|
|
3331
3521
|
}
|
|
3522
|
+
/** The fonts a box can name: the fonts plugin's list, mapped once per list, else the registered ones. */
|
|
3523
|
+
function createFontLookup(ctx) {
|
|
3524
|
+
let mapped = null;
|
|
3525
|
+
return () => {
|
|
3526
|
+
const list = ctx.tryGet(_embedpdf_plugin_fonts_contract.FontsToken)?.getFontList();
|
|
3527
|
+
if (!list) return (0, _embedpdf_engine_core_runtime.isLocalEngine)(ctx.engine) ? ctx.engine.fonts.list() : [];
|
|
3528
|
+
if (mapped?.list !== list) mapped = {
|
|
3529
|
+
list,
|
|
3530
|
+
fonts: list.fonts.filter((font) => font.source !== "standard").map(({ name, familyName, weight, italic }) => ({
|
|
3531
|
+
key: name,
|
|
3532
|
+
familyName,
|
|
3533
|
+
weight,
|
|
3534
|
+
italic
|
|
3535
|
+
}))
|
|
3536
|
+
};
|
|
3537
|
+
return mapped.fonts;
|
|
3538
|
+
};
|
|
3539
|
+
}
|
|
3332
3540
|
//#endregion
|
|
3333
3541
|
//#region src/services/announce.ts
|
|
3334
3542
|
/**
|
|
@@ -4981,7 +5189,7 @@ function createStamps(ctx, { store, geometry, tools, filePicker, afterCreate })
|
|
|
4981
5189
|
* typed after that goes into a new one.
|
|
4982
5190
|
*/
|
|
4983
5191
|
const TEXT_WRITE_DELAY_MS = 250;
|
|
4984
|
-
function createTextEditing(ctx, { store, fonts }, chrome) {
|
|
5192
|
+
function createTextEditing(ctx, { store, fonts, textFonts }, chrome) {
|
|
4985
5193
|
/** What is being typed: the record, the change it goes into, how it settles, and the pause before it is sent. */
|
|
4986
5194
|
let typing = null;
|
|
4987
5195
|
/** Send what was typed now. Resolves once the engine answered it; never rejects. */
|
|
@@ -5007,6 +5215,7 @@ function createTextEditing(ctx, { store, fonts }, chrome) {
|
|
|
5007
5215
|
});
|
|
5008
5216
|
const current = typing;
|
|
5009
5217
|
const commit = store.commit(message, { into: current.hold });
|
|
5218
|
+
textFonts.typed(id);
|
|
5010
5219
|
current.written = commit.written;
|
|
5011
5220
|
current.cancelPause();
|
|
5012
5221
|
current.cancelPause = ctx.clock.after(TEXT_WRITE_DELAY_MS, () => {
|
|
@@ -5088,7 +5297,8 @@ function createTextEditing(ctx, { store, fonts }, chrome) {
|
|
|
5088
5297
|
if (previous === null === (next === null) && (!previous || !next || previous.id === next.id && previous.start === next.start && previous.end === next.end)) return;
|
|
5089
5298
|
ctx.state.update(setTextSelection, next);
|
|
5090
5299
|
},
|
|
5091
|
-
getCssFontFamily: (
|
|
5300
|
+
getCssFontFamily: (face, id) => textFonts.faceCss(id, face).fontFamily,
|
|
5301
|
+
getFamilyOfCss: (cssFamily) => textFonts.familyOfCss(cssFamily) ?? cssFamily
|
|
5092
5302
|
}
|
|
5093
5303
|
};
|
|
5094
5304
|
}
|
|
@@ -5209,7 +5419,8 @@ function createAnnotationController(ctx) {
|
|
|
5209
5419
|
* optionally uses the selection plugin: shapes and ink work without it, text
|
|
5210
5420
|
* markup lights up only when it is present. The engine's pictures of its
|
|
5211
5421
|
* annotations come through the render plugin; without it, what draws live
|
|
5212
|
-
* still draws.
|
|
5422
|
+
* still draws. With the fonts plugin, text being typed shows in the font
|
|
5423
|
+
* programs the engine writes with. `config` is the settings the app
|
|
5213
5424
|
* registers, over {@link ANNOTATION_DEFAULTS}.
|
|
5214
5425
|
*/
|
|
5215
5426
|
const annotationPlugin = (config) => (0, _embedpdf_core.definePlugin)({
|
|
@@ -5220,7 +5431,8 @@ const annotationPlugin = (config) => (0, _embedpdf_core.definePlugin)({
|
|
|
5220
5431
|
optional: [
|
|
5221
5432
|
_embedpdf_plugin_selection_contract_host.SelectionToken,
|
|
5222
5433
|
_embedpdf_plugin_actions_contract.ActionsToken,
|
|
5223
|
-
_embedpdf_plugin_render_contract.RenderToken
|
|
5434
|
+
_embedpdf_plugin_render_contract.RenderToken,
|
|
5435
|
+
_embedpdf_plugin_fonts_contract.FontsToken
|
|
5224
5436
|
],
|
|
5225
5437
|
state: initialAnnotationModel,
|
|
5226
5438
|
settings: {
|