-
Notifications
You must be signed in to change notification settings - Fork 38
/
precomputeValues.ts
38 lines (31 loc) · 1.44 KB
/
precomputeValues.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
import { ComputedValues, CapsizeOptions } from './types';
import { normaliseOptions } from './normaliseOptions';
import { round } from './round';
export function precomputeValues(options: CapsizeOptions): ComputedValues {
const { fontSize, lineHeight, fontMetrics } = normaliseOptions(options);
const absoluteDescent = Math.abs(fontMetrics.descent);
const capHeightScale = fontMetrics.capHeight / fontMetrics.unitsPerEm;
const descentScale = absoluteDescent / fontMetrics.unitsPerEm;
const ascentScale = fontMetrics.ascent / fontMetrics.unitsPerEm;
const lineGapScale = fontMetrics.lineGap / fontMetrics.unitsPerEm;
const contentArea =
fontMetrics.ascent + fontMetrics.lineGap + absoluteDescent;
const lineHeightScale = contentArea / fontMetrics.unitsPerEm;
const lineHeightNormal = lineHeightScale * fontSize;
const allowForLineHeight = (trim: number) => {
if (lineHeight) {
const specifiedLineHeightOffset = (lineHeightNormal - lineHeight) / 2;
return trim - specifiedLineHeightOffset / fontSize;
}
return trim;
};
const capHeightTrim =
allowForLineHeight(ascentScale - capHeightScale + lineGapScale / 2) * -1;
const baselineTrim = allowForLineHeight(descentScale + lineGapScale / 2) * -1;
return {
fontSize: `${round(fontSize)}px`,
lineHeight: lineHeight ? `${round(lineHeight)}px` : 'normal',
capHeightTrim: `${round(capHeightTrim)}em`,
baselineTrim: `${round(baselineTrim)}em`,
};
}