-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_fonts_manager.js
84 lines (71 loc) · 2.12 KB
/
google_fonts_manager.js
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
let loadedFonts = [];
let fontList = {};
class FontNotFound extends Error {}
class VariantNotFound extends FontNotFound {}
class InvalidFamilyName extends VariantNotFound {}
/*
* @param fonts {string[] | string} - Font or a list of fonts that your want to be added
*/
async function loadFont({ name, variant = "regular" }) {
name = name.toLowerCase();
variant = variant.toLowerCase();
// fonts = [...fonts]; // Convert it into a list
if (Object.isEmpty(fontList)) await loadFontList();
// Dealing with the font name
if (!(name in fontList)) throw new FontNotFound(`${name} is not a registered Google font!`);
requestedFont = fontList[name];
// Dealing with the fontVariable
match = variant.match(/[134579]00|thin|extra-?light|light|regular|medium|semi[\s-]?bold|extra[\s-]?bold|bold|black/i);
matchMap = {
100: "100",
200: "200",
300: "300",
400: "regular",
500: "500",
600: "600",
700: "700",
800: "800",
900: "900",
thin: "100",
extralight: "200",
light: "300",
regular: "regular",
medium: "500",
semibold: "600",
bold: "700",
extrabold: "800",
black: "900",
};
finalVariant = matchMap[String(match).replace(/\s|-/, "")] + (variant.includes("italic") ? "italic" : "");
if (!requestedFont.variants.includes(finalVariant)) throw new VariantNotFound(`The variant "${variant}"(${finalVariant}) is not a valid variant`);
// Dealing with loading the font in
// loadedFonts.concat(font);
}
function unloadFonts(fonts) {
fonts = [...fonts];
}
async function loadFontList() {
return await fetch("https://www.googleapis.com/webfonts/v1/webfonts?key=AIzaSyDDiO8nLVRMDaXwrJp61Cdcar5gFmmiR1Q")
.then((resp) => resp.json())
.then((list) => {
list.items.forEach((item) => {
fontList[item.family.toLowerCase()] = item;
});
});
}
loadFont({
name: "Abhaya Libre",
variant: "bold",
});
// https://fonts.googleapis.com/css2?family=ABeeZee&display=swap
/*
Link to analyze
https://fonts.googleapis.com/css2?family=ABeeZee&family=Abhaya+Libre:wght@800&family=Poppins:ital,wght@1,100;1,400&display=swap
- ABeeZee
- Regular
- Abhaya Libre
- Extra Bold
- Poppins
- Thin 100 Italic
- Regular 400 Italic
*/