Skip to content

The VS Code of video editing, built for agents.

TypeScript

Refactor caption handling and improve styling logic

konstantin-paulus21a689708b2e
diff --git a/apps/web/src/components/sidebar-right/inspector/caption-settings.tsx b/apps/web/src/components/sidebar-right/inspector/caption-settings.tsx
index e177497f..659c7a69 100644
--- a/apps/web/src/components/sidebar-right/inspector/caption-settings.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/caption-settings.tsx
@@ -43,10 +43,10 @@ type CaptionSettingsProps = {
/**
* What a `<captions>` element says for itself: which preset draws it
* (`preset`) and the colors filling that preset's slots (`colors`).
- * Everything else about a caption's look — the
- * font, the box, the paints and shadows it is drawn with — is the preset's,
- * authored onto the entity by its decoder and not in the file at all, which
- * is why this panel is as short as it is.
+ * Everything else about a caption's look is the preset's base coat — the
+ * document writes it onto the entity and lets authored style props (font,
+ * position) overwrite it — and not in the file at all unless the user edits
+ * it, which is why this panel is as short as it is.
*/
export function CaptionSettings(props: CaptionSettingsProps) {
const editor = useEditor();
diff --git a/apps/web/src/components/sidebar-right/inspector/inspector.tsx b/apps/web/src/components/sidebar-right/inspector/inspector.tsx
index 1fa53465..62270225 100644
--- a/apps/web/src/components/sidebar-right/inspector/inspector.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/inspector.tsx
@@ -131,7 +131,7 @@ export function Inspector() {
<TransformSettings selection={nodes()} />
</Show>
- <Show when={includesTarget("shape", "text", "audio", "scene", "caption", "mask")}>
+ <Show when={includesTarget("shape", "text", "audio", "scene", "mask")}>
<LayoutPanel selection={nodes()} />
</Show>
@@ -147,11 +147,11 @@ export function Inspector() {
<TextPanel selection={nodes()} />
</Show>
- <Show when={includesTarget("shape", "text", "scene", "caption")}>
+ <Show when={includesTarget("shape", "text", "scene")}>
<FillsSettings selection={nodes()} />
</Show>
- <Show when={includesTarget("shape")}>
+ <Show when={includesTarget("shape", "caption")}>
<SourceSettings selection={nodes()} />
</Show>
@@ -159,7 +159,7 @@ export function Inspector() {
<StrokesSettings selection={nodes()} />
</Show>
- <Show when={includesTarget("shape", "text", "scene", "caption")}>
+ <Show when={includesTarget("shape", "text", "scene")}>
<ShadowsSettings selection={nodes()} />
</Show>
diff --git a/apps/web/src/components/sidebar-right/inspector/text.tsx b/apps/web/src/components/sidebar-right/inspector/text.tsx
index 3ef00c64..fd8e4b89 100644
--- a/apps/web/src/components/sidebar-right/inspector/text.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/text.tsx
@@ -252,13 +252,15 @@ export function TextPanel(props: TextPanelProps) {
/>
</ControlRow>
- <ControlRow label="Grow">
- <SegmentedIconTabs
- value={resizeMode}
- onChange={handleResizeModeChange}
- items={RESIZE_MODE_ITEMS}
- />
- </ControlRow>
+ <Show when={textSelected()}>
+ <ControlRow label="Grow">
+ <SegmentedIconTabs
+ value={resizeMode}
+ onChange={handleResizeModeChange}
+ items={RESIZE_MODE_ITEMS}
+ />
+ </ControlRow>
+ </Show>
<ControlRow label="Align" contentClass="flex gap-2">
<SegmentedIconTabs
diff --git a/packages/reconciler/src/document.ts b/packages/reconciler/src/document.ts
index 12aaa407..8bdd4b3b 100644
--- a/packages/reconciler/src/document.ts
+++ b/packages/reconciler/src/document.ts
@@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-import { Active, AdjustmentLayer, Animation, AnimationPhase, AnimationType, appendChild, AssetId, Audio, Background, bindAsset, BlendMode, BlendModeType, Blur, Caption, CaptionAlign, CaptionType, Chars, ClipHeight, ClipsContent, Computed, CornerRadius, createEntity, DEFAULT_BACKGROUND, Color, ColorStop, Delay, Effect, EffectType, Expanded, FontStyle, FramePromises, FrameRate, Generating, GenerationRequest, getActiveEntity, Loop, LoadRequest, Geometry, GeometryType, getEntityTree, getParentEntity, getParentNode, Hidden, Host, IsMask, isText, ItemIndex, KeepAspectRatio, Keyframe, KeyframeTrack, MixedCornerRadius, Muted, Name, Offset, Opacity, Paint, PaintType, parseColor, PendingSource, PendingSync, Playback, PlaybackRate, Position, removeChild, RenderSurface, resizeEntity, Scale, ScaleMode, ScaleModeType, secondsToFrames, getAsset, getEntityChildren, Group, Sequential, Shader, Size, Stage, Root, Rotation, Scene, Selected, Shadow, Source, SourceError, SourceFrameRate, SourceModifiers, hasModifier, setCameraMatrix, Stroke, StrokeCap, StrokeJoin, StrokeStyle, SyncRequest, TextAlign, TextBaseline, TextCase, TextRange, TextStyle, TranscriptionRequest, Transition, TransitionType, Trim, UniformScale, Volume, Workarea } from '@diffusionstudio/runtime';
+import { Active, AdjustmentLayer, Animation, AnimationPhase, AnimationType, appendChild, AssetId, Audio, Background, bindAsset, BlendMode, BlendModeType, Blur, Caption, CaptionAlign, CAPTION_PRESET_FILLS, CAPTION_PRESET_STYLES, CaptionType, Chars, ClipHeight, ClipsContent, Computed, CornerRadius, createEntity, DEFAULT_BACKGROUND, Color, ColorStop, Delay, Effect, EffectType, Expanded, FontStyle, FramePromises, FrameRate, Generating, GenerationRequest, getActiveEntity, Loop, LoadRequest, Geometry, GeometryType, getEntityTree, getParentEntity, getParentNode, Hidden, Host, IsMask, isText, ItemIndex, KeepAspectRatio, Keyframe, KeyframeTrack, MixedCornerRadius, Muted, Name, Offset, Opacity, Paint, PaintType, parseColor, PendingSource, PendingSync, Playback, PlaybackRate, Position, removeChild, RenderSurface, resizeEntity, Scale, ScaleMode, ScaleModeType, secondsToFrames, getAsset, getEntityChildren, Group, Sequential, Shader, Size, Stage, Root, Rotation, Scene, Selected, Shadow, Source, SourceError, SourceFrameRate, SourceModifiers, hasModifier, setCameraMatrix, Stroke, StrokeCap, StrokeJoin, StrokeStyle, SyncRequest, TextAlign, TextBaseline, TextCase, TextRange, TextStyle, TranscriptionRequest, Transition, TransitionType, Trim, UniformScale, Volume, Workarea } from '@diffusionstudio/runtime';
import { LOOP_ATTR, parseTime, SOURCE_ATTR } from '@diffusionstudio/jsx';
import { createSignal } from 'solid-js';
import { SVGElements } from 'solid-js/web';
@@ -328,6 +328,23 @@ const FONT_WEIGHTS: Record<string, string> = {
bold: '700',
};
+// The authored props that overwrite a caption preset's base coat (its
+// TextStyle and its intrinsic `fill`). The `preset` handler re-runs them
+// after writing the preset's defaults: props are not applied in authored
+// order, so some may have landed before it.
+const CAPTION_OVERRIDE_PROPS = [
+ 'fontSize',
+ 'fontFamily',
+ 'fontWeight',
+ 'fontStyle',
+ 'textAlign',
+ 'textBaseline',
+ 'textCase',
+ 'letterSpacing',
+ 'leading',
+ 'fill',
+] as const;
+
/**
* A numeric prop's value, or undefined for none. A boolean is none, not 0
@@ -470,6 +487,10 @@ export class RuntimeDocument implements ProjectDocument<SceneNode> {
entity.add(Chars);
entity.add(Caption);
entity.add(TranscriptionRequest);
+ entity.add(TextStyle);
+ entity.set(TextStyle, CAPTION_PRESET_STYLES[CaptionType.CLASSIC]);
+ entity.add(Color);
+ entity.set(Color, { value: CAPTION_PRESET_FILLS[CaptionType.CLASSIC]! });
break;
}
case 'adjustmentLayer': {
@@ -1264,7 +1285,25 @@ export class RuntimeDocument implements ProjectDocument<SceneNode> {
case 'preset': {
if (!entity.has(Caption)) return;
const preset = typeof value === 'string' ? CAPTION_PRESETS[value] : undefined;
- entity.set(Caption, { type: preset ?? CaptionType.CLASSIC });
+ const type = preset ?? CaptionType.CLASSIC;
+ entity.set(Caption, { type });
+
+ // The preset is the caption's base coat: write its styles and
+ // its intrinsic fill, then re-run the authored overrides that
+ // may already have been applied before this prop was.
+ entity.add(TextStyle);
+ entity.set(TextStyle, CAPTION_PRESET_STYLES[type]);
+ const fill = CAPTION_PRESET_FILLS[type];
+ if (fill === undefined) {
+ entity.remove(Color);
+ } else {
+ entity.add(Color);
+ entity.set(Color, { value: fill });
+ }
+ for (const prop of CAPTION_OVERRIDE_PROPS) {
+ const authored = node.props[prop];
+ if (authored !== undefined) this.setProperty(node, prop, authored);
+ }
return;
}
case 'colors': {
diff --git a/packages/runtime/src/media/caption/cascade.ts b/packages/runtime/src/media/caption/cascade.ts
index 4953c035..6515b845 100644
--- a/packages/runtime/src/media/caption/cascade.ts
+++ b/packages/runtime/src/media/caption/cascade.ts
@@ -2,26 +2,38 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-import { CaptionAlign, CaptionType, PaintType, FontStyle, TextAlign, TextBaseline, TextCase } from '../../constants';
-import { Paint, Color, TextStyle } from '../../traits';
+import { CaptionAlign, CaptionType, FontStyle, TextAlign, TextBaseline, TextCase } from '../../constants';
import { renderText } from '../../utils/text';
import { loadWebFont } from '../../fonts/utils';
import { groupBy, findActiveGroup, resolveTranscript, setChars } from './utils';
import { placeCaption } from './position';
-import { createEntity } from '../../actions/entities';
-import { appendChild } from '../../actions/hierarchy';
import type { Entity, World } from 'koota';
import type { Asset } from '@diffusionstudio/assets';
-import type { CaptionDecoder } from './types';
+import type { CaptionDecoder, CaptionPresetStyle } from './types';
const WIDTH = 800;
const HEIGHT = 200;
+// The preset's base TextStyle; the document writes it and authored style
+// props overwrite it (see CAPTION_PRESET_STYLES).
+export const CASCADE_TEXT_STYLE = {
+ fontFamily: 'Inter',
+ fontWeight: '300',
+ fontSize: 50,
+ textAlign: TextAlign.LEFT,
+ textBaseline: TextBaseline.TOP,
+ fontStyle: FontStyle.NORMAL,
+ textCase: TextCase.ORIGINAL,
+ leading: 1.2,
+ letterSpacing: undefined,
+} as const satisfies CaptionPresetStyle;
+
export class CascadeCaptionDecoder implements CaptionDecoder {
public readonly type = CaptionType.CASCADE;
public groups: ReturnType<typeof groupBy> = [];
public ready = false;
+ public styled = false;
private readonly asset: Asset;
private currentText = '';
@@ -42,29 +54,11 @@ export class CascadeCaptionDecoder implements CaptionDecoder {
return placeCaption(world, entity, { width: WIDTH, height: HEIGHT, x: 100, defaultAlign: CaptionAlign.BOTTOM });
}
- public applyStyles(world: World, entity: Entity): void {
- if (!this.reposition(world, entity)) return;
-
- entity.add(TextStyle);
- entity.set(TextStyle, {
- fontFamily: 'Inter',
- fontWeight: '300',
- fontSize: 50,
- textAlign: TextAlign.LEFT,
- textBaseline: TextBaseline.TOP,
- fontStyle: FontStyle.NORMAL,
- textCase: TextCase.ORIGINAL,
- leading: 1.2,
- });
-
- const fill = createEntity(world);
- fill.add(Paint);
- fill.set(Paint, { value: PaintType.SOLID });
- fill.add(Color);
- fill.set(Color, { value: 0xFFFFFF });
- appendChild(world, fill, entity);
-
- loadWebFont(world, 'Inter');
+ public applyStyles(world: World, entity: Entity): boolean {
+ if (!this.reposition(world, entity)) return false;
+
+ loadWebFont(world, CASCADE_TEXT_STYLE.fontFamily);
+ return true;
}
public seekTo(world: World, entity: Entity, relativeTime: number): void {
diff --git a/packages/runtime/src/media/caption/classic.ts b/packages/runtime/src/media/caption/classic.ts
index 1bf029c6..c6d596cc 100644
--- a/packages/runtime/src/media/caption/classic.ts
+++ b/packages/runtime/src/media/caption/classic.ts
@@ -2,8 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-import { CaptionAlign, CaptionType, PaintType, FontStyle, TextAlign, TextBaseline, TextCase } from '../../constants';
-import { Paint, Color, Shadow, Opacity, Blur, Offset, TextStyle } from '../../traits';
+import { CaptionAlign, CaptionType, FontStyle, TextAlign, TextBaseline, TextCase } from '../../constants';
+import { Color, Shadow, Opacity, Blur, Offset } from '../../traits';
import { renderText } from '../../utils/text';
import { loadWebFont } from '../../fonts/utils';
import { groupBy, findActiveGroup, resolveTranscript, setChars } from './utils';
@@ -13,15 +13,30 @@ import { appendChild } from '../../actions/hierarchy';
import type { Entity, World } from 'koota';
import type { Asset } from '@diffusionstudio/assets';
-import type { CaptionDecoder } from './types';
+import type { CaptionDecoder, CaptionPresetStyle } from './types';
export const CLASSIC_PRESET_WIDTH = 600;
export const CLASSIC_PRESET_HEIGHT = 100;
+// The preset's base TextStyle; the document writes it and authored style
+// props overwrite it (see CAPTION_PRESET_STYLES).
+export const CLASSIC_TEXT_STYLE = {
+ fontFamily: 'Urbanist',
+ fontWeight: '600',
+ fontSize: 62,
+ textAlign: TextAlign.CENTER,
+ textBaseline: TextBaseline.MIDDLE,
+ textCase: TextCase.LOWER,
+ fontStyle: FontStyle.NORMAL,
+ leading: 1,
+ letterSpacing: undefined,
+} as const satisfies CaptionPresetStyle;
+
export class ClassicCaptionDecoder implements CaptionDecoder {
public readonly type = CaptionType.CLASSIC;
public groups: ReturnType<typeof groupBy> = [];
public ready = false;
+ public styled = false;
private readonly asset: Asset;
private currentGroupIndex = -1;
@@ -46,27 +61,8 @@ export class ClassicCaptionDecoder implements CaptionDecoder {
});
}
- public applyStyles(world: World, entity: Entity): void {
- if (!this.reposition(world, entity)) return;
-
- entity.add(TextStyle);
- entity.set(TextStyle, {
- fontFamily: 'Urbanist',
- fontWeight: '600',
- fontSize: 62,
- textAlign: TextAlign.CENTER,
- textBaseline: TextBaseline.MIDDLE,
- textCase: TextCase.LOWER,
- fontStyle: FontStyle.NORMAL,
- leading: 1,
- });
-
- const fill = createEntity(world);
- fill.add(Paint);
- fill.set(Paint, { value: PaintType.SOLID });
- fill.add(Color);
- fill.set(Color, { value: 0xFFFFFF });
- appendChild(world, fill, entity);
+ public applyStyles(world: World, entity: Entity): boolean {
+ if (!this.reposition(world, entity)) return false;
const shadow = createEntity(world);
shadow.add(Shadow);
@@ -80,7 +76,8 @@ export class ClassicCaptionDecoder implements CaptionDecoder {
shadow.set(Offset, { x: 0, y: 5 });
appendChild(world, shadow, entity);
- loadWebFont(world, 'Urbanist');
+ loadWebFont(world, CLASSIC_TEXT_STYLE.fontFamily);
+ return true;
}
public seekTo(world: World, entity: Entity, relativeTime: number): void {
diff --git a/packages/runtime/src/media/caption/guinea.ts b/packages/runtime/src/media/caption/guinea.ts
index e16d3226..146bda41 100644
--- a/packages/runtime/src/media/caption/guinea.ts
+++ b/packages/runtime/src/media/caption/guinea.ts
@@ -14,11 +14,25 @@ import { appendChild } from '../../actions/hierarchy';
import type { Entity, World } from 'koota';
import type { Asset } from '@diffusionstudio/assets';
-import type { CaptionDecoder } from './types';
+import type { CaptionDecoder, CaptionPresetStyle } from './types';
const WIDTH = 700;
const HEIGHT = 200;
+// The preset's base TextStyle; the document writes it and authored style
+// props overwrite it (see CAPTION_PRESET_STYLES).
+export const GUINEA_TEXT_STYLE = {
+ fontFamily: 'The Bold Font',
+ fontWeight: '500',
+ fontSize: 62,
+ textAlign: TextAlign.CENTER,
+ textBaseline: TextBaseline.MIDDLE,
+ textCase: TextCase.UPPER,
+ fontStyle: FontStyle.NORMAL,
+ leading: 1,
+ letterSpacing: undefined,
+} as const satisfies CaptionPresetStyle;
+
const HIGHLIGHT_COLOR_0 = 0xF55353;
const HIGHLIGHT_COLOR_1 = 0xFEB139;
const HIGHLIGHT_COLOR_2 = 0xF6F54D;
@@ -29,6 +43,7 @@ export class GuineaCaptionDecoder implements CaptionDecoder {
public readonly type = CaptionType.GUINEA;
public groups: ReturnType<typeof groupBy> = [];
public ready = false;
+ public styled = false;
private readonly asset: Asset;
private currentGroupIndex = -1;
@@ -52,29 +67,11 @@ export class GuineaCaptionDecoder implements CaptionDecoder {
return placeCaption(world, entity, { width: WIDTH, height: HEIGHT, defaultAlign: CaptionAlign.CENTER });
}
- public applyStyles(world: World, entity: Entity): void {
- if (!this.reposition(world, entity)) return;
-
- entity.add(TextStyle);
- entity.set(TextStyle, {
- fontFamily: 'The Bold Font',
- fontWeight: '500',
- fontSize: 62,
- textAlign: TextAlign.CENTER,
- textBaseline: TextBaseline.MIDDLE,
- textCase: TextCase.UPPER,
- fontStyle: FontStyle.NORMAL,
- leading: 1,
- });
-
- const fill = createEntity(world);
- fill.add(Paint);
- fill.set(Paint, { value: PaintType.SOLID });
- fill.add(Color);
- fill.set(Color, { value: 0xFFFFFF });
- appendChild(world, fill, entity);
-
- loadWebFont(world, 'The Bold Font');
+ public applyStyles(world: World, entity: Entity): boolean {
+ if (!this.reposition(world, entity)) return false;
+
+ loadWebFont(world, GUINEA_TEXT_STYLE.fontFamily);
+ return true;
}
public seekTo(world: World, entity: Entity, relativeTime: number): void {
diff --git a/packages/runtime/src/media/caption/index.ts b/packages/runtime/src/media/caption/index.ts
index 0782eef2..81fbf2ab 100644
--- a/packages/runtime/src/media/caption/index.ts
+++ b/packages/runtime/src/media/caption/index.ts
@@ -3,22 +3,22 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { CaptionType } from '../../constants';
-import { AssetId, Caption, ChildOf, Paint, Shadow, TextRange, TextStyle, CaptionDecoderHandle } from '../../traits';
+import { AssetId, Caption, ChildOf, Paint, Shadow, Source, TextRange, CaptionDecoderHandle } from '../../traits';
import { getAsset } from '../../actions/assets';
import { deleteEntity } from '../../actions/entities';
-import { ClassicCaptionDecoder } from './classic';
-import { CascadeCaptionDecoder } from './cascade';
-import { SpotlightCaptionDecoder } from './spotlight';
-import { WhisperCaptionDecoder } from './whisper';
-import { PaperCaptionDecoder } from './paper';
-import { GuineaCaptionDecoder } from './guinea';
-import { StarkCaptionDecoder } from './stark';
+import { ClassicCaptionDecoder, CLASSIC_TEXT_STYLE } from './classic';
+import { CascadeCaptionDecoder, CASCADE_TEXT_STYLE } from './cascade';
+import { SpotlightCaptionDecoder, SPOTLIGHT_TEXT_STYLE } from './spotlight';
+import { WhisperCaptionDecoder, WHISPER_TEXT_STYLE } from './whisper';
+import { PaperCaptionDecoder, PAPER_TEXT_STYLE } from './paper';
+import { GuineaCaptionDecoder, GUINEA_TEXT_STYLE } from './guinea';
+import { StarkCaptionDecoder, STARK_TEXT_STYLE } from './stark';
import type { Entity, World } from 'koota';
import type { Asset } from '@diffusionstudio/assets';
-import type { CaptionDecoder } from './types';
+import type { CaptionDecoder, CaptionPresetStyle } from './types';
-export type { CaptionDecoder } from './types';
+export type { CaptionDecoder, CaptionPresetStyle } from './types';
export { ClassicCaptionDecoder, CLASSIC_PRESET_WIDTH, CLASSIC_PRESET_HEIGHT } from './classic';
export { CascadeCaptionDecoder } from './cascade';
export { SpotlightCaptionDecoder } from './spotlight';
@@ -30,6 +30,39 @@ export * from './position';
export * from './subtitles';
export * from './utils';
+/**
+ * Each preset's base TextStyle, keyed by type. The document writes these onto
+ * a `<captions>` entity when the preset is (re)applied, then re-runs the
+ * element's authored style props over them — so the preset is the base coat
+ * and everything the file says overwrites it.
+ */
+export const CAPTION_PRESET_STYLES: Record<CaptionType, CaptionPresetStyle> = {
+ [CaptionType.CLASSIC]: CLASSIC_TEXT_STYLE,
+ [CaptionType.CASCADE]: CASCADE_TEXT_STYLE,
+ [CaptionType.SPOTLIGHT]: SPOTLIGHT_TEXT_STYLE,
+ [CaptionType.WHISPER]: WHISPER_TEXT_STYLE,
+ [CaptionType.PAPER]: PAPER_TEXT_STYLE,
+ [CaptionType.GUINEA]: GUINEA_TEXT_STYLE,
+ [CaptionType.STARK]: STARK_TEXT_STYLE,
+};
+
+/**
+ * Each preset's intrinsic base fill: the Color the document seeds the
+ * element with, drawn beneath its paint children, which the authored `fill`
+ * prop overwrites — so a recolored caption survives a reload. Stark has
+ * none: its base coat is a DIFFERENCE-blend fill child its decoder makes,
+ * which an intrinsic solid cannot express.
+ */
+export const CAPTION_PRESET_FILLS: Record<CaptionType, number | undefined> = {
+ [CaptionType.CLASSIC]: 0xFFFFFF,
+ [CaptionType.CASCADE]: 0xFFFFFF,
+ [CaptionType.SPOTLIGHT]: 0xFFFFFF,
+ [CaptionType.WHISPER]: 0xFFFFFF,
+ [CaptionType.PAPER]: 0xFFFFFF,
+ [CaptionType.GUINEA]: 0xFFFFFF,
+ [CaptionType.STARK]: undefined,
+};
+
function createCaptionDecoder(type: CaptionType, asset: Asset): CaptionDecoder {
switch (type) {
case CaptionType.CLASSIC:
@@ -53,18 +86,21 @@ function createCaptionDecoder(type: CaptionType, asset: Asset): CaptionDecoder {
/**
* Drops what the last preset authored onto the entity: the paints and shadows
- * it draws with, whatever text ranges it left behind, and its TextStyle. The
- * look of a caption is the preset's, so the next one starts from a bare node
- * instead of stacking on it. Animations and keyframe tracks are the file's and
- * stay.
+ * it draws with and whatever text ranges it left behind, so the next preset's
+ * fills don't stack on them. The TextStyle stays — it is the document's, which
+ * rewrites it from CAPTION_PRESET_STYLES (plus the authored overrides)
+ * whenever the preset prop changes. Animations and keyframe tracks are the
+ * file's and stay.
*/
function clearPresetStyling(world: World, entity: Entity): void {
for (const child of world.query(ChildOf(entity))) {
+ // A child with a Source is the file's (an authored stroke, say), not
+ // the preset's; only what a decoder made is cleared.
+ if (child.has(Source)) continue;
if (child.has(Paint) || child.has(Shadow) || child.has(TextRange)) {
deleteEntity(world, child);
}
}
- entity.remove(TextStyle);
}
/**
@@ -79,7 +115,12 @@ export function resolveCaptionDecoder(world: World, entity: Entity): CaptionDeco
const captionType = entity.get(Caption)?.type ?? CaptionType.CLASSIC;
const existing = entity.get(CaptionDecoderHandle);
- if (existing && captionType === existing.type) return existing;
+ if (existing && captionType === existing.type) {
+ // A resolve before the entity was parented had nothing to place
+ // against; keep trying until placement lands.
+ if (!existing.styled) existing.styled = existing.applyStyles(world, entity);
+ return existing;
+ }
const typeChanged = existing != null;
existing?.dispose();
@@ -92,9 +133,7 @@ export function resolveCaptionDecoder(world: World, entity: Entity): CaptionDeco
entity.set(CaptionDecoderHandle, decoder);
if (typeChanged) clearPresetStyling(world, entity);
- if (typeChanged || !entity.has(TextStyle)) {
- decoder.applyStyles(world, entity);
- }
+ decoder.styled = decoder.applyStyles(world, entity);
return decoder;
}
diff --git a/packages/runtime/src/media/caption/paper.ts b/packages/runtime/src/media/caption/paper.ts
index 76f6fc61..8f849c72 100644
--- a/packages/runtime/src/media/caption/paper.ts
+++ b/packages/runtime/src/media/caption/paper.ts
@@ -2,8 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-import { CaptionAlign, CaptionType, PaintType, FontStyle, TextAlign, TextBaseline, TextCase } from '../../constants';
-import { Paint, Color, TextRange, TextStyle } from '../../traits';
+import { CaptionAlign, CaptionType, FontStyle, TextAlign, TextBaseline, TextCase } from '../../constants';
+import { TextRange, TextStyle } from '../../traits';
import { renderText } from '../../utils/text';
import { loadWebFont } from '../../fonts/utils';
import { groupBy, findActiveGroup, splitSequence, clearTextRanges, resolveTranscript, setChars } from './utils';
@@ -13,15 +13,30 @@ import { appendChild } from '../../actions/hierarchy';
import type { Entity, World } from 'koota';
import type { Asset } from '@diffusionstudio/assets';
-import type { CaptionDecoder } from './types';
+import type { CaptionDecoder, CaptionPresetStyle } from './types';
const WIDTH = 700;
const HEIGHT = 200;
+// The preset's base TextStyle; the document writes it and authored style
+// props overwrite it (see CAPTION_PRESET_STYLES).
+export const PAPER_TEXT_STYLE = {
+ fontFamily: 'Montserrat',
+ fontWeight: '300',
+ fontSize: 50,
+ textAlign: TextAlign.CENTER,
+ textBaseline: TextBaseline.MIDDLE,
+ textCase: TextCase.ORIGINAL,
+ leading: 0.9,
+ fontStyle: FontStyle.NORMAL,
+ letterSpacing: undefined,
+} as const satisfies CaptionPresetStyle;
+
export class PaperCaptionDecoder implements CaptionDecoder {
public readonly type = CaptionType.PAPER;
public groups: ReturnType<typeof groupBy> = [];
public ready = false;
+ public styled = false;
private readonly asset: Asset;
private currentGroupIndex = -1;
@@ -43,29 +58,11 @@ export class PaperCaptionDecoder implements CaptionDecoder {
return placeCaption(world, entity, { width: WIDTH, height: HEIGHT, defaultAlign: CaptionAlign.CENTER });
}
- public applyStyles(world: World, entity: Entity): void {
- if (!this.reposition(world, entity)) return;
-
- entity.add(TextStyle);
- entity.set(TextStyle, {
- fontFamily: 'Montserrat',
- fontWeight: '300',
- fontSize: 50,
- textAlign: TextAlign.CENTER,
- textBaseline: TextBaseline.MIDDLE,
- textCase: TextCase.ORIGINAL,
- leading: 0.9,
- fontStyle: FontStyle.NORMAL,
- });
-
- const fill = createEntity(world);
- fill.add(Paint);
- fill.set(Paint, { value: PaintType.SOLID });
- fill.add(Color);
- fill.set(Color, { value: 0xFFFFFF });
- appendChild(world, fill, entity);
-
- loadWebFont(world, 'Montserrat');
+ public applyStyles(world: World, entity: Entity): boolean {
+ if (!this.reposition(world, entity)) return false;
+
+ loadWebFont(world, PAPER_TEXT_STYLE.fontFamily);
+ return true;
}
public seekTo(world: World, entity: Entity, relativeTime: number): void {
diff --git a/packages/runtime/src/media/caption/position.ts b/packages/runtime/src/media/caption/position.ts
index 9d9be1c6..5219e224 100644
--- a/packages/runtime/src/media/caption/position.ts
+++ b/packages/runtime/src/media/caption/position.ts
@@ -4,7 +4,7 @@
import { store } from '../../world/store';
import { CaptionAlign } from '../../constants';
-import { Caption, Position, Computed } from '../../traits';
+import { Caption, Position, Computed, Host } from '../../traits';
import { getParentNode } from '../../queries/hierarchy';
import { resizeEntity } from '../../actions/resize';
@@ -14,11 +14,25 @@ import type { Entity, World } from 'koota';
export const CAPTION_MARGIN = 100;
/**
- * Sizes the caption box and places it within its parent. Horizontal placement
- * belongs to the preset (centered unless a fixed `x` is given); vertical
- * placement follows the entity's `Caption.verticalAlign`, falling back to the
- * preset's default. Returns false when the entity has no parent to place
- * against.
+ * A numeric authored prop's value, or undefined for none. A boolean is none:
+ * `false` is how an editor unsets a prop (see the reconciler's `toNumber`).
+ */
+function authoredNumber(value: unknown): number | undefined {
+ if (value === undefined || value === null || typeof value === 'boolean') {
+ return undefined;
+ }
+
+ const number = Number(value);
+ return Number.isFinite(number) ? number : undefined;
+}
+
+/**
+ * Sizes the caption box and places it within its parent. An authored `x`/`y`
+ * is the author's own placement and wins; the preset only places the axes the
+ * file does not say. Horizontal placement belongs to the preset (centered
+ * unless a fixed `x` is given); vertical placement follows the entity's
+ * `Caption.verticalAlign`, falling back to the preset's default. Returns
+ * false when the entity has no parent to place against.
*/
export function placeCaption(
world: World,
@@ -33,6 +47,10 @@ export function placeCaption(
resizeEntity(world, entity, { width: preset.width, height: preset.height });
+ const props = entity.get(Host)?.props;
+ const authoredX = authoredNumber(props?.x);
+ const authoredY = authoredNumber(props?.y);
+
const align = entity.get(Caption)?.verticalAlign ?? preset.defaultAlign;
const y = align === CaptionAlign.TOP
? CAPTION_MARGIN
@@ -42,8 +60,8 @@ export function placeCaption(
entity.add(Position);
entity.set(Position, {
- x: preset.x ?? (parentWidth - preset.width) / 2,
- y,
+ x: authoredX ?? preset.x ?? (parentWidth - preset.width) / 2,
+ y: authoredY ?? y,
});
return true;
}
diff --git a/packages/runtime/src/media/caption/spotlight.ts b/packages/runtime/src/media/caption/spotlight.ts
index 01e5db7c..2c092745 100644
--- a/packages/runtime/src/media/caption/spotlight.ts
+++ b/packages/runtime/src/media/caption/spotlight.ts
@@ -4,7 +4,7 @@
import { store } from '../../world/store';
import { CaptionAlign, CaptionType, PaintType, FontStyle, TextAlign, TextBaseline, TextCase } from '../../constants';
-import { Paint, Color, Caption, TextRange, TextStyle } from '../../traits';
+import { Paint, Color, Caption, TextRange } from '../../traits';
import { renderText } from '../../utils/text';
import { loadWebFont } from '../../fonts/utils';
import { groupBy, findActiveGroup, clearTextRanges, resolveTranscript, setChars } from './utils';
@@ -14,17 +14,32 @@ import { appendChild } from '../../actions/hierarchy';
import type { Entity, World } from 'koota';
import type { Asset } from '@diffusionstudio/assets';
-import type { CaptionDecoder } from './types';
+import type { CaptionDecoder, CaptionPresetStyle } from './types';
const WIDTH = 700;
const HEIGHT = 100;
const HIGHLIGHT_COLOR = 0x24D5FF;
+// The preset's base TextStyle; the document writes it and authored style
+// props overwrite it (see CAPTION_PRESET_STYLES).
+export const SPOTLIGHT_TEXT_STYLE = {
+ fontFamily: 'The Bold Font',
+ fontWeight: '500',
+ fontStyle: FontStyle.NORMAL,
+ fontSize: 70,
+ textAlign: TextAlign.CENTER,
+ textBaseline: TextBaseline.MIDDLE,
+ textCase: TextCase.ORIGINAL,
+ leading: 1,
+ letterSpacing: undefined,
+} as const satisfies CaptionPresetStyle;
+
export class SpotlightCaptionDecoder implements CaptionDecoder {
public readonly type = CaptionType.SPOTLIGHT;
public groups: ReturnType<typeof groupBy> = [];
public ready = false;
+ public styled = false;
private readonly asset: Asset;
private currentGroupIndex = -1;
@@ -47,29 +62,11 @@ export class SpotlightCaptionDecoder implements CaptionDecoder {
return placeCaption(world, entity, { width: WIDTH, height: HEIGHT, defaultAlign: CaptionAlign.CENTER });
}
- public applyStyles(world: World, entity: Entity): void {
- if (!this.reposition(world, entity)) return;
-
- entity.add(TextStyle);
- entity.set(TextStyle, {
- fontFamily: 'The Bold Font',
- fontWeight: '500',
- fontStyle: FontStyle.NORMAL,
- fontSize: 70,
- textAlign: TextAlign.CENTER,
- textBaseline: TextBaseline.MIDDLE,
- textCase: TextCase.ORIGINAL,
- leading: 1,
- });
-
- const fill = createEntity(world);
- fill.add(Paint);
- fill.set(Paint, { value: PaintType.SOLID });
- fill.add(Color);
- fill.set(Color, { value: 0xFFFFFF });
- appendChild(world, fill, entity);
-
- loadWebFont(world, 'The Bold Font');
+ public applyStyles(world: World, entity: Entity): boolean {
+ if (!this.reposition(world, entity)) return false;
+
+ loadWebFont(world, SPOTLIGHT_TEXT_STYLE.fontFamily);
+ return true;
}
public seekTo(world: World, entity: Entity, relativeTime: number): void {
diff --git a/packages/runtime/src/media/caption/stark.ts b/packages/runtime/src/media/caption/stark.ts
index ad7ac963..5e2fb207 100644
--- a/packages/runtime/src/media/caption/stark.ts
+++ b/packages/runtime/src/media/caption/stark.ts
@@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { BlendModeType, CaptionAlign, CaptionType, PaintType, FontStyle, TextAlign, TextBaseline, TextCase } from '../../constants';
-import { Paint, Color, BlendMode, TextStyle } from '../../traits';
+import { Paint, Color, BlendMode } from '../../traits';
import { renderText } from '../../utils/text';
import { loadWebFont } from '../../fonts/utils';
import { groupBy, findActiveGroup, resolveTranscript, setChars } from './utils';
@@ -13,15 +13,30 @@ import { appendChild } from '../../actions/hierarchy';
import type { Entity, World } from 'koota';
import type { Asset } from '@diffusionstudio/assets';
-import type { CaptionDecoder } from './types';
+import type { CaptionDecoder, CaptionPresetStyle } from './types';
const WIDTH = 700;
const HEIGHT = 100;
+// The preset's base TextStyle; the document writes it and authored style
+// props overwrite it (see CAPTION_PRESET_STYLES).
+export const STARK_TEXT_STYLE = {
+ fontFamily: 'Figtree',
+ fontWeight: '800',
+ fontSize: 70,
+ textAlign: TextAlign.CENTER,
+ textBaseline: TextBaseline.MIDDLE,
+ textCase: TextCase.UPPER,
+ fontStyle: FontStyle.NORMAL,
+ leading: 1,
+ letterSpacing: undefined,
+} as const satisfies CaptionPresetStyle;
+
export class StarkCaptionDecoder implements CaptionDecoder {
public readonly type = CaptionType.STARK;
public groups: ReturnType<typeof groupBy> = [];
public ready = false;
+ public styled = false;
private readonly asset: Asset;
private currentGroupIndex = -1;
@@ -42,20 +57,8 @@ export class StarkCaptionDecoder implements CaptionDecoder {
return placeCaption(world, entity, { width: WIDTH, height: HEIGHT, defaultAlign: CaptionAlign.CENTER });
}
- public applyStyles(world: World, entity: Entity): void {
- if (!this.reposition(world, entity)) return;
-
- entity.add(TextStyle);
- entity.set(TextStyle, {
- fontFamily: 'Figtree',
- fontWeight: '800',
- fontSize: 70,
- textAlign: TextAlign.CENTER,
- textBaseline: TextBaseline.MIDDLE,
- textCase: TextCase.UPPER,
- fontStyle: FontStyle.NORMAL,
- leading: 1,
- });
+ public applyStyles(world: World, entity: Entity): boolean {
+ if (!this.reposition(world, entity)) return false;
const fill = createEntity(world);
fill.add(Paint);
@@ -66,7 +69,8 @@ export class StarkCaptionDecoder implements CaptionDecoder {
fill.set(BlendMode, { value: BlendModeType.DIFFERENCE });
appendChild(world, fill, entity);
- loadWebFont(world, 'Figtree');
+ loadWebFont(world, STARK_TEXT_STYLE.fontFamily);
+ return true;
}
public seekTo(world: World, entity: Entity, relativeTime: number): void {
diff --git a/packages/runtime/src/media/caption/types.ts b/packages/runtime/src/media/caption/types.ts
index 402036e0..a662e16d 100644
--- a/packages/runtime/src/media/caption/types.ts
+++ b/packages/runtime/src/media/caption/types.ts
@@ -3,14 +3,34 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import type { Entity, World } from 'koota';
-import type { CaptionType } from '../../constants';
+import type { CaptionType, FontStyle, TextAlign, TextBaseline, TextCase } from '../../constants';
import type { WordGroup } from '@diffusionstudio/assets';
+/**
+ * A preset's complete base TextStyle. The document writes it onto the
+ * `<captions>` entity whenever the preset is (re)applied and then re-runs the
+ * element's authored style props over it, so an authored font survives both a
+ * reload and a preset switch. Complete on purpose: every field is spelled out
+ * (even as `undefined`) so switching presets resets what the last one set.
+ */
+export type CaptionPresetStyle = {
+ fontFamily: string;
+ fontWeight: string;
+ fontSize: number;
+ fontStyle: FontStyle;
+ textAlign: TextAlign;
+ textBaseline: TextBaseline;
+ textCase: TextCase;
+ leading: number;
+ letterSpacing: number | undefined;
+};
+
export interface CaptionDecoder {
readonly type: CaptionType;
groups: WordGroup[];
ready: boolean;
- applyStyles(world: World, entity: Entity): void;
+ styled: boolean;
+ applyStyles(world: World, entity: Entity): boolean;
reposition(world: World, entity: Entity): boolean;
seekTo(world: World, entity: Entity, relativeTime: number): void;
draw(world: World, entity: Entity): void;
diff --git a/packages/runtime/src/media/caption/whisper.ts b/packages/runtime/src/media/caption/whisper.ts
index 9bb04b95..eedddf3e 100644
--- a/packages/runtime/src/media/caption/whisper.ts
+++ b/packages/runtime/src/media/caption/whisper.ts
@@ -2,26 +2,38 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
-import { CaptionAlign, CaptionType, PaintType, FontStyle, TextAlign, TextBaseline, TextCase } from '../../constants';
-import { Paint, Color, TextStyle } from '../../traits';
+import { CaptionAlign, CaptionType, FontStyle, TextAlign, TextBaseline, TextCase } from '../../constants';
import { renderText } from '../../utils/text';
import { loadWebFont } from '../../fonts/utils';
import { groupBy, findActiveGroup, resolveTranscript, setChars } from './utils';
import { placeCaption } from './position';
-import { createEntity } from '../../actions/entities';
-import { appendChild } from '../../actions/hierarchy';
import type { Entity, World } from 'koota';
import type { Asset } from '@diffusionstudio/assets';
-import type { CaptionDecoder } from './types';
+import type { CaptionDecoder, CaptionPresetStyle } from './types';
const WIDTH = 1000;
const HEIGHT = 100;
+// The preset's base TextStyle; the document writes it and authored style
+// props overwrite it (see CAPTION_PRESET_STYLES).
+export const WHISPER_TEXT_STYLE = {
+ fontFamily: 'Montserrat',
+ fontWeight: '400',
+ fontSize: 40,
+ textAlign: TextAlign.CENTER,
+ textBaseline: TextBaseline.MIDDLE,
+ textCase: TextCase.ORIGINAL,
+ leading: 1.4,
+ fontStyle: FontStyle.NORMAL,
+ letterSpacing: undefined,
+} as const satisfies CaptionPresetStyle;
+
export class WhisperCaptionDecoder implements CaptionDecoder {
public readonly type = CaptionType.WHISPER;
public groups: ReturnType<typeof groupBy> = [];
public ready = false;
+ public styled = false;
private readonly asset: Asset;
private currentGroupIndex = -1;
@@ -43,29 +55,11 @@ export class WhisperCaptionDecoder implements CaptionDecoder {
return placeCaption(world, entity, { width: WIDTH, height: HEIGHT, defaultAlign: CaptionAlign.BOTTOM });
}
- public applyStyles(world: World, entity: Entity): void {
- if (!this.reposition(world, entity)) return;
-
- entity.add(TextStyle);
- entity.set(TextStyle, {
- fontFamily: 'Montserrat',
- fontWeight: '400',
- fontSize: 40,
- textAlign: TextAlign.CENTER,
- textBaseline: TextBaseline.MIDDLE,
- textCase: TextCase.ORIGINAL,
- leading: 1.4,
- fontStyle: FontStyle.NORMAL,
- });
-
- const fill = createEntity(world);
- fill.add(Paint);
- fill.set(Paint, { value: PaintType.SOLID });
- fill.add(Color);
- fill.set(Color, { value: 0xFFFFFF });
- appendChild(world, fill, entity);
-
- loadWebFont(world, 'Montserrat');
+ public applyStyles(world: World, entity: Entity): boolean {
+ if (!this.reposition(world, entity)) return false;
+
+ loadWebFont(world, WHISPER_TEXT_STYLE.fontFamily);
+ return true;
}
public seekTo(world: World, entity: Entity, relativeTime: number): void {
diff --git a/packages/runtime/src/systems/render.ts b/packages/runtime/src/systems/render.ts
index a7bc519b..6a39601d 100644
--- a/packages/runtime/src/systems/render.ts
+++ b/packages/runtime/src/systems/render.ts
@@ -810,9 +810,10 @@ export function renderNode(world: World, entity: Entity): void {
ctx.clip();
}
- // Opacity and blend mode
+ // Opacity and blend mode. The store slot may hold a destroyed entity's
+ // value (ids are recycled), so it is only readable behind has().
ctx.globalAlpha *= computed.opacity[eid]!;
- const bi = store(world, BlendMode).value[eid] ?? 0;
+ const bi = entity.has(BlendMode) ? store(world, BlendMode).value[eid] ?? 0 : 0;
if (bi !== 0) ctx.globalCompositeOperation = COMPOSITE_OPERATIONS[bi]!;
const effects = buildEffects(world, entity);
diff --git a/packages/runtime/src/utils/text.ts b/packages/runtime/src/utils/text.ts
index 93077832..9ba4a201 100644
--- a/packages/runtime/src/utils/text.ts
+++ b/packages/runtime/src/utils/text.ts
@@ -310,12 +310,14 @@ function renderTokens(ctx: Ctx, world: World, entity: Entity): void {
if (shadow.has(Hidden)) continue;
const sid = shadow.id();
- ctx.shadowOffsetX = (offsetStore.x[sid] ?? 0) * shadowScale;
- ctx.shadowOffsetY = (offsetStore.y[sid] ?? 0) * shadowScale;
- ctx.shadowBlur = (blurStore.value[sid] ?? 0) * shadowScale;
+ // Recycled-id safety: optional traits only behind has().
+ const hasOffset = shadow.has(Offset);
+ ctx.shadowOffsetX = (hasOffset ? offsetStore.x[sid] ?? 0 : 0) * shadowScale;
+ ctx.shadowOffsetY = (hasOffset ? offsetStore.y[sid] ?? 0 : 0) * shadowScale;
+ ctx.shadowBlur = (shadow.has(Blur) ? blurStore.value[sid] ?? 0 : 0) * shadowScale;
ctx.shadowColor = colorToHex(colorStore.value[sid] ?? 0x000000);
ctx.fillStyle = colorToHex(colorStore.value[sid] ?? 0x000000);
- ctx.globalAlpha = savedAlpha * (opacityStore.value[sid] ?? 1);
+ ctx.globalAlpha = savedAlpha * (shadow.has(Opacity) ? opacityStore.value[sid] ?? 1 : 1);
if (widest !== null) {
ctx.strokeText(word.chars, word.x, word.y);
@@ -353,12 +355,13 @@ function renderTokens(ctx: Ctx, world: World, entity: Entity): void {
if (stroke.has(Hidden)) continue;
const sid = stroke.id();
+ // Recycled-id safety: optional traits only behind has().
const savedCO = ctx.globalCompositeOperation;
- const blendMode = blendStore.value[sid] ?? 0;
+ const blendMode = stroke.has(BlendMode) ? blendStore.value[sid] ?? 0 : 0;
if (blendMode !== 0) {
ctx.globalCompositeOperation = COMPOSITE_OPERATIONS[blendMode]!;
}
- ctx.globalAlpha = savedAlpha * (opacityStore.value[sid] ?? 1);
+ ctx.globalAlpha = savedAlpha * (stroke.has(Opacity) ? opacityStore.value[sid] ?? 1 : 1);
applyStrokeStyle(ctx, world, stroke);
const paintType = paintStore.value[sid];
@@ -404,12 +407,14 @@ function renderTokens(ctx: Ctx, world: World, entity: Entity): void {
if (fill.has(Hidden)) continue;
const fid = fill.id();
+ // Store slots outlive destroyed entities and ids are recycled,
+ // so an optional trait's slot is only readable behind has().
const savedCO = ctx.globalCompositeOperation;
- const blendMode = blendStore.value[fid] ?? 0;
+ const blendMode = fill.has(BlendMode) ? blendStore.value[fid] ?? 0 : 0;
if (blendMode !== 0) {
ctx.globalCompositeOperation = COMPOSITE_OPERATIONS[blendMode]!;
}
- ctx.globalAlpha = savedAlpha * (opacityStore.value[fid] ?? 1);
+ ctx.globalAlpha = savedAlpha * (fill.has(Opacity) ? opacityStore.value[fid] ?? 1 : 1);
const paintType = paintStore.value[fid];
if (paintType === PaintType.LINEAR_GRADIENT) {