@@ -11,7 +11,7 @@ Please note that the y origin of the Text is the bottom of the text, not the top
11
11
| Name | Type | Description |
12
12
| :------------| :-----------| :----------------------------------------------------------------|
13
13
| text | ` string ` | Text to draw |
14
- | font | ` SkFont ` | Font to use (optional, see [ font management ] ( /docs/text/fonts ) ) |
14
+ | font | ` SkFont ` | Font to use (optional) |
15
15
| x | ` number ` | Left position of the text (default is 0) |
16
16
| y | ` number ` | Bottom position the text (default is 0, the ) |
17
17
@@ -40,21 +40,130 @@ export const HelloWorld = () => {
40
40
41
41
<img src={require("/static/img/text/hello-world.png").default} width="256" height="256" />
42
42
43
- ### Emojis
43
+ ## Fonts
44
+
45
+ Once the fonts are loaded, we provide a ` matchFont ` function that given a font style will return a font object that you can use directly.
44
46
45
47
``` tsx twoslash
46
- import {Canvas , Text , useFont , Fill } from " @shopify/react-native-skia" ;
48
+ import {useFonts , Text , matchFont } from " @shopify/react-native-skia" ;
49
+
50
+ const Demo = () => {
51
+ const fontMgr = useFonts ({
52
+ Roboto: [
53
+ require (" ./Roboto-Medium.ttf" ),
54
+ require (" ./Roboto-Regular.ttf" ),
55
+ require (" ./Roboto-Bold.ttf" ),
56
+ ]
57
+ });
58
+ if (! fontMgr ) {
59
+ return null ;
60
+ }
61
+ const fontStyle = {
62
+ fontFamily: " Roboto" ,
63
+ fontWeight: " bold" ,
64
+ fontSize: 16
65
+ } as const ;
66
+ const font = matchFont (fontStyle , fontMgr );
67
+ return (
68
+ <Text text = " Hello World" y = { 32 } x = { 32 } font = { font } />
69
+ );
70
+ };
71
+ ```
72
+
73
+ ## System Fonts
74
+
75
+ System fonts are available via ` Skia.FontMgr.System() ` .
76
+ You can list system fonts via ` listFontFamilies ` function returns the list of available system font families.
77
+ By default the function will list system fonts but you can pass an optional ` fontMgr ` object as parameter.
78
+
79
+ ``` jsx twoslash
80
+ import {listFontFamilies } from " @shopify/react-native-skia" ;
81
+
82
+ console .log (listFontFamilies ());
83
+ ```
84
+
85
+ Output example on Android:
86
+ ```
87
+ ["sans-serif", "arial", "helvetica", "tahoma", "verdana", ...]
88
+ ```
89
+
90
+ or on iOS:
91
+ ```
92
+ ["Academy Engraved LET", "Al Nile", "American Typewriter", "Apple Color Emoji", ...]
93
+ ```
94
+
95
+ By default matchFont, will match fonts from the system font manager:
96
+
97
+ ``` jsx twoslash
98
+ import {Platform } from " react-native" ;
99
+ import {Canvas , Text , matchFont , Fill , Skia } from " @shopify/react-native-skia" ;
100
+
101
+ const fontFamily = Platform .select ({ ios: " Helvetica" , default: " serif" });
102
+ const fontStyle = {
103
+ fontFamily,
104
+ fontSize: 14 ,
105
+ fontStyle: " italic" ,
106
+ fontWeight: " bold" ,
107
+ };
108
+ const font = matchFont (fontStyle);
47
109
48
110
export const HelloWorld = () => {
49
- const fontSize = 32 ;
50
- const font = useFont (require (" ./NotoColorEmoji.ttf" ), fontSize );
51
111
return (
52
112
< Canvas style= {{ flex: 1 }}>
53
113
< Fill color= " white" / >
54
- <Text text = " 🙋🌎" font = { font } y = { fontSize } x = { 0 } />
114
+ < Text
115
+ x= {0 }
116
+ y= {fontStyle .fontSize }
117
+ text= " Hello World"
118
+ font= {font}
119
+ / >
55
120
< / Canvas>
56
121
);
57
122
};
58
123
```
59
124
60
- <img src={require("/static/img/text/text-emoji.png").default} width="256" height="256" />
125
+ The ` fontStyle ` object can have the following list of optional attributes:
126
+
127
+ - ` fontFamily ` : The name of the font family.
128
+ - ` fontSize ` : The size of the font.
129
+ - ` fontStyle ` : The slant of the font. Can be ` normal ` , ` italic ` , or ` oblique ` .
130
+ - ` fontWeight ` : The weight of the font. Can be ` normal ` , ` bold ` , or any of ` 100 ` , ` 200 ` , ` 300 ` , ` 400 ` , ` 500 ` , ` 600 ` , ` 700 ` , ` 800 ` , ` 900 ` .
131
+
132
+ By default, ` matchFont ` uses the system font manager to match the font style. However, if you want to use your custom font manager, you can pass it as the second parameter to the ` matchFont ` function:
133
+
134
+ ``` jsx twoslash
135
+ import {matchFont , useFonts } from " @shopify/react-native-skia" ;
136
+
137
+ const fontMgr = useFonts ({
138
+ Roboto: [
139
+ require (" ../../Tests/assets/Roboto-Medium.ttf" ),
140
+ require (" ../../Tests/assets/Roboto-Bold.ttf" ),
141
+ ]
142
+ });
143
+
144
+ const font = matchFont (fontStyle, fontMgr);
145
+ ```
146
+
147
+ ## Low-level API
148
+
149
+ The basic usage of the system font manager is as follows.
150
+ These are the APIs used behind the scene by the ` matchFont ` function.
151
+
152
+ ``` tsx twoslash
153
+ import {Platform } from " react-native" ;
154
+ import {Skia , FontStyle } from " @shopify/react-native-skia" ;
155
+
156
+ const familyName = Platform .select ({ ios: " Helvetica" , default: " serif" });
157
+ const fontSize = 32 ;
158
+ // Get the system font manager
159
+ const fontMgr = Skia .FontMgr .System ();
160
+ // The custom font manager is available via Skia.TypefaceFontProvider.Make()
161
+ const customFontMgr = Skia .TypefaceFontProvider .Make ();
162
+ // typeface needs to be loaded via Skia.Data and instanciated via
163
+ // Skia.Typeface.MakeFreeTypeFaceFromData()
164
+ // customFontMgr.registerTypeface(customTypeFace, "Roboto");
165
+
166
+ // Matching a font
167
+ const typeface = fontMgr .matchFamilyStyle (familyName , FontStyle .Bold );
168
+ const font = Skia .Font (typeface , fontSize );
169
+ ```
0 commit comments