remotion icon indicating copy to clipboard operation
remotion copied to clipboard

`@remotion/google-fonts`: sliced CJK font lays out differently per render tab because slices are added to `document.fonts` in download order

Open psyche314 opened this issue 1 month ago • 0 comments

Bug Report 🐛

With a sliced CJK font from @remotion/google-fonts (here Noto Sans SC), the same static text can be laid out with a different width in different render tabs. In the rendered video a static subtitle jumps sideways from frame to frame.

Reproduction

import React from 'react';
import {AbsoluteFill, Composition} from 'remotion';
import {loadFont} from '@remotion/google-fonts/NotoSansSC';

const {fontFamily} = loadFont('normal', {weights: ['500'], ignoreTooManyRequestsWarning: true});

// The same static line on every frame.
const Repro: React.FC = () => (
  <AbsoluteFill style={{backgroundColor: 'black', justifyContent: 'center', alignItems: 'center'}}>
    <span style={{fontFamily, fontSize: 46, fontWeight: 500, color: 'white'}}>那句“你圈出的地方想表达什么”,就是这么问出来的。</span>
  </AbsoluteFill>
);

export const Root: React.FC = () => <Composition id="Repro" component={Repro} durationInFrames={64} fps={30} width={1920} height={1080} />;

To reproduce, run npx remotion render <entry> Repro out.mp4 --concurrency=8, then measure the width of the white text on each frame.

Result: the text measures 1096 px on 40 of the 64 frames and 1118 px on the other 24, in a pattern that repeats every 8 frames. Each render tab is consistent with itself, but the tabs disagree with each other.

The difference is about half an em, and it sits at the ”, pair. It looks like Chrome's CJK punctuation trimming (text-spacing-trim) is applied in some tabs and not in others. In a longer video, the same thing happened at ”。.

Cause

loadFonts() in packages/google-fonts/src/base.ts calls document.fonts.add(fontFace) as each slice finishes loading. The order of the FontFaceSet is therefore the network completion order, which differs from tab to tab. Chrome's handling of a family made of many unicode-range slices depends on that order.

Verification

Both checks were made on the repro above:

  • Fixed order: after waitUntilDone(), delete every FontFace and re-add them in a fixed (sorted) order. All 64 frames are then identical (1118 px).
  • Random order: do the same but shuffle the order. The jitter comes back.

Workaround

const {fontFamily, waitUntilDone} = loadFont('normal', {weights: ['500']});

const handle = delayRender('fixed font order');
waitUntilDone().then(() => {
  const key = (f: FontFace) => `${f.family}|${f.weight}|${f.style}|${f.unicodeRange}`;
  const faces = [...document.fonts].sort((a, b) => (key(a) < key(b) ? -1 : key(a) > key(b) ? 1 : 0));
  for (const f of faces) if (document.fonts.delete(f)) document.fonts.add(f);
  continueRender(handle);
});

Suggested fix

Add the FontFaces of a loadFont() call to document.fonts in a deterministic order, for example in the order of the requested subsets, once they have loaded. Adding them in completion order is what makes the tabs differ.

Environment

  • remotion and @remotion/google-fonts 4.0.531
  • Chrome Headless Shell 149.0.7790.0, downloaded by Remotion
  • Windows 11

psyche314 avatar Oct 20 '26 11:10 psyche314