You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This fork is maintained by pafin Inc. because the original repository is no longer maintained and to enable Asian font subsetting. Special thanks to [@joewestcott](https://github.com/joewestcott) for his PR that provided the foundation for this feature.
40
46
41
47
# fontkit
42
48
43
49
Fontkit is an advanced font engine for Node and the browser, used by [PDFKit](https://github.com/devongovett/pdfkit) and [`pdf-lib`](https://github.com/Hopding/pdf-lib). It supports many font formats, advanced glyph substitution and layout features, glyph path extraction, color emoji glyphs, font subsetting, and more.
44
50
45
51
## Features
46
52
47
-
* Suports TrueType (.ttf), OpenType (.otf), WOFF, WOFF2, TrueType Collection (.ttc), and Datafork TrueType (.dfont) font files
48
-
* Supports mapping characters to glyphs, including support for ligatures and other advanced substitutions (see below)
49
-
* Supports reading glyph metrics and laying out glyphs, including support for kerning and other advanced layout features (see below)
50
-
* Advanced OpenType features including glyph substitution (GSUB) and positioning (GPOS)
51
-
* Apple Advanced Typography (AAT) glyph substitution features (morx table)
52
-
* Support for getting glyph vector paths and converting them to SVG paths, or rendering them to a graphics context
53
-
* Supports TrueType (glyf) and PostScript (CFF) outlines
54
-
* Support for color glyphs (e.g. emoji), including Apple’s SBIX table, and Microsoft’s COLR table
55
-
* Support for AAT variation glyphs, allowing for nearly infinite design control over weight, width, and other axes.
56
-
* Font subsetting support - create a new font including only the specified glyphs
53
+
- Suports TrueType (.ttf), OpenType (.otf), WOFF, WOFF2, TrueType Collection (.ttc), and Datafork TrueType (.dfont) font files
54
+
- Supports mapping characters to glyphs, including support for ligatures and other advanced substitutions (see below)
55
+
- Supports reading glyph metrics and laying out glyphs, including support for kerning and other advanced layout features (see below)
56
+
- Advanced OpenType features including glyph substitution (GSUB) and positioning (GPOS)
57
+
- Apple Advanced Typography (AAT) glyph substitution features (morx table)
58
+
- Support for getting glyph vector paths and converting them to SVG paths, or rendering them to a graphics context
59
+
- Supports TrueType (glyf) and PostScript (CFF) outlines
60
+
- Support for color glyphs (e.g. emoji), including Apple’s SBIX table, and Microsoft’s COLR table
61
+
- Support for AAT variation glyphs, allowing for nearly infinite design control over weight, width, and other axes.
62
+
- Font subsetting support - create a new font including only the specified glyphs
57
63
58
64
## Example
59
65
60
66
```js
61
-
importfontkitfrom'@pdf-lib/fontkit';
62
-
importfsfrom'fs';
67
+
importfontkitfrom"@pdf-lib/fontkit";
68
+
importfsfrom"fs";
63
69
64
70
// open a font synchronously
65
-
constfontData=fs.readFileSync('font.ttf');
71
+
constfontData=fs.readFileSync("font.ttf");
66
72
constfont=fontkit.create(fontData);
67
73
68
74
// layout a string, using default shaping features.
69
75
// returns a GlyphRun, describing glyphs and positions.
This assumes you're using [npm](https://www.npmjs.com/) or [yarn](https://yarnpkg.com/lang/en/) as your package manager.
96
105
97
106
### UMD Module
107
+
98
108
You can also download `@pdf-lib/fontkit` as a UMD module from [unpkg](https://unpkg.com/#/). The UMD builds have been compiled to ES5, so they should work [in any modern browser](https://caniuse.com/#feat=es5). UMD builds are useful if you aren't using a package manager or module bundler. For example, you can use them directly in the `<script>` tag of an HTML page.
When using a UMD build, you will have access to a global `window.fontkit` variable. This variable contains the object exported by `@pdf-lib/fontkit`. For example:
106
116
107
117
```javascript
108
118
// NPM module
109
-
importfontkitfrom'@pdf-lib/fontkit';
119
+
importfontkitfrom"@pdf-lib/fontkit";
110
120
111
121
// UMD module
112
122
var fontkit =window.fontkit;
@@ -126,33 +136,33 @@ There are several different types of font objects that are returned by fontkit d
126
136
127
137
The following properties are strings (or null if the font does not contain strings for them) describing the font, as specified by the font creator.
128
138
129
-
*`postscriptName`
130
-
*`fullName`
131
-
*`familyName`
132
-
*`subfamilyName`
133
-
*`copyright`
134
-
*`version`
139
+
-`postscriptName`
140
+
-`fullName`
141
+
-`familyName`
142
+
-`subfamilyName`
143
+
-`copyright`
144
+
-`version`
135
145
136
146
### Metrics
137
147
138
148
The following properties describe the general metrics of the font. See [here](http://www.freetype.org/freetype2/docs/glyphs/glyphs-3.html) for a good overview of how all of these properties relate to one another.
139
149
140
-
*`unitsPerEm` - the size of the font’s internal coordinate grid
141
-
*`ascent` - the font’s [ascender](http://en.wikipedia.org/wiki/Ascender_(typography))
142
-
*`descent` - the font’s [descender](http://en.wikipedia.org/wiki/Descender)
143
-
*`lineGap` - the amount of space that should be included between lines
144
-
*`underlinePosition` - the offset from the normal underline position that should be used
145
-
*`underlineThickness` - the weight of the underline that should be used
146
-
*`italicAngle` - if this is an italic font, the angle the cursor should be drawn at to match the font design
147
-
*`capHeight` - the height of capital letters above the baseline. See [here](http://en.wikipedia.org/wiki/Cap_height) for more details.
148
-
*`xHeight`- the height of lower case letters. See [here](http://en.wikipedia.org/wiki/X-height) for more details.
149
-
*`bbox` - the font’s bounding box, i.e. the box that encloses all glyphs in the font
150
+
-`unitsPerEm` - the size of the font’s internal coordinate grid
151
+
-`ascent` - the font’s [ascender](<http://en.wikipedia.org/wiki/Ascender_(typography)>)
152
+
-`descent` - the font’s [descender](http://en.wikipedia.org/wiki/Descender)
153
+
-`lineGap` - the amount of space that should be included between lines
154
+
-`underlinePosition` - the offset from the normal underline position that should be used
155
+
-`underlineThickness` - the weight of the underline that should be used
156
+
-`italicAngle` - if this is an italic font, the angle the cursor should be drawn at to match the font design
157
+
-`capHeight` - the height of capital letters above the baseline. See [here](http://en.wikipedia.org/wiki/Cap_height) for more details.
158
+
-`xHeight`- the height of lower case letters. See [here](http://en.wikipedia.org/wiki/X-height) for more details.
159
+
-`bbox` - the font’s bounding box, i.e. the box that encloses all glyphs in the font
150
160
151
161
### Other properties
152
162
153
-
*`numGlyphs` - the number of glyphs in the font
154
-
*`characterSet` - an array of all of the unicode code points supported by the font
155
-
*`availableFeatures` - an array of all [OpenType feature tags](https://www.microsoft.com/typography/otspec/featuretags.htm) (or mapped AAT tags) supported by the font (see below for a description of this)
163
+
-`numGlyphs` - the number of glyphs in the font
164
+
-`characterSet` - an array of all of the unicode code points supported by the font
165
+
-`availableFeatures` - an array of all [OpenType feature tags](https://www.microsoft.com/typography/otspec/featuretags.htm) (or mapped AAT tags) supported by the font (see below for a description of this)
156
166
157
167
### Character to glyph mapping
158
168
@@ -242,12 +252,12 @@ You do not create glyph objects directly. They are created by various methods on
242
252
243
253
### Properties
244
254
245
-
*`id` - the glyph id in the font
246
-
*`codePoints` - an array of unicode code points that are represented by this glyph. There can be multiple code points in the case of ligatures and other glyphs that represent multiple visual characters.
247
-
*`path` - a vector Path object representing the glyph
248
-
*`bbox` - the glyph’s bounding box, i.e. the rectangle that encloses the glyph outline as tightly as possible.
249
-
*`cbox` - the glyph’s control box. This is often the same as the bounding box, but is faster to compute. Because of the way bezier curves are defined, some of the control points can be outside of the bounding box. Where `bbox` takes this into account, `cbox` does not. Thus, `cbox` is less accurate, but faster to compute. See [here](http://www.freetype.org/freetype2/docs/glyphs/glyphs-6.html#section-2) for a more detailed description.
250
-
*`advanceWidth` - the glyph’s advance width.
255
+
-`id` - the glyph id in the font
256
+
-`codePoints` - an array of unicode code points that are represented by this glyph. There can be multiple code points in the case of ligatures and other glyphs that represent multiple visual characters.
257
+
-`path` - a vector Path object representing the glyph
258
+
-`bbox` - the glyph’s bounding box, i.e. the rectangle that encloses the glyph outline as tightly as possible.
259
+
-`cbox` - the glyph’s control box. This is often the same as the bounding box, but is faster to compute. Because of the way bezier curves are defined, some of the control points can be outside of the bounding box. Where `bbox` takes this into account, `cbox` does not. Thus, `cbox` is less accurate, but faster to compute. See [here](http://www.freetype.org/freetype2/docs/glyphs/glyphs-6.html#section-2) for a more detailed description.
260
+
-`advanceWidth` - the glyph’s advance width.
251
261
252
262
### `glyph.render(ctx, size)`
253
263
@@ -322,4 +332,5 @@ Includes the given glyph object or glyph ID in the subset.
322
332
Returns a [stream](https://nodejs.org/api/stream.html) containing the encoded font file that can be piped to a destination, such as a file.
0 commit comments