-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfontNormaliser.ts
103 lines (92 loc) · 3.29 KB
/
fontNormaliser.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import type {
FontScaleArgs,
FontScaleFunctionStr,
} from "@guardian/source-foundations/dist/types/typography/types";
// eslint-disable-next-line no-restricted-imports -- suppress our own lint rule as this is the one legit place to import the fonts
import * as sourceFoundations from "@guardian/source-foundations";
import { css } from "@emotion/react";
type FontOverride = (
originalFunc: FontScaleFunctionStr
) => (options?: FontScaleArgs | undefined) => string;
// different tools provide Agate Sans either with or without the Web suffix, so use fallbacks to try both.
const agateFontNameVariants = [
"Guardian Agate Sans",
"Guardian Agate Sans Web",
"GuardianAgateSans1Web",
];
export const agateFontFamily = `${agateFontNameVariants
.map((_) => `"${_}"`)
.join(",")}, Arial, sans-serif;`;
// source foundations will give us Guardian Text Sans, but we usually want Guardian Agate Sans, so as to fit in with
// the tools hosting pinboard.
const overrideToAgateSans: FontOverride =
(originalFunc: FontScaleFunctionStr) => (options?: FontScaleArgs) =>
`
${originalFunc(options)};
font-family: ${agateFontFamily};
`;
const defaultToPx: FontOverride =
(originalFunc: FontScaleFunctionStr) => (options?: FontScaleArgs) =>
originalFunc({ unit: "px", ...options });
type FontDefinition = { [fontSizeName: string]: FontScaleFunctionStr };
const applyFontOverride =
<T extends FontDefinition>(fontOverride: FontOverride) =>
(originalFont: T) =>
Object.entries(originalFont).reduce(
(acc, [fontSizeName, fontScaleFunc]) => ({
...acc,
[fontSizeName]: fontOverride(fontScaleFunc),
}),
{} as T
);
const agateSansFont = applyFontOverride(overrideToAgateSans);
const pixelSizedFont = applyFontOverride(defaultToPx);
export const agateSans = agateSansFont(
pixelSizedFont(sourceFoundations.textSans)
);
export const textSans = pixelSizedFont(sourceFoundations.textSans);
const isAgateLoaded = () => {
let foundAgate = false;
document.fonts.forEach((font) => {
if (agateFontNameVariants.includes(font.family)) {
foundAgate = true;
}
});
return foundAgate;
};
const agateFontFileBasePath =
"https://interactive.guim.co.uk/fonts/guss-webfonts/GuardianAgateSans1Web/GuardianAgateSans1Web-";
export const getAgateFontFaceIfApplicable = () =>
isAgateLoaded()
? null
: css`
@font-face {
font-family: "Guardian Agate Sans";
src: url("${agateFontFileBasePath}Regular.woff2") format("woff2");
font-weight: 400;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: "Guardian Agate Sans";
src: url("${agateFontFileBasePath}Bold.woff2") format("woff2");
font-weight: 700;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: "Guardian Agate Sans";
src: url("${agateFontFileBasePath}RegularItalic.woff2")
format("woff2");
font-weight: 400;
font-style: italic;
font-display: swap;
}
@font-face {
font-family: "Guardian Agate Sans";
src: url("${agateFontFileBasePath}BoldItalic.woff2") format("woff2");
font-weight: 700;
font-style: italic;
font-display: swap;
}
`;