-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfonts.ts
176 lines (161 loc) · 4.7 KB
/
fonts.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* Utility functions to manage font properties to style mapping for both iOS and Android
* Fonts are managed differently on Android and iOS. Read the Font section of the
* README file included in this repository.
*/
import { Platform, TextStyle } from "react-native";
/**
* Choose the font name based on the platform
*/
const fonts = {
TitilliumSansPro: Platform.select({
android: "TitilliumSansPro",
web: "TitilliumSansPro",
ios: "Titillium Sans Pro",
default: "TitilliumSansPro"
}),
Titillio: Platform.select({
android: "Titillio",
web: "Titillio",
ios: "Titillio",
default: "Titillio"
}),
DMMono: Platform.select({
android: "DMMono",
web: "DMMono",
ios: "DM Mono",
default: "DMMono"
})
} as const;
export type IOFontFamily = keyof typeof fonts;
/*
* Font Sizes
*/
const fontSizes = [12, 14, 16, 20, 22, 26, 28, 32] as const;
const fontSizesLegacy = [17, 28, 31, 35] as const;
const allFontSizes = [...new Set([...fontSizes, ...fontSizesLegacy])];
export type IOFontSize = (typeof allFontSizes)[number];
/*
* Font Weights
*/
const weights = [
"Thin",
"Light",
"Regular",
"Medium",
"Semibold",
"Bold",
"Black"
] as const;
export type IOFontWeight = (typeof weights)[number];
const weightValues = ["200", "300", "400", "500", "600", "700", "900"] as const;
export type IOFontWeightNumeric = (typeof weightValues)[number];
/**
* Mapping between the nominal description of the weight (also the postfix used on Android) and the numeric value
* used on iOS
*/
export const fontWeights: Record<IOFontWeight, IOFontWeightNumeric> = {
Thin: "200",
Light: "300",
Regular: "400",
Medium: "500",
Semibold: "600",
Bold: "700",
Black: "900"
};
type FontStyleObject = {
fontSize: IOFontSize | number;
/* We also accept `string` because Android needs a composed
fontFamily name, like `TitilliumSansPro-Regular` */
fontFamily: string | IOFontFamily;
fontWeight?: IOFontWeightNumeric;
lineHeight?: TextStyle["lineHeight"];
fontStyle?: TextStyle["fontStyle"];
boldEnabled?: boolean;
};
/* Function that, given a certain weight, returns the next
available `FontWeight` value.
For example, if I set it to `Regular`, the function
should return `Medium`, and so on. If I set it to the last `FontWeight`
value, the function will return the same value.
*/
const getBolderFontWeight = (weight: IOFontWeight): IOFontWeight => {
const currentWeight = weights.indexOf(weight);
return currentWeight === weights.length - 1
? weight
: weights[currentWeight + 1];
};
/**
* Get the correct `fontFamily` name on both Android and iOS.
* @param font
* @param weight
* @param isItalic
*/
export const makeFontFamilyName = (
font: IOFontFamily,
weight: IOFontWeight = defaultWeight,
fontStyle: TextStyle["fontStyle"] = "normal"
): string =>
Platform.select({
web: fonts[font],
android: `${fonts[font]}-${weight || "Regular"}${
fontStyle === "italic" ? "Italic" : ""
}`,
ios: fonts[font],
default: fonts[font]
});
/**
* Default `IOText` typography style
*/
const defaultFont: IOFontFamily = "TitilliumSansPro";
const defaultWeight: IOFontWeight = "Regular";
const defaultFontSize: IOFontSize = 16;
export const IOMaxFontSizeMultiplier = 1.5;
/**
* Return a {@link FontStyleObject} with the fields filled based on the platform (iOS or Android).
* @param size
* @param font
* @param weight
* @param fontStyle
*/
export const makeFontStyleObject = (
size: number = defaultFontSize,
font: IOFontFamily = defaultFont,
lineHeight: TextStyle["lineHeight"],
weight: IOFontWeight = defaultWeight,
fontStyle: TextStyle["fontStyle"] = "normal",
boldEnabled: boolean = false
): FontStyleObject => {
/* If bold text is currently enabled, we select the next
available `IOFontWeight` value */
const dynamicWeight = boldEnabled ? getBolderFontWeight(weight) : weight;
return Platform.select({
web: {
fontSize: size,
fontFamily: makeFontFamilyName(font, dynamicWeight, fontStyle),
fontWeight: fontWeights[dynamicWeight],
lineHeight,
fontStyle
},
// On Android other type attributes, like `fontWeight` or `fontStyle`
// are directly managed through the `fontFamily` name, so we dont' need to
// include them in the object.
android: {
fontSize: size,
fontFamily: makeFontFamilyName(font, dynamicWeight, fontStyle),
lineHeight,
includeFontPadding: false
},
ios: {
fontSize: size,
fontFamily: makeFontFamilyName(font, dynamicWeight, fontStyle),
fontWeight: fontWeights[dynamicWeight],
lineHeight,
fontStyle
},
default: {
fontSize: size,
fontFamily: makeFontFamilyName(font, dynamicWeight, fontStyle)
}
});
};