@alpha2-sh/editor 0.2.0 → 0.2.2

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.
@@ -7,6 +7,7 @@ import { ResourceRegistry } from "./resources.js";
7
7
  import { ExportJob, requireExportFrameRate } from "./jobs.js";
8
8
  import { attemptOptions } from "./runtime/budget.js";
9
9
  import { createWorkerPreview } from "./worker.js";
10
+ import { registerCaptionFonts } from "./runtime/caption-fonts.js";
10
11
  import { AssetLibrary, streamPeaks } from "./runtime/assets.js";
11
12
  import { SourcePixels, providerSettings, sameProviderSettings, } from "./runtime/sources.js";
12
13
  import { fingerprintFile } from "./runtime/fingerprint.js";
@@ -212,6 +213,8 @@ export function createBrowserBridge(options) {
212
213
  };
213
214
  /** The engine's asset decisions, from the wasm module, loaded on first use. */
214
215
  const decisions = async () => (decisionBinding ??= await loadWasm());
216
+ /** What `measureText` lays text out with, made on first use, and the font stack it holds. */
217
+ let measurer = null, measuredFonts = null;
215
218
  const registry = new ResourceRegistry(resourceLimit);
216
219
  /**
217
220
  * The runtimes this session holds, by {@link ownerKey}: a lease and the registry revision it was
@@ -240,6 +243,8 @@ export function createBrowserBridge(options) {
240
243
  let previewSelection = new Map();
241
244
  /** What that selection was decided from (see `loaded`). */
242
245
  let previewConformKey = null;
246
+ /** What preview prefers where a source has a fresh proxy (see `setPreviewMedia`). */
247
+ let preferredPreviewMedia = "original";
243
248
  const hashOf = (asset) => asset?.fingerprint?.content_sha256;
244
249
  const originalOf = (document, id) => {
245
250
  const descriptor = document.assets[id];
@@ -335,7 +340,10 @@ export function createBrowserBridge(options) {
335
340
  * A proxy this session makes is adopted directly.
336
341
  */
337
342
  let absentProxies = new Set();
338
- /** Finds the kept proxies of sources this session has no original for. */
343
+ /**
344
+ * Finds the kept proxies of sources this session has no original for, and of every source while
345
+ * preview prefers proxies.
346
+ */
339
347
  async function discoverProxies(document) {
340
348
  for (const source of document.project.sources) {
341
349
  const descriptor = document.assets[source.id];
@@ -343,7 +351,8 @@ export function createBrowserBridge(options) {
343
351
  if (!hash ||
344
352
  proxies.has(hash) ||
345
353
  absentProxies.has(hash) ||
346
- originalOf(document, source.id))
354
+ (preferredPreviewMedia === "original" &&
355
+ originalOf(document, source.id)))
347
356
  continue;
348
357
  // Held before it is read, so no page removes it between.
349
358
  const hold = await proxyStore.hold(hash);
@@ -683,13 +692,18 @@ export function createBrowserBridge(options) {
683
692
  const catalog = catalogOf(document);
684
693
  const conformKey = checked.revisionKey +
685
694
  JSON.stringify(previewOutput) +
695
+ preferredPreviewMedia +
686
696
  JSON.stringify(catalog);
697
+ const before = JSON.stringify([...previewSelection]);
687
698
  if (conformKey !== previewConformKey) {
688
- const selected = conformProject(await decisions(), compiled.project, catalog, "preview");
699
+ const selected = conformProject(await decisions(), compiled.project, catalog, preferredPreviewMedia === "proxies" ? "preview_proxy" : "preview");
689
700
  previewSelection = new Map(selected.sources.map((source) => [source.source_id, source.selection]));
690
701
  previewConformKey = conformKey;
691
702
  }
692
703
  const selectionKey = JSON.stringify([...previewSelection]);
704
+ // `proxyStatus` reports what preview reads.
705
+ if (selectionKey !== before)
706
+ notifyAssets();
693
707
  const used = shownSources(compiled.project);
694
708
  for (const source of compiled.project.sources)
695
709
  if (!drawnByEngine(source) &&
@@ -1763,6 +1777,8 @@ export function createBrowserBridge(options) {
1763
1777
  proxies.clear();
1764
1778
  registry.dispose();
1765
1779
  engine = null;
1780
+ measurer?.free();
1781
+ measurer = null;
1766
1782
  detachOwner();
1767
1783
  })();
1768
1784
  return disposal;
@@ -1863,10 +1879,26 @@ export function createBrowserBridge(options) {
1863
1879
  }
1864
1880
  return families;
1865
1881
  },
1882
+ async measureText(generator) {
1883
+ const { WasmTextMeasurer } = await loadWasm();
1884
+ if (disposed)
1885
+ throw new EditorError("disposed", "Bridge is disposed", "browser");
1886
+ // The fonts and canvas preview draws the draft with.
1887
+ const document = editor.draft;
1888
+ const fonts = fontStackOf(document);
1889
+ measurer ??= new WasmTextMeasurer();
1890
+ if (!sameList(measuredFonts, fonts)) {
1891
+ // A font the engine refuses is left out here as preview leaves it out, and reported there.
1892
+ registerCaptionFonts(measurer, fonts);
1893
+ measuredFonts = fonts;
1894
+ }
1895
+ const { width, height } = document.project.settings;
1896
+ return JSON.parse(measurer.measure(JSON.stringify(generator), width, height));
1897
+ },
1866
1898
  conform,
1867
1899
  /**
1868
1900
  * Makes a proxy of a source whose original this session holds, keeps it, and previews from it
1869
- * wherever the original goes offline.
1901
+ * wherever the original goes offline, or while preview prefers proxies.
1870
1902
  */
1871
1903
  async generateProxy(id, { signal } = {}) {
1872
1904
  const descriptor = editor.document.assets[id];
@@ -1951,6 +1983,24 @@ export function createBrowserBridge(options) {
1951
1983
  get previewOutput() {
1952
1984
  return previewOutput;
1953
1985
  },
1986
+ /**
1987
+ * Makes preview read a fresh proxy wherever a source has one (`proxies`), or the original
1988
+ * wherever it is online (`original`, the default). Preview only: an export reads what its own
1989
+ * `media` says.
1990
+ */
1991
+ setPreviewMedia(media) {
1992
+ if (media !== "original" && media !== "proxies")
1993
+ throw Error("Preview media must be original or proxies");
1994
+ if (media === preferredPreviewMedia)
1995
+ return;
1996
+ preferredPreviewMedia = media;
1997
+ renderKey = null;
1998
+ if (engine)
1999
+ void seek(tick).catch(onError);
2000
+ },
2001
+ get previewMedia() {
2002
+ return preferredPreviewMedia;
2003
+ },
1954
2004
  hasOriginal: (id) => !!originalOf(editor.document, id),
1955
2005
  /**
1956
2006
  * Which video codecs this browser encodes for an export of the current document at
@@ -2146,7 +2196,11 @@ export function createBrowserBridge(options) {
2146
2196
  get catalog() {
2147
2197
  return engine?.catalog ?? null;
2148
2198
  },
2149
- getAsset: (id) => library.get(id),
2199
+ // A source's own media while it is online, whichever preview reads: its size, file and peaks
2200
+ // are the original's.
2201
+ getAsset: (id) => (preferredPreviewMedia === "proxies" &&
2202
+ sourceView(editor.draft, id, originalOf(editor.draft, id))) ||
2203
+ library.get(id),
2150
2204
  };
2151
2205
  }
2152
2206
  /**
@@ -62,6 +62,7 @@ export function exportTimeline({ supplyFrame, renderFrame, sourceTransfer, engin
62
62
  source: string;
63
63
  };
64
64
  appliedVideoRateControl: import("alpha2/mediabunny").AppliedVideoRateControl | null;
65
+ videoLatencyMode: import("alpha2/mediabunny").VideoEncoderLatencyMode | null;
65
66
  audioCodec: "aac" | "opus" | undefined;
66
67
  audioRateControl: {
67
68
  bitrate: number;
@@ -386,6 +386,7 @@ export async function exportTimeline({ supplyFrame, renderFrame, sourceTransfer,
386
386
  },
387
387
  // `quantizer` when the encoder accepted constant-quantizer encoding, else the bitrate path.
388
388
  appliedVideoRateControl: sink.appliedVideoRateControl ?? null,
389
+ videoLatencyMode: sink.videoLatencyMode ?? null,
389
390
  audioCodec,
390
391
  audioRateControl: audioBitrate === undefined
391
392
  ? null
@@ -1,4 +1,4 @@
1
- import type { MediaFingerprint, Project } from "alpha2/project";
1
+ import type { Generator, MediaFingerprint, Project } from "alpha2/project";
2
2
  import type { AssetBinding, ConformReport, MediaPurpose } from "alpha2/assets";
3
3
  import type { BuiltinEffectCatalog } from "alpha2/effects";
4
4
  import type { EditorSession } from "../core/session.js";
@@ -273,6 +273,37 @@ export interface ProxyRemoval {
273
273
  * ticks (120000 per second). Methods that change the document dispatch through the session, so
274
274
  * they are undoable.
275
275
  */
276
+ /** A rectangle in fractions of the canvas, y down, as a text style's `position` is. */
277
+ export interface CanvasRect {
278
+ readonly left: number;
279
+ readonly top: number;
280
+ readonly right: number;
281
+ readonly bottom: number;
282
+ }
283
+ /**
284
+ * Where a text generator's text lies on the picture it draws, before its clip's transform and
285
+ * effects: what {@link BrowserBridge.measureText} answers.
286
+ */
287
+ export interface TextBounds {
288
+ /**
289
+ * The box the style's `anchor` and `position` place: `max_width` wide, every line's em box tall,
290
+ * and the whole pixels of outline and background padding around that.
291
+ */
292
+ readonly layout: CanvasRect;
293
+ /**
294
+ * From the start of the line that starts farthest left to the end of the one that ends farthest
295
+ * right, as tall as `layout`, with the same decorations around it. It moves only as the lines'
296
+ * advances do, not as glyphs with more or less ink are typed: steady handles for a text clip.
297
+ */
298
+ readonly content: CanvasRect;
299
+ /**
300
+ * What the text draws: its glyphs, their outline, and the background's boxes. Every pixel the
301
+ * frame gives the text meets it, give or take the pixel the texture's sampling spreads it by;
302
+ * `null` when the text draws nothing (only spaces).
303
+ */
304
+ readonly ink: CanvasRect | null;
305
+ readonly lineCount: number;
306
+ }
276
307
  export interface BrowserBridge {
277
308
  /** Where the preview renders: nowhere until `bindPreview`, then a worker or the main thread. */
278
309
  readonly previewBackend: "unbound" | "worker" | "main-thread";
@@ -302,9 +333,10 @@ export interface BrowserBridge {
302
333
  /** The engine's built-in effects and transitions; `null` until a preview is bound. */
303
334
  readonly catalog: BuiltinEffectCatalog | null;
304
335
  /**
305
- * What preview reads for source `id` of the draft: its original, or the kept proxy standing in
306
- * while the original is offline; opened with the provider settings the document names. Use {@link
307
- * BrowserBridge.hasOriginal} to ask whether the original itself is linked.
336
+ * Source `id` of the draft: its original while it is online, even where preview reads a proxy
337
+ * (see {@link BrowserBridge.setPreviewMedia}), else the kept proxy standing in for it; opened
338
+ * with the provider settings the document names. Use {@link BrowserBridge.hasOriginal} to ask
339
+ * whether the original itself is linked.
308
340
  */
309
341
  getAsset(id: string): RuntimeAsset | null;
310
342
  /**
@@ -417,6 +449,17 @@ export interface BrowserBridge {
417
449
  * name tables.
418
450
  */
419
451
  captionFontFamilies(): Promise<string[]>;
452
+ /**
453
+ * Where the engine draws a text generator's text (see {@link TextBounds}): laid out as preview
454
+ * lays it out, with the same fonts, at the draft's canvas size, without rendering. A clip's
455
+ * transform maps these rectangles as it maps the rest of its picture.
456
+ *
457
+ * Rejects a generator that is not a valid text generator (`INVALID_INPUT`), with no fonts to
458
+ * draw it, and once the bridge is disposed (`disposed`).
459
+ */
460
+ measureText(generator: Extract<Generator, {
461
+ type: "text";
462
+ }>): Promise<TextBounds>;
420
463
  /**
421
464
  * The files a native export of `document` (the current one by default) needs, for upload:
422
465
  * shown media (or their proxies with `media: "proxies"`), an image sequence's frames, font
@@ -481,6 +524,14 @@ export interface BrowserBridge {
481
524
  readonly compositionId: string;
482
525
  readonly nodeId: string;
483
526
  } | null;
527
+ /**
528
+ * Makes preview read a fresh proxy wherever a source has one (`proxies`), or the original
529
+ * wherever it is online (`original`, the default). Preview only: an export reads what its own
530
+ * `media` says. {@link BrowserBridge.proxyStatus} reports what each source reads.
531
+ */
532
+ setPreviewMedia(media: "original" | "proxies"): void;
533
+ /** What {@link BrowserBridge.setPreviewMedia} last set. */
534
+ readonly previewMedia: "original" | "proxies";
484
535
  /**
485
536
  * Shows another animation of a dotLottie source, keeping its clips. Throws `EditorError`
486
537
  * `unsupported_media` for another kind of source or an animation the package lacks.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alpha2-sh/editor",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Headless Project-based editing sessions and browser adapters for alpha2.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -60,11 +60,11 @@
60
60
  "build": "rm -rf dist && tsc -p tsconfig.json"
61
61
  },
62
62
  "peerDependencies": {
63
- "alpha2": "0.2.0",
63
+ "alpha2": "0.2.2",
64
64
  "react": "^19.3.0"
65
65
  },
66
66
  "devDependencies": {
67
- "alpha2": "0.2.0",
67
+ "alpha2": "0.2.2",
68
68
  "react": "19.3.0",
69
69
  "react-dom": "19.3.0",
70
70
  "@types/react": "19.3.0",