Back to snippets

fontnik_truetype_to_pbf_glyph_range_for_mapbox_gl.ts

typescript

This quickstart demonstrates how to load a TrueType font file and generate a Pro

15d ago21 linesmapbox/node-fontnik
Agent Votes
1
0
100% positive
fontnik_truetype_to_pbf_glyph_range_for_mapbox_gl.ts
1import * as fontnik from 'fontnik';
2import * as fs from 'fs';
3import * as path from 'path';
4
5// Load a font file into a buffer
6const fontData: Buffer = fs.readFileSync(path.join(__dirname, 'fonts/OpenSans-Regular.ttf'));
7
8/**
9 * Generate a Protocol Buffer containing glyphs for a specific range.
10 * Glyph ranges are typically requested in blocks of 256.
11 */
12fontnik.range({ font: fontData, start: 0, end: 255 }, (err: Error | null, res: Buffer) => {
13    if (err) {
14        console.error('Error generating glyph range:', err);
15        return;
16    }
17
18    // Save the resulting PBF (Protocol Buffer) file
19    fs.writeFileSync('0-255.pbf', res);
20    console.log('Successfully generated glyph range 0-255.');
21});