-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
avatarTextSizeCalculator.ts
52 lines (44 loc) · 1.19 KB
/
avatarTextSizeCalculator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { getEmojiCount } from '../components/emoji/lib';
import { isEmojiOnlyText } from './isEmojiOnlyText';
type FontSizes = {
diameter: number;
singleEmoji: number;
smol: number;
text: number;
};
type RectSize = {
height: number;
width: number;
};
export function getFontSizes(bubbleSize: number): FontSizes {
return {
diameter: Math.ceil(bubbleSize * 0.75),
singleEmoji: Math.ceil(bubbleSize * 0.6),
smol: Math.ceil(bubbleSize * 0.05),
text: Math.ceil(bubbleSize * 0.45),
};
}
export function getFittedFontSize(
bubbleSize: number,
text: string,
measure: (candidateFontSize: number) => RectSize
): number {
const sizes = getFontSizes(bubbleSize);
let candidateFontSize = sizes.text;
if (isEmojiOnlyText(text) && getEmojiCount(text) === 1) {
candidateFontSize = sizes.singleEmoji;
}
for (
candidateFontSize;
candidateFontSize >= sizes.smol;
candidateFontSize -= 1
) {
const { height, width } = measure(candidateFontSize);
if (width < sizes.diameter && height < sizes.diameter) {
return candidateFontSize;
}
}
return candidateFontSize;
}