Skip to content

🎥 Make videos programmatically with React

initialize `@remotion/layout-utils` package

willliuf965d5d43a7e
diff --git a/packages/cli/src/list-of-remotion-packages.ts b/packages/cli/src/list-of-remotion-packages.ts
index 65e63b39d9..58f14c7d2d 100644
--- a/packages/cli/src/list-of-remotion-packages.ts
+++ b/packages/cli/src/list-of-remotion-packages.ts
@@ -1,5 +1,6 @@
// Keep in sync with create-video
export const listOfRemotionPackages = [
+ '@remotion/layout-utils',
'@remotion/bundler',
'@remotion/cli',
'@remotion/cloudrun',
diff --git a/packages/create-video/src/list-of-remotion-packages.ts b/packages/create-video/src/list-of-remotion-packages.ts
index dff54a889a..fdd70bf63e 100644
--- a/packages/create-video/src/list-of-remotion-packages.ts
+++ b/packages/create-video/src/list-of-remotion-packages.ts
@@ -1,5 +1,6 @@
// Keep in sync with cli
export const listOfRemotionPackages = [
+ '@remotion/layout-utils',
'@remotion/bundler',
'@remotion/cli',
'@remotion/cloudrun',
diff --git a/packages/docs/components/TableOfContents/LayoutUtils.tsx b/packages/docs/components/TableOfContents/LayoutUtils.tsx
new file mode 100644
index 0000000000..64f0c6ce86
--- /dev/null
+++ b/packages/docs/components/TableOfContents/LayoutUtils.tsx
@@ -0,0 +1,16 @@
+import React from "react";
+import { Grid } from "./Grid";
+import { TOCItem } from "./TOCItem";
+
+export const TableOfContents: React.FC = () => {
+ return (
+ <div>
+ <Grid>
+ <TOCItem link="/docs/layout-utils/measure-text">
+ <strong>measureText()</strong>
+ <div>Create measure text</div>
+ </TOCItem>
+ </Grid>
+ </div>
+ );
+};
diff --git a/packages/docs/components/TableOfContents/api.tsx b/packages/docs/components/TableOfContents/api.tsx
index ab9c030a17..186c475626 100644
--- a/packages/docs/components/TableOfContents/api.tsx
+++ b/packages/docs/components/TableOfContents/api.tsx
@@ -88,6 +88,10 @@ export const TableOfContents: React.FC = () => {
<strong>@remotion/shapes</strong>
<div>Generate SVG shapes</div>
</TOCItem>
+ <TOCItem link="/docs/layout-utils">
+ <strong>@remotion/layout-utils</strong>
+ <div>Utility functions</div>
+ </TOCItem>
</Grid>
</div>
);
diff --git a/packages/docs/docs/layout-utils/index.md b/packages/docs/docs/layout-utils/index.md
new file mode 100644
index 0000000000..d8a4403d07
--- /dev/null
+++ b/packages/docs/docs/layout-utils/index.md
@@ -0,0 +1,54 @@
+---
+image: /generated/articles-docs-layout-utils-index.png
+title: "@remotion/layout-utils"
+---
+
+
+_Available from v4.0.50_
+
+Utility functions that create a remotion layout.
+
+import Tabs from '@theme/Tabs';
+import TabItem from '@theme/TabItem';
+import {TableOfContents} from '../../components/TableOfContents/LayoutUtils';
+
+<Tabs
+defaultValue="npm"
+values={[
+{ label: 'npm', value: 'npm', },
+{ label: 'pnpm', value: 'pnpm', },
+{ label: 'yarn', value: 'yarn', },
+]
+}>
+<TabItem value="npm">
+
+```bash
+npm i @remotion/layout-utils
+```
+
+ </TabItem>
+
+ <TabItem value="pnpm">
+
+```bash
+pnpm i @remotion/layout-utils
+```
+
+ </TabItem>
+
+ <TabItem value="yarn">
+
+```bash
+yarn add @remotion/layout-utils
+```
+
+ </TabItem>
+</Tabs>
+
+## Functions
+
+<TableOfContents/>
+
+## License
+
+MIT
diff --git a/packages/docs/docs/layout-utils/measure-text.md b/packages/docs/docs/layout-utils/measure-text.md
new file mode 100644
index 0000000000..0fd5c148a7
--- /dev/null
+++ b/packages/docs/docs/layout-utils/measure-text.md
@@ -0,0 +1,67 @@
+---
+image: /generated/articles-docs-layout-utils-measure-text.png
+title: measureText()
+crumb: "Make some"
+---
+
+_Part of the [`@remotion/layout-utils`](/docs/layout-utils) package._
+
+Creates measure text get the true width and height of the text.
+
+## API
+
+This function has a cache. If there are two duplicate arguments inputs, the second one will return the first result without repeating the calculation
+
+The function takes five arguments:
+
+### `text`
+
+_Required_ _string_
+
+Any string.
+
+### `fontFamily`
+
+_Required_ _string_
+
+Same as CSS style `font-family`
+
+### `fontSize`
+
+_Required_ _number_
+
+Same as CSS style `font-size`. Only _numbers_ allowed, unit `px`
+
+### `fontWeight`
+
+_string_
+
+Same as CSS style `font-weight`
+
+### `letterSpacing`
+
+_string_
+
+Same as CSS style `font-spacing`.
+
+## Return value
+
+An object with `height` and `width`
+
+## Example
+
+```tsx twoslash
+import { measureText } from "@remotion/layout-utils";
+
+const text = 'remotion';
+const fontFamily = 'Arial';
+const fontWeight = '500';
+const fontSize = 12;
+const letterSpacing = '1px';
+
+console.log(measureText({ text, fontFamily, fontWeight, fontSize, letterSpacing })); // { height: 14, width: 20 }
+```
+
+## See also
+
+- [`@remotion/layout-utils`](/docs/layout-utils)
diff --git a/packages/docs/package.json b/packages/docs/package.json
index 8e46af62f0..72e3b2873a 100644
--- a/packages/docs/package.json
+++ b/packages/docs/package.json
@@ -48,6 +48,7 @@
"@remotion/skia": "workspace:*",
"@remotion/tailwind": "workspace:*",
"@remotion/three": "workspace:*",
+ "@remotion/layout-utils": "workspace:*",
"@shopify/react-native-skia": "^0.1.191",
"@swc/core": "^1.3.27",
"@types/uuid": "^8.3.4",
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 71347962a4..7eca18828a 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -398,6 +398,15 @@ module.exports = {
},
items: ["zod-types/z-color", "zod-types/z-textarea"],
},
+ {
+ type: "category",
+ label: "@remotion/layout-utils",
+ link: {
+ type: "doc",
+ id: "layout-utils/index",
+ },
+ items: ["layout-utils/measure-text"],
+ },
],
mainSidebar: [
{
diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.ts
index ff61711901..698d13f1d2 100644
--- a/packages/docs/src/data/articles.ts
+++ b/packages/docs/src/data/articles.ts
@@ -2640,4 +2640,18 @@ export const articles = [
compId: "articles-docs-zod-types-z-textarea",
crumb: null,
},
+ {
+ id: "layout-utils/index",
+ title: "@remotion/layout-utils",
+ relativePath: "docs/layout-utils/index.md",
+ compId: "articles-docs-layout-utils-index",
+ crumb: null,
+ },
+ {
+ id: "layout-utils/measure-text",
+ title: "measureText()",
+ relativePath: "docs/layout-utils/measure-text.md",
+ compId: "articles-docs-layout-utils-measure-text",
+ crumb: "Make some",
+ },
];
diff --git a/packages/example/package.json b/packages/example/package.json
index 1465124ba7..7aa32b02a3 100644
--- a/packages/example/package.json
+++ b/packages/example/package.json
@@ -48,6 +48,7 @@
"@remotion/tailwind": "workspace:*",
"@remotion/three": "workspace:*",
"@remotion/zod-types": "workspace:*",
+ "@remotion/layout-utils": "workspace:*",
"@types/express": "^4.17.11",
"@types/node": "18.14.6",
"@types/opentype.js": "^1.3.4",
diff --git a/packages/example/src/Title/index.tsx b/packages/example/src/Title/index.tsx
index 091782f6f8..d315aa435c 100644
--- a/packages/example/src/Title/index.tsx
+++ b/packages/example/src/Title/index.tsx
@@ -1,5 +1,6 @@
import React from 'react';
import {spring, SpringConfig, useCurrentFrame, useVideoConfig} from 'remotion';
+import {measureText} from '@remotion/layout-utils';
export const Title: React.FC<{
line1: string;
@@ -28,6 +29,16 @@ export const Title: React.FC<{
to: 1,
fps,
});
+ const fontStyle = {
+ fontSize: 110,
+ fontWeight: 'bold',
+ fontFamily: 'SF Pro Text',
+ };
+ const {width, height} = measureText({
+ text: line1,
+ fontFamily: fontStyle.fontFamily,
+ fontSize: fontStyle.fontSize
+ })
return (
<div
style={{
@@ -40,16 +51,14 @@ export const Title: React.FC<{
}}
>
<div
- style={{
- fontSize: 110,
- fontWeight: 'bold',
- fontFamily: 'SF Pro Text',
- }}
+ style={fontStyle}
>
<span
style={{
display: 'inline-block',
transform: `scale(${firstWord})`,
+ width,
+ height,
}}
>
{line1}
diff --git a/packages/example/tsconfig.json b/packages/example/tsconfig.json
index 041aad5cec..6d21d98d17 100644
--- a/packages/example/tsconfig.json
+++ b/packages/example/tsconfig.json
@@ -29,6 +29,7 @@
{"path": "../shapes"},
{"path": "../skia"},
{"path": "../three"},
- {"path": "../zod-types"}
+ {"path": "../zod-types"},
+ {"path": "../layout-utils"},
]
}
diff --git a/packages/layout-utils/.eslintrc b/packages/layout-utils/.eslintrc
new file mode 100644
index 0000000000..d4cc0fb226
--- /dev/null
+++ b/packages/layout-utils/.eslintrc
@@ -0,0 +1,3 @@
+{
+ "extends": "@jonny"
+}
diff --git a/packages/layout-utils/.npmignore b/packages/layout-utils/.npmignore
new file mode 100644
index 0000000000..ee84819b37
--- /dev/null
+++ b/packages/layout-utils/.npmignore
@@ -0,0 +1,14 @@
+dist/test/**
+src
+.eslintrc
+tsconfig.tsbuildinfo
+*.tgz
+.turbo
+vitest.config.ts
+tsconfig.json
+.prettierrc.js
+.rollup
+.rollup.cache
+rollup.config.js
+tsconfig-esm.json
+dist/tsconfig-esm.tsbuildinfo
diff --git a/packages/layout-utils/.prettierrc.js b/packages/layout-utils/.prettierrc.js
new file mode 100644
index 0000000000..32eb24f3bf
--- /dev/null
+++ b/packages/layout-utils/.prettierrc.js
@@ -0,0 +1,14 @@
+module.exports = {
+ singleQuote: true,
+ bracketSpacing: false,
+ useTabs: true,
+ overrides: [
+ {
+ files: ['*.yml'],
+ options: {
+ singleQuote: false,
+ },
+ },
+ ],
+ plugins: [require.resolve('prettier-plugin-organize-imports')],
+};
diff --git a/packages/layout-utils/README.md b/packages/layout-utils/README.md
new file mode 100644
index 0000000000..a177385f2a
--- /dev/null
+++ b/packages/layout-utils/README.md
@@ -0,0 +1,3 @@
+This package a layout utils for Remotion.
+
+**Documentation: https://remotion.dev/layout-utils**
diff --git a/packages/layout-utils/package.json b/packages/layout-utils/package.json
new file mode 100644
index 0000000000..7b8e6486a1
--- /dev/null
+++ b/packages/layout-utils/package.json
@@ -0,0 +1,58 @@
+{
+ "name": "@remotion/layout-utils",
+ "version": "0.0.1",
+ "description": "Layout Utils for Remotion",
+ "main": "dist/cjs/index.js",
+ "types": "dist/cjs/index.d.ts",
+ "module": "dist/esm/index.mjs",
+ "sideEffects": false,
+ "scripts": {
+ "formatting": "prettier src --check",
+ "lint": "eslint src --ext ts,tsx",
+ "watch": "tsc -w",
+ "build": "rollup --config rollup.config.js && tsc -d",
+ "test": "vitest --run"
+ },
+ "exports": {
+ "./package.json": "./package.json",
+ ".": {
+ "types": "./dist/cjs/index.d.ts",
+ "module": "./dist/esm/index.mjs",
+ "import": "./dist/esm/index.mjs",
+ "require": "./dist/cjs/index.js"
+ }
+ },
+ "author": "Yehor Misiats (https://github.com/satelllte)",
+ "maintainers": [
+ "Jonny Burger <[email protected]>",
+ "Yehor Misiats (https://github.com/satelllte)"
+ ],
+ "contributors": [],
+ "license": "MIT",
+ "repository": {
+ "url": "https://github.com/remotion-dev/remotion"
+ },
+ "bugs": {
+ "url": "https://github.com/remotion-dev/remotion/issues"
+ },
+ "dependencies": {
+ "remotion": "workspace:*"
+ },
+ "devDependencies": {
+ "@jonny/eslint-config": "3.0.276",
+ "@rollup/plugin-typescript": "^8.2.0",
+ "@types/node": "18.14.6",
+ "eslint": "8.42.0",
+ "prettier": "3.0.2",
+ "prettier-plugin-organize-imports": "^3.2.2",
+ "rollup": "^2.70.1",
+ "vitest": "0.31.1"
+ },
+ "keywords": [
+ "remotion",
+ "layout-utils"
+ ],
+ "publishConfig": {
+ "access": "public"
+ }
+}
diff --git a/packages/layout-utils/rollup.config.js b/packages/layout-utils/rollup.config.js
new file mode 100644
index 0000000000..29e9e02725
--- /dev/null
+++ b/packages/layout-utils/rollup.config.js
@@ -0,0 +1,23 @@
+// rollup.config.js
+import typescript from '@rollup/plugin-typescript';
+
+export default [
+ {
+ input: 'src/index.ts',
+ output: [
+ {
+ file: 'dist/esm/index.mjs',
+ format: 'es',
+ sourcemap: false,
+ },
+ ],
+ external: ['react', 'remotion', 'react/jsx-runtime', 'simplex-noise'],
+ plugins: [
+ typescript({
+ tsconfig: 'tsconfig-esm.json',
+ sourceMap: false,
+ outputToFilesystem: true,
+ }),
+ ],
+ },
+];
diff --git a/packages/layout-utils/src/index.ts b/packages/layout-utils/src/index.ts
new file mode 100644
index 0000000000..d2304f59ab
--- /dev/null
+++ b/packages/layout-utils/src/index.ts
@@ -0,0 +1 @@
+export {measureText} from './layouts/measure-text';
diff --git a/packages/layout-utils/src/layouts/measure-text.ts b/packages/layout-utils/src/layouts/measure-text.ts
new file mode 100644
index 0000000000..80db44d793
--- /dev/null
+++ b/packages/layout-utils/src/layouts/measure-text.ts
@@ -0,0 +1,52 @@
+type Dimensions = {
+ width: number;
+ height: number;
+};
+
+const wordCache = new Map<string, Dimensions>();
+
+export const measureText = ({
+ text,
+ fontFamily,
+ fontSize,
+ fontWeight,
+ letterSpacing,
+}: {
+ text: string;
+ fontFamily: string;
+ fontSize: number;
+ fontWeight?: number | string;
+ letterSpacing?: string;
+}): Dimensions => {
+ const key = `${text}-${fontFamily}-${fontWeight}-${fontSize}-${letterSpacing}`;
+
+ if (wordCache.has(key)) {
+ return wordCache.get(key) as Dimensions;
+ }
+
+ const node = document.createElement("span");
+
+ node.style.fontFamily = fontFamily;
+ node.style.display = "inline-block";
+ node.style.position = "absolute";
+ node.style.top = `-10000px`;
+ node.style.whiteSpace = "pre";
+ if (fontWeight) {
+ node.style.fontWeight = fontWeight.toString();
+ }
+
+ node.style.fontSize = `${fontSize}px`;
+ if (letterSpacing) {
+ node.style.letterSpacing = letterSpacing;
+ }
+
+ node.innerText = text;
+
+ document.body.appendChild(node);
+ const boundingBox = node.getBoundingClientRect();
+ document.body.removeChild(node);
+
+ const result = { height: boundingBox.height, width: boundingBox.width };
+ wordCache.set(key, result);
+ return result;
+};
diff --git a/packages/layout-utils/src/test/measure-text.test.ts b/packages/layout-utils/src/test/measure-text.test.ts
new file mode 100644
index 0000000000..31c0e685e2
--- /dev/null
+++ b/packages/layout-utils/src/test/measure-text.test.ts
@@ -0,0 +1,19 @@
+import {expect, test} from 'vitest';
+import {measureText} from '../index';
+
+// Make Node.JS 14 pass
+if (typeof performance === 'undefined') {
+ test('Pass', () => {
+ expect(1).toBe(1);
+ });
+} else {
+ test('should return dimensions for given text', () => {
+ const text = 'Lorem ipsum';
+ const fontFamily = 'Arial';
+ const fontSize = 12;
+ const dimensions = measureText({ text, fontFamily, fontSize });
+
+ expect(dimensions).toHaveProperty('height');
+ expect(dimensions).toHaveProperty('width');
+ });
+}
diff --git a/packages/layout-utils/tsconfig-esm.json b/packages/layout-utils/tsconfig-esm.json
new file mode 100644
index 0000000000..38c57cd749
--- /dev/null
+++ b/packages/layout-utils/tsconfig-esm.json
@@ -0,0 +1,11 @@
+{
+ "extends": "../tsconfig-esm.settings.json",
+ "compilerOptions": {
+ "rootDir": "./src",
+ "outDir": "./dist/esm",
+ "skipLibCheck": true,
+ "jsx": "react-jsx",
+ "moduleResolution": "node"
+ },
+ "include": ["./src"]
+}
diff --git a/packages/layout-utils/tsconfig.json b/packages/layout-utils/tsconfig.json
new file mode 100644
index 0000000000..fe046539de
--- /dev/null
+++ b/packages/layout-utils/tsconfig.json
@@ -0,0 +1,14 @@
+{
+ "extends": "../tsconfig.settings.json",
+ "compilerOptions": {
+ "rootDir": "src",
+ "outDir": "dist/cjs",
+ "skipLibCheck": true
+ },
+ "include": ["src"],
+ "references": [
+ {
+ "path": "../core"
+ }
+ ]
+}