Spaces:
Sleeping
Sleeping
File size: 6,729 Bytes
391a73c | 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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | <p align="center">
<img src="https://user-images.githubusercontent.com/5600341/27505816-c8bc37aa-587f-11e7-9a86-08a2d081a8b9.png" height="280px">
</p>
# @react-pdf/font
Font registration, loading, and resolution library for react-pdf. Handles TTF, WOFF, and WOFF2 fonts from various sources including local files, remote URLs, and base64 data URIs. Includes built-in support for PDF standard fonts.
## Installation
```bash
yarn add @react-pdf/font
```
## Usage
```js
import FontStore from '@react-pdf/font';
const fontStore = new FontStore();
// Register a custom font
fontStore.register({
family: 'Roboto',
src: 'https://example.com/fonts/Roboto-Regular.ttf',
});
// Load the font
await fontStore.load({
fontFamily: 'Roboto',
fontStyle: 'normal',
fontWeight: 400,
});
```
## Font Sources
The library supports multiple font source types:
### Remote URL
```js
fontStore.register({
family: 'Open Sans',
src: 'https://example.com/fonts/OpenSans-Regular.ttf',
});
```
With custom request options:
```js
fontStore.register({
family: 'Open Sans',
src: 'https://example.com/fonts/OpenSans-Regular.ttf',
method: 'GET',
headers: {
Authorization: 'Bearer token',
},
body: null,
});
```
### Local File (Node.js)
```js
fontStore.register({
family: 'Custom Font',
src: '/path/to/font.ttf',
});
```
> **Note:** Local file resolution is only available in Node.js environments.
### Base64 Data URI
```js
fontStore.register({
family: 'Embedded Font',
src: 'data:font/ttf;base64,AAEAAAALAIAAAwAwT1MvMg...',
});
```
## Registering Font Families
### Single Font
```js
fontStore.register({
family: 'Roboto',
src: 'https://example.com/fonts/Roboto-Regular.ttf',
fontWeight: 400,
fontStyle: 'normal',
});
```
### Multiple Weights and Styles (Bulk Registration)
```js
fontStore.register({
family: 'Roboto',
fonts: [
{ src: 'https://example.com/fonts/Roboto-Regular.ttf', fontWeight: 400 },
{ src: 'https://example.com/fonts/Roboto-Bold.ttf', fontWeight: 700 },
{
src: 'https://example.com/fonts/Roboto-Italic.ttf',
fontWeight: 400,
fontStyle: 'italic',
},
{
src: 'https://example.com/fonts/Roboto-BoldItalic.ttf',
fontWeight: 700,
fontStyle: 'italic',
},
],
});
```
## Standard Fonts
The following PDF standard fonts are pre-registered and available without any additional setup:
- **Helvetica** (with Bold, Oblique, and BoldOblique variants)
- **Courier** (with Bold, Oblique, and BoldOblique variants)
- **Times-Roman** (with Bold, Italic, and BoldItalic variants)
Helvetica variants are pre-loaded by default, so no explicit `load()` call is needed for them.
```js
// Standard fonts are ready to use immediately
const font = fontStore.getFont({
fontFamily: 'Helvetica',
fontWeight: 700,
fontStyle: 'normal',
});
```
## Font Weight Resolution
The library implements CSS font-weight resolution rules. You can specify font weights as numbers or keywords:
| Keyword | Numeric Value |
| -------------------------- | ------------- |
| `thin`, `hairline` | 100 |
| `ultralight`, `extralight` | 200 |
| `light` | 300 |
| `normal` | 400 |
| `medium` | 500 |
| `semibold`, `demibold` | 600 |
| `bold` | 700 |
| `ultrabold`, `extrabold` | 800 |
| `heavy`, `black` | 900 |
When an exact weight match isn't available, the library uses CSS fallback rules to find the closest available weight.
## Emoji Support
Register an emoji source to enable emoji rendering:
```js
// Using a URL pattern (must end with trailing slash)
fontStore.registerEmojiSource({
url: 'https://cdnjs.cloudflare.com/ajax/libs/twemoji/14.0.2/72x72/',
format: 'png',
});
// Using a custom builder function
fontStore.registerEmojiSource({
builder: (code) => `https://example.com/emojis/${code}.png`,
});
```
The `code` parameter passed to the builder is a hyphen-separated string of hex code points (e.g., `1f44d` for 👍 or `1f44d-1f3ff` for 👍🏿).
Set `withVariationSelectors: true` if your emoji source requires variation selectors in the code points.
## Hyphenation
Register a hyphenation callback for text wrapping:
```js
import hyphenationCallback from 'hyphen/en';
fontStore.registerHyphenationCallback(hyphenationCallback);
```
## API Reference
### FontStore
#### `register(data: SingleLoad | BulkLoad)`
Register a font or font family.
#### `load(descriptor: FontDescriptor): Promise<void>`
Load a specific font variant.
#### `getFont(descriptor: FontDescriptor): FontSource`
Get a font source matching the descriptor.
#### `registerEmojiSource(source: EmojiSource)`
Register an emoji image source.
#### `registerHyphenationCallback(callback: HyphenationCallback)`
Register a hyphenation callback function.
#### `reset()`
Reset all loaded font data (keeps registrations).
#### `clear()`
Clear all font registrations, emoji source, and hyphenation callback.
#### `getRegisteredFontFamilies(): string[]`
Get list of registered font family names.
#### `getRegisteredFonts(): Record<string, FontFamily>`
Get all registered font families with their sources.
#### `getEmojiSource(): EmojiSource | null`
Get the registered emoji source, if any.
#### `getHyphenationCallback(): HyphenationCallback | null`
Get the registered hyphenation callback, if any.
## Types
### FontDescriptor
```ts
type FontDescriptor = {
fontFamily: string;
fontStyle?: 'normal' | 'italic' | 'oblique';
fontWeight?: FontWeight;
};
```
### FontWeight
```ts
type FontWeight =
| number
| 'thin'
| 'hairline'
| 'ultralight'
| 'extralight'
| 'light'
| 'normal'
| 'medium'
| 'semibold'
| 'demibold'
| 'bold'
| 'ultrabold'
| 'extrabold'
| 'heavy'
| 'black';
```
### SingleLoad
```ts
type SingleLoad = {
family: string;
src: string;
fontStyle?: 'normal' | 'italic' | 'oblique';
fontWeight?: FontWeight;
postscriptName?: string;
method?: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
headers?: Record<string, string>;
body?: any;
};
```
### BulkLoad
```ts
type BulkLoad = {
family: string;
fonts: FontSource[];
};
```
### EmojiSource
```ts
type EmojiSource =
| { url: string; format?: string; withVariationSelectors?: boolean }
| { builder: (code: string) => string; withVariationSelectors?: boolean };
```
### HyphenationCallback
```ts
type HyphenationCallback = (word: string) => string[];
```
## Supported Font Formats
- **TTF** - TrueType Font
- **WOFF** - Web Open Font Format
- **WOFF2** - Web Open Font Format 2.0
## License
MIT
|