2021-08-06 00:17:05 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { getEmojiCount } from '../components/emoji/lib';
|
2021-10-06 17:37:53 +00:00
|
|
|
import { isEmojiOnlyText } from './isEmojiOnlyText';
|
2021-08-06 00:17:05 +00:00
|
|
|
|
|
|
|
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;
|
2021-10-06 17:37:53 +00:00
|
|
|
if (isEmojiOnlyText(text) && getEmojiCount(text) === 1) {
|
2021-08-06 00:17:05 +00:00
|
|
|
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;
|
|
|
|
}
|