First, ensure you have the required tools installed:
Linux/macOS:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/envWindows: Download and run: https://rustup.rs/
Verify installation:
rustc --version
cargo --versionDownload from: https://nodejs.org/ (LTS version recommended)
Verify installation:
node --version
npm --versionNavigate to your project directory and install dependencies:
cd ttf-parser-node
npm installBuild in release mode (recommended):
npm run buildOr build in debug mode for development:
npm run build-debugThis will:
- Compile the Rust code to a native Node.js addon
- Generate
index.nodein your project directory
Run the test script:
# With default system font
npm test
# Or specify a font file
node test.js /path/to/your/font.ttfCreate a new file example.js:
const Font = require('./index.js');
// Load a font
const font = Font.fromFile('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf');
// Print basic info
console.log('Font Family:', font.familyName);
console.log('Glyphs:', font.numberOfGlyphs);
console.log('Units per EM:', font.unitsPerEm);
// Get info for character 'A'
const glyphId = font.glyphIndex('A');
console.log('Glyph ID for "A":', glyphId);
const advance = font.glyphHorAdvance(glyphId);
console.log('Advance width:', advance);Run it:
node example.jsSolution: Build the native module first:
npm run buildSolution: Install Rust or ensure cargo is in your PATH:
source $HOME/.cargo/env # Linux/macOSSolution:
- Linux: Install build essentials:
sudo apt-get install build-essential
- Windows: Install Visual Studio Build Tools
- macOS: Install Xcode command line tools:
xcode-select --install
Solution: Ensure:
- The file path is correct
- The file is a valid TrueType/OpenType font
- You have read permissions for the file
Copy these files to your project:
index.js(JavaScript wrapper)index.node(compiled native module)package.json(dependencies)
Then in your code:
const Font = require('./path/to/ttf-parser-node');- Update
package.jsonwith your details - Ensure
.gitignoreexcludesindex.nodeandtarget/ - Users will need to build locally or you can publish pre-built binaries
# In ttf-parser-node directory
npm link
# In your project directory
npm link ttf-parser-node- Edit
src/lib.rs - Rebuild:
npm run build
- Test your changes:
node test.js
-
Add Rust function in
src/lib.rs:fn my_new_function(mut cx: FunctionContext) -> JsResult<JsNumber> { // Your Rust code here Ok(cx.number(42.0)) }
-
Export in
mainfunction:cx.export_function("myNewFunction", my_new_function)?;
-
Add JavaScript wrapper in
index.js:myNewFunction() { return addon.myNewFunction(this.buffer); }
-
Rebuild and test:
npm run build node test.js
- Reuse Font instances - Parsing is fast but not free
- Batch operations - Use
getAllAdvances()instead of callingglyphHorAdvance()repeatedly - Cache glyph IDs - If you're measuring the same text repeatedly
- Use release builds - They're significantly faster than debug builds
- Read the full README.md for API documentation
- Check out the ttf-parser documentation for more features to expose
- Explore adding glyph outline extraction, font subsetting, etc.
If you encounter issues:
- Check this guide and README.md
- Verify your Rust and Node.js installations
- Try with a simple, known-good font file (like DejaVu Sans)
- Check the ttf-parser repository for known issues