@motionhub/runtime 0.8.0 → 0.8.1

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.
Files changed (3) hide show
  1. package/README.md +3 -2
  2. package/index.js +23 -7
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -15,7 +15,7 @@ You do not use this package on its own: an engine binding provides it.
15
15
  | `useTokens()` | The motion's `tokens.json` resolved: `color.ink` (a CSS colour), `font.display` (a CSS stack), `duration.light` (seconds), `easing.light` (`[x1, y1, x2, y2]`), `caption.*` |
16
16
  | `seededRandom(seed)` | A stream of numbers in [0, 1) for a string or number seed: the same seed draws the same picture in every engine |
17
17
  | `<Video src startAt trimBefore playbackRate objectFit>` | The engine's video: shown from `startAt` s of the sequence, its source from `trimBefore` s |
18
- | `<Caption line opacity>` | One spoken line, drawn from the `caption` tokens (font, size, weight, color, background, radius, padding-y, padding-x, bottom, max-width, shadow, rule-left or rule-top); with `line`, that line of the film's while it is said |
18
+ | `<Caption line opacity>` | One spoken line, drawn from the `caption` tokens (font, size, weight, color, background, radius, padding-y, padding-x, bottom, max-width, shadow, rule-left or rule-top; lengths in pixels, as `36` or `'36px'`; with neither color nor background, white on a dark ground; without max-width, at most 1500 px and 64 px from the frame's sides); with `line`, that line of the film's while it is said |
19
19
  | `<Sequence from duration>`, `interpolate(t, input, output, {easing})`, `Easing` | As Remotion's, in seconds: a span whose children count time from its start, a value mapped through a curve, and the curves (`Easing.bezier` takes a cubicBezier token). For composing components, as a reel does |
20
20
  | `<MotionRoot tokens>` | A full-frame box carrying the tokens as CSS custom properties |
21
21
  | `<MotionScope tokens root>` | Its children drawn in another motion, on the engine's clock: `useTokens()` under it gives that motion's tokens (a `tokens.json`, or tokens already resolved), set as custom properties on a full-frame root (`root={false}` leaves it out). A film in several motions wraps each scene, or each part of a frame, in one |
@@ -58,7 +58,8 @@ check it scene by scene without running it:
58
58
  is cut there, and the line says by how much (`over`), for the engine to tell; lines it leaves no time are said where it ends, in none.
59
59
  Speech never makes the outline fail, since its length is the engine's; times the author wrote that cannot all hold still do. A voiced line in the outline has `voiced` and its `words`, in the film's seconds. `useLine(id)` gives a
60
60
  line's `{start, end, words}` in its scene's time, so a picture can move with what is said; `<Film captions="words">` draws a voiced line
61
- word by word, each full once it is said.
61
+ word by word, each full once it is said. Its words are set apart by a blank, but where either side is Chinese, Japanese, Korean or Thai,
62
+ which an engine gives one character (or syllable) a word: `热向前走` stays together.
62
63
  - **`<Sound src at dur trim loop volume fadeIn fadeOut duck>`**: a sound the film plays, the file `src` (a path the engine serves), from
63
64
  `at` seconds of its scene, or of the film when it is one of `<Film>`'s own; `dur` seconds of it, from `trim` seconds into the file (the
64
65
  whole file when left out); with `loop`, again and again to the end of its scene or of the film; at `volume` dB (0 is the file as it is),
package/index.js CHANGED
@@ -255,8 +255,9 @@ const timeLines = (lines, start, duration, voice = {}) => {
255
255
  * twice where it must be unique, and a sound with no file or a time that is no number of seconds, are errors.
256
256
  */
257
257
  export function filmOutline(children, {width = 1920, height = 1080, captions = false, voice = {}, speaker} = {}) {
258
- if (speaker !== undefined && !(typeof speaker === 'string' && /^[\w.-]+:\S+$/.test(speaker))) {
259
- throw new Error(`<Film voice="${speaker}">: a voice is its provider and its name, as voice="edge:en-US-AriaNeural"`);
258
+ // A provider's own name for a voice may hold blanks and brackets, as MiniMax's "Chinese (Mandarin)_News_Anchor" does.
259
+ if (speaker !== undefined && !(typeof speaker === 'string' && /^[\w.-]+:\S(?:.*\S)?$/.test(speaker))) {
260
+ throw new Error(`<Film voice="${speaker}">: a voice is its provider and its name, as voice="minimax:Chinese (Mandarin)_News_Anchor"`);
260
261
  }
261
262
  const box = (e) => ({id: named('Layer', e.props.id), x: e.props.x ?? 0, y: e.props.y ?? 0, w: e.props.w ?? width, h: e.props.h ?? height, z: e.props.z ?? 0});
262
263
  const unique = (what, items) => {
@@ -429,13 +430,21 @@ export const MotionScope = ({tokens, root = true, children}) => {
429
430
  /**
430
431
  * The motion's caption: one spoken line on the frame, drawn from the caption group of its tokens: font, size, weight, color, background, radius,
431
432
  * padding-y, padding-x, bottom, max-width, shadow (a text shadow), and a rule: rule-left (a bar down its left side) or rule-top (a short bar above it).
433
+ * Lengths are pixels, as numbers (36) or as '36px'. With neither a color nor a background it is drawn white on a dark ground.
434
+ * Without a max-width, its text is at most 1500 px wide and keeps 64 px from the frame's sides.
432
435
  * `opacity` fades it; it stays a function of what you pass. With `line`, the id of one of its <Film>'s lines, it draws that line's words while
433
436
  * it is said, fading in and out, and nothing at other times.
434
437
  */
438
+ // Scripts written without blanks between words: an engine gives their words one character (or syllable) each, and they
439
+ // are shown together again, with a blank only where neither side is one of them, as HyperFrames joins its words.
440
+ const UNSPACED = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\p{Script=Thai}]/u;
441
+ const blankBetween = (left, right) => (UNSPACED.test(left.at(-1) ?? '') || UNSPACED.test(right[0] ?? '') ? '' : ' ');
442
+
435
443
  export const Caption = ({line, words = false, children, opacity = 1, style}) => {
436
444
  // Both read before anything branches, so a caption calls the same hooks with a line or without.
437
445
  const said = useContext(Said);
438
446
  const c = useTokens().caption ?? {};
447
+ const frame = useVideoConfig().width;
439
448
  if (line !== undefined) {
440
449
  const spoken = said?.lines.find((l) => l.id === line);
441
450
  if (!spoken) throw new Error(`<Caption line="${line}">: ${said ? 'the film has no line by that id' : 'a caption of a line is drawn inside a <Film>'}`);
@@ -443,17 +452,24 @@ export const Caption = ({line, words = false, children, opacity = 1, style}) =>
443
452
  if (fade <= 0) return null;
444
453
  // Word by word, where the line is voiced: each word full once it is said, the rest waiting faint.
445
454
  const shown = words && spoken.words?.length
446
- ? spoken.words.map((w, i) => createElement('span', {key: i, style: {opacity: said.now >= w.start ? 1 : 0.35}}, `${i ? ' ' : ''}${w.text}`))
455
+ ? spoken.words.map((w, i) => createElement('span', {key: i, style: {opacity: said.now >= w.start ? 1 : 0.35}}, `${i ? blankBetween(spoken.words[i - 1].text, w.text) : ''}${w.text}`))
447
456
  : spoken.text;
448
457
  return createElement(Caption, {opacity: opacity * fade, style}, children ?? shown);
449
458
  }
450
- const py = c['padding-y'] ?? 10, pad = `${py}px ${c['padding-x'] ?? 26}px`;
459
+ // A length in pixels as resolved tokens give it (36), or as a film's own tokens may write it ('36px'); anything else is not one.
460
+ const px = (v, fallback) => (typeof v === 'number' ? v : typeof v === 'string' && /^\s*-?[\d.]+(px)?\s*$/.test(v) ? parseFloat(v) : fallback);
461
+ const py = px(c['padding-y'], 10), padX = px(c['padding-x'], 26), pad = `${py}px ${padX}px`;
462
+ // At most 1500 px of text, and never closer than 64 px to the frame's sides: a portrait frame is narrower than that.
463
+ const widest = Number.isFinite(frame) && frame > 0 ? Math.min(1500, frame - 128 - 2 * padX) : 1500;
464
+ // A caption given neither a colour nor a ground is drawn light on a dark ground, legible over any picture.
465
+ const plain = c.color === undefined && c.background === undefined;
451
466
  const rule = c['rule-left']
452
467
  ? createElement('div', {style: {position: 'absolute', left: 0, top: py * 0.6, bottom: py * 0.6, width: 4, borderRadius: 2, background: c['rule-left']}})
453
468
  : c['rule-top'] ? createElement('div', {style: {width: 56, height: 3, background: c['rule-top'], margin: '0 auto 8px'}}) : null;
454
- return createElement('div', {style: {position: 'absolute', left: 0, right: 0, bottom: c.bottom ?? 54, display: 'flex', justifyContent: 'center', opacity, pointerEvents: 'none'}},
469
+ return createElement('div', {style: {position: 'absolute', left: 0, right: 0, bottom: px(c.bottom, 54), display: 'flex', justifyContent: 'center', opacity, pointerEvents: 'none'}},
455
470
  createElement('div', {style: {
456
- position: 'relative', maxWidth: c['max-width'] ?? 1500, textAlign: 'center', fontFamily: c.font, fontSize: c.size ?? 34, fontWeight: c.weight ?? 400, lineHeight: 1.35,
457
- color: c.color, background: c.background ?? 'transparent', padding: pad, borderRadius: c.radius ?? 10, textShadow: c.shadow, ...style,
471
+ position: 'relative', maxWidth: px(c['max-width'], widest), textAlign: 'center', fontFamily: c.font, fontSize: px(c.size, 34), fontWeight: c.weight ?? 400, lineHeight: 1.35,
472
+ color: c.color ?? (plain ? '#ffffff' : undefined), background: c.background ?? (plain ? 'rgba(12, 14, 18, 0.72)' : 'transparent'), padding: pad,
473
+ borderRadius: px(c.radius, 10), textShadow: c.shadow, ...style,
458
474
  }}, rule, children));
459
475
  };
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@motionhub/runtime",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "description": "What a motion's React components draw with (time, frame, tokens, seeded randomness, video, caption), and a film of scenes and sounds, in any engine that provides it.",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
8
- "url": "git+https://github.com/motionhub-dev/motionhub.git",
8
+ "url": "git+https://github.com/motionhubdev/motionhub.git",
9
9
  "directory": "packages/runtime"
10
10
  },
11
11
  "homepage": "https://motionhub.dev",