@remotion/layout-utils 4.0.134 → 4.0.136
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.
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/layouts/fill-text-box.d.ts +1 -1
- package/dist/cjs/layouts/fill-text-box.js +2 -1
- package/dist/cjs/layouts/fit-text.d.ts +2 -1
- package/dist/cjs/layouts/fit-text.js +2 -1
- package/dist/cjs/layouts/measure-text.d.ts +3 -3
- package/dist/cjs/layouts/measure-text.js +45 -5
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.mjs +49 -7
- package/dist/esm/layouts/fill-text-box.d.ts +1 -1
- package/dist/esm/layouts/fit-text.d.ts +2 -1
- package/dist/esm/layouts/measure-text.d.ts +3 -3
- package/package.json +1 -1
package/dist/cjs/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export declare const fillTextBox: ({ maxBoxWidth, maxLines, }: {
|
|
|
3
3
|
maxBoxWidth: number;
|
|
4
4
|
maxLines: number;
|
|
5
5
|
}) => {
|
|
6
|
-
add: ({ text, fontFamily, fontWeight, fontSize, letterSpacing, fontVariantNumeric, }: Word) => {
|
|
6
|
+
add: ({ text, fontFamily, fontWeight, fontSize, letterSpacing, fontVariantNumeric, validateFontIsLoaded, }: Word) => {
|
|
7
7
|
exceedsBox: boolean;
|
|
8
8
|
newLine: boolean;
|
|
9
9
|
};
|
|
@@ -5,7 +5,7 @@ const measure_text_1 = require("./measure-text");
|
|
|
5
5
|
const fillTextBox = ({ maxBoxWidth, maxLines, }) => {
|
|
6
6
|
const lines = new Array(maxLines).fill(0).map(() => []);
|
|
7
7
|
return {
|
|
8
|
-
add: ({ text, fontFamily, fontWeight, fontSize, letterSpacing, fontVariantNumeric, }) => {
|
|
8
|
+
add: ({ text, fontFamily, fontWeight, fontSize, letterSpacing, fontVariantNumeric, validateFontIsLoaded, }) => {
|
|
9
9
|
const lastLineIndex = lines.reduceRight((acc, curr, index) => {
|
|
10
10
|
if (acc === -1 && curr.length > 0) {
|
|
11
11
|
return index;
|
|
@@ -23,6 +23,7 @@ const fillTextBox = ({ maxBoxWidth, maxLines, }) => {
|
|
|
23
23
|
fontSize,
|
|
24
24
|
letterSpacing,
|
|
25
25
|
fontVariantNumeric,
|
|
26
|
+
validateFontIsLoaded,
|
|
26
27
|
},
|
|
27
28
|
];
|
|
28
29
|
const widths = lineWithWord.map((w) => (0, measure_text_1.measureText)(w).width);
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
export declare const fitText: ({ text, withinWidth, fontFamily, fontVariantNumeric, fontWeight, letterSpacing, }: {
|
|
1
|
+
export declare const fitText: ({ text, withinWidth, fontFamily, fontVariantNumeric, fontWeight, letterSpacing, validateFontIsLoaded, }: {
|
|
2
2
|
text: string;
|
|
3
3
|
withinWidth: number;
|
|
4
4
|
fontFamily: string;
|
|
5
5
|
fontWeight?: string | number | undefined;
|
|
6
6
|
letterSpacing?: string | undefined;
|
|
7
7
|
fontVariantNumeric?: string | undefined;
|
|
8
|
+
validateFontIsLoaded?: boolean | undefined;
|
|
8
9
|
}) => {
|
|
9
10
|
fontSize: number;
|
|
10
11
|
};
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.fitText = void 0;
|
|
4
4
|
const measure_text_1 = require("../layouts/measure-text");
|
|
5
5
|
const sampleSize = 100;
|
|
6
|
-
const fitText = ({ text, withinWidth, fontFamily, fontVariantNumeric, fontWeight, letterSpacing, }) => {
|
|
6
|
+
const fitText = ({ text, withinWidth, fontFamily, fontVariantNumeric, fontWeight, letterSpacing, validateFontIsLoaded, }) => {
|
|
7
7
|
const estimate = (0, measure_text_1.measureText)({
|
|
8
8
|
text,
|
|
9
9
|
fontFamily,
|
|
@@ -11,6 +11,7 @@ const fitText = ({ text, withinWidth, fontFamily, fontVariantNumeric, fontWeight
|
|
|
11
11
|
fontWeight,
|
|
12
12
|
fontVariantNumeric,
|
|
13
13
|
letterSpacing,
|
|
14
|
+
validateFontIsLoaded,
|
|
14
15
|
});
|
|
15
16
|
return { fontSize: (withinWidth / estimate.width) * sampleSize };
|
|
16
17
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
type Dimensions = {
|
|
1
|
+
export type Dimensions = {
|
|
2
2
|
width: number;
|
|
3
3
|
height: number;
|
|
4
4
|
};
|
|
@@ -9,6 +9,6 @@ export type Word = {
|
|
|
9
9
|
fontWeight?: number | string;
|
|
10
10
|
letterSpacing?: string;
|
|
11
11
|
fontVariantNumeric?: string;
|
|
12
|
+
validateFontIsLoaded?: boolean;
|
|
12
13
|
};
|
|
13
|
-
export declare const measureText: ({ text, fontFamily, fontSize, fontWeight, letterSpacing, fontVariantNumeric, }: Word) => Dimensions;
|
|
14
|
-
export {};
|
|
14
|
+
export declare const measureText: ({ text, fontFamily, fontSize, fontWeight, letterSpacing, fontVariantNumeric, validateFontIsLoaded, }: Word) => Dimensions;
|
|
@@ -2,13 +2,14 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.measureText = void 0;
|
|
4
4
|
const wordCache = new Map();
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
return wordCache.get(key);
|
|
5
|
+
const takeMeasurement = ({ text, fontFamily, fontSize, fontWeight, letterSpacing, fontVariantNumeric, }) => {
|
|
6
|
+
if (typeof document === 'undefined') {
|
|
7
|
+
throw new Error('measureText() can only be called in a browser.');
|
|
9
8
|
}
|
|
10
9
|
const node = document.createElement('span');
|
|
11
|
-
|
|
10
|
+
if (fontFamily) {
|
|
11
|
+
node.style.fontFamily = fontFamily;
|
|
12
|
+
}
|
|
12
13
|
node.style.display = 'inline-block';
|
|
13
14
|
node.style.position = 'absolute';
|
|
14
15
|
node.style.top = `-10000px`;
|
|
@@ -26,8 +27,47 @@ const measureText = ({ text, fontFamily, fontSize, fontWeight, letterSpacing, fo
|
|
|
26
27
|
}
|
|
27
28
|
node.innerText = text;
|
|
28
29
|
document.body.appendChild(node);
|
|
30
|
+
const computedFontFamily = window.getComputedStyle(node).fontFamily;
|
|
29
31
|
const boundingBox = node.getBoundingClientRect();
|
|
30
32
|
document.body.removeChild(node);
|
|
33
|
+
return {
|
|
34
|
+
boundingBox,
|
|
35
|
+
computedFontFamily,
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
const measureText = ({ text, fontFamily, fontSize, fontWeight, letterSpacing, fontVariantNumeric, validateFontIsLoaded, }) => {
|
|
39
|
+
const key = `${text}-${fontFamily}-${fontWeight}-${fontSize}-${letterSpacing}`;
|
|
40
|
+
if (wordCache.has(key)) {
|
|
41
|
+
return wordCache.get(key);
|
|
42
|
+
}
|
|
43
|
+
const { boundingBox, computedFontFamily } = takeMeasurement({
|
|
44
|
+
fontFamily,
|
|
45
|
+
fontSize,
|
|
46
|
+
text,
|
|
47
|
+
fontVariantNumeric,
|
|
48
|
+
fontWeight,
|
|
49
|
+
letterSpacing,
|
|
50
|
+
});
|
|
51
|
+
if (validateFontIsLoaded) {
|
|
52
|
+
const { boundingBox: boundingBoxOfFallbackFont, computedFontFamily: computedFallback, } = takeMeasurement({
|
|
53
|
+
fontFamily: null,
|
|
54
|
+
fontSize,
|
|
55
|
+
text,
|
|
56
|
+
fontVariantNumeric,
|
|
57
|
+
fontWeight,
|
|
58
|
+
letterSpacing,
|
|
59
|
+
});
|
|
60
|
+
const sameAsFallbackFont = boundingBox.height === boundingBoxOfFallbackFont.height &&
|
|
61
|
+
boundingBox.width === boundingBoxOfFallbackFont.width;
|
|
62
|
+
if (sameAsFallbackFont && computedFallback !== computedFontFamily) {
|
|
63
|
+
const err = [
|
|
64
|
+
`Called measureText() with "fontFamily": ${JSON.stringify(fontFamily)} but it looks like the font is not loaded at the time of calling.`,
|
|
65
|
+
`A measurement with the fallback font ${computedFallback} was taken and had the same dimensions, indicating that the browser used the fallback font.`,
|
|
66
|
+
'See https://remotion.dev/docs/layout-utils/best-practices for best practices.',
|
|
67
|
+
];
|
|
68
|
+
throw new Error(err.join('\n'));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
31
71
|
const result = { height: boundingBox.height, width: boundingBox.width };
|
|
32
72
|
wordCache.set(key, result);
|
|
33
73
|
return result;
|
package/dist/esm/index.d.ts
CHANGED
package/dist/esm/index.mjs
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
const wordCache = new Map();
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
return wordCache.get(key);
|
|
2
|
+
const takeMeasurement = ({ text, fontFamily, fontSize, fontWeight, letterSpacing, fontVariantNumeric, }) => {
|
|
3
|
+
if (typeof document === 'undefined') {
|
|
4
|
+
throw new Error('measureText() can only be called in a browser.');
|
|
6
5
|
}
|
|
7
6
|
const node = document.createElement('span');
|
|
8
|
-
|
|
7
|
+
if (fontFamily) {
|
|
8
|
+
node.style.fontFamily = fontFamily;
|
|
9
|
+
}
|
|
9
10
|
node.style.display = 'inline-block';
|
|
10
11
|
node.style.position = 'absolute';
|
|
11
12
|
node.style.top = `-10000px`;
|
|
@@ -23,8 +24,47 @@ const measureText = ({ text, fontFamily, fontSize, fontWeight, letterSpacing, fo
|
|
|
23
24
|
}
|
|
24
25
|
node.innerText = text;
|
|
25
26
|
document.body.appendChild(node);
|
|
27
|
+
const computedFontFamily = window.getComputedStyle(node).fontFamily;
|
|
26
28
|
const boundingBox = node.getBoundingClientRect();
|
|
27
29
|
document.body.removeChild(node);
|
|
30
|
+
return {
|
|
31
|
+
boundingBox,
|
|
32
|
+
computedFontFamily,
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
const measureText = ({ text, fontFamily, fontSize, fontWeight, letterSpacing, fontVariantNumeric, validateFontIsLoaded, }) => {
|
|
36
|
+
const key = `${text}-${fontFamily}-${fontWeight}-${fontSize}-${letterSpacing}`;
|
|
37
|
+
if (wordCache.has(key)) {
|
|
38
|
+
return wordCache.get(key);
|
|
39
|
+
}
|
|
40
|
+
const { boundingBox, computedFontFamily } = takeMeasurement({
|
|
41
|
+
fontFamily,
|
|
42
|
+
fontSize,
|
|
43
|
+
text,
|
|
44
|
+
fontVariantNumeric,
|
|
45
|
+
fontWeight,
|
|
46
|
+
letterSpacing,
|
|
47
|
+
});
|
|
48
|
+
if (validateFontIsLoaded) {
|
|
49
|
+
const { boundingBox: boundingBoxOfFallbackFont, computedFontFamily: computedFallback, } = takeMeasurement({
|
|
50
|
+
fontFamily: null,
|
|
51
|
+
fontSize,
|
|
52
|
+
text,
|
|
53
|
+
fontVariantNumeric,
|
|
54
|
+
fontWeight,
|
|
55
|
+
letterSpacing,
|
|
56
|
+
});
|
|
57
|
+
const sameAsFallbackFont = boundingBox.height === boundingBoxOfFallbackFont.height &&
|
|
58
|
+
boundingBox.width === boundingBoxOfFallbackFont.width;
|
|
59
|
+
if (sameAsFallbackFont && computedFallback !== computedFontFamily) {
|
|
60
|
+
const err = [
|
|
61
|
+
`Called measureText() with "fontFamily": ${JSON.stringify(fontFamily)} but it looks like the font is not loaded at the time of calling.`,
|
|
62
|
+
`A measurement with the fallback font ${computedFallback} was taken and had the same dimensions, indicating that the browser used the fallback font.`,
|
|
63
|
+
'See https://remotion.dev/docs/layout-utils/best-practices for best practices.',
|
|
64
|
+
];
|
|
65
|
+
throw new Error(err.join('\n'));
|
|
66
|
+
}
|
|
67
|
+
}
|
|
28
68
|
const result = { height: boundingBox.height, width: boundingBox.width };
|
|
29
69
|
wordCache.set(key, result);
|
|
30
70
|
return result;
|
|
@@ -33,7 +73,7 @@ const measureText = ({ text, fontFamily, fontSize, fontWeight, letterSpacing, fo
|
|
|
33
73
|
const fillTextBox = ({ maxBoxWidth, maxLines, }) => {
|
|
34
74
|
const lines = new Array(maxLines).fill(0).map(() => []);
|
|
35
75
|
return {
|
|
36
|
-
add: ({ text, fontFamily, fontWeight, fontSize, letterSpacing, fontVariantNumeric, }) => {
|
|
76
|
+
add: ({ text, fontFamily, fontWeight, fontSize, letterSpacing, fontVariantNumeric, validateFontIsLoaded, }) => {
|
|
37
77
|
const lastLineIndex = lines.reduceRight((acc, curr, index) => {
|
|
38
78
|
if (acc === -1 && curr.length > 0) {
|
|
39
79
|
return index;
|
|
@@ -51,6 +91,7 @@ const fillTextBox = ({ maxBoxWidth, maxLines, }) => {
|
|
|
51
91
|
fontSize,
|
|
52
92
|
letterSpacing,
|
|
53
93
|
fontVariantNumeric,
|
|
94
|
+
validateFontIsLoaded,
|
|
54
95
|
},
|
|
55
96
|
];
|
|
56
97
|
const widths = lineWithWord.map((w) => measureText(w).width);
|
|
@@ -85,7 +126,7 @@ const fillTextBox = ({ maxBoxWidth, maxLines, }) => {
|
|
|
85
126
|
};
|
|
86
127
|
|
|
87
128
|
const sampleSize = 100;
|
|
88
|
-
const fitText = ({ text, withinWidth, fontFamily, fontVariantNumeric, fontWeight, letterSpacing, }) => {
|
|
129
|
+
const fitText = ({ text, withinWidth, fontFamily, fontVariantNumeric, fontWeight, letterSpacing, validateFontIsLoaded, }) => {
|
|
89
130
|
const estimate = measureText({
|
|
90
131
|
text,
|
|
91
132
|
fontFamily,
|
|
@@ -93,6 +134,7 @@ const fitText = ({ text, withinWidth, fontFamily, fontVariantNumeric, fontWeight
|
|
|
93
134
|
fontWeight,
|
|
94
135
|
fontVariantNumeric,
|
|
95
136
|
letterSpacing,
|
|
137
|
+
validateFontIsLoaded,
|
|
96
138
|
});
|
|
97
139
|
return { fontSize: (withinWidth / estimate.width) * sampleSize };
|
|
98
140
|
};
|
|
@@ -3,7 +3,7 @@ export declare const fillTextBox: ({ maxBoxWidth, maxLines, }: {
|
|
|
3
3
|
maxBoxWidth: number;
|
|
4
4
|
maxLines: number;
|
|
5
5
|
}) => {
|
|
6
|
-
add: ({ text, fontFamily, fontWeight, fontSize, letterSpacing, fontVariantNumeric, }: Word) => {
|
|
6
|
+
add: ({ text, fontFamily, fontWeight, fontSize, letterSpacing, fontVariantNumeric, validateFontIsLoaded, }: Word) => {
|
|
7
7
|
exceedsBox: boolean;
|
|
8
8
|
newLine: boolean;
|
|
9
9
|
};
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
export declare const fitText: ({ text, withinWidth, fontFamily, fontVariantNumeric, fontWeight, letterSpacing, }: {
|
|
1
|
+
export declare const fitText: ({ text, withinWidth, fontFamily, fontVariantNumeric, fontWeight, letterSpacing, validateFontIsLoaded, }: {
|
|
2
2
|
text: string;
|
|
3
3
|
withinWidth: number;
|
|
4
4
|
fontFamily: string;
|
|
5
5
|
fontWeight?: string | number | undefined;
|
|
6
6
|
letterSpacing?: string | undefined;
|
|
7
7
|
fontVariantNumeric?: string | undefined;
|
|
8
|
+
validateFontIsLoaded?: boolean | undefined;
|
|
8
9
|
}) => {
|
|
9
10
|
fontSize: number;
|
|
10
11
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
type Dimensions = {
|
|
1
|
+
export type Dimensions = {
|
|
2
2
|
width: number;
|
|
3
3
|
height: number;
|
|
4
4
|
};
|
|
@@ -9,6 +9,6 @@ export type Word = {
|
|
|
9
9
|
fontWeight?: number | string;
|
|
10
10
|
letterSpacing?: string;
|
|
11
11
|
fontVariantNumeric?: string;
|
|
12
|
+
validateFontIsLoaded?: boolean;
|
|
12
13
|
};
|
|
13
|
-
export declare const measureText: ({ text, fontFamily, fontSize, fontWeight, letterSpacing, fontVariantNumeric, }: Word) => Dimensions;
|
|
14
|
-
export {};
|
|
14
|
+
export declare const measureText: ({ text, fontFamily, fontSize, fontWeight, letterSpacing, fontVariantNumeric, validateFontIsLoaded, }: Word) => Dimensions;
|