samsa-core

Samsa docs

SamsaBuffer.encodeInstance() method

This method is used to generate a binary TrueType font in memory. The resulting font is a static font, not a variable font. Clients may use the font in memory in all the ways they might use any other TrueType font, for example as as a webfont, to offer it for download, or to save it to disk.

Syntax

encodeInstance(instance, options);

Parameters

Return value

The size of the resulting font in bytes, which is the number of bytes written to the SamsaBuffer.

If an ArrayBuffer or Buffer is needed that contains the font and is precisely the size of the font, the client must create a new buffer of the appropriate size and copy the data from the SamsaBuffer to the new buffer.

Example

// samsa-instantiate.js
// This reads a font file and instantiates it at a certain designspace location.
// It then writes the resulting font to disk.
// Example usage:
// node samsa-instantiate.js Gingham.ttf '{"wght": 850, "wdth": 70}' 

import fs from "fs";
import { SamsaFont, SamsaBuffer } from "./samsa-core.js";

// load the font into a SamsaBuffer and create a SamsaFont object
const filename = process.argv[2];
const fontVariationSettings = process.argv[3] ? JSON.parse(process.argv[3]) : {};
const nBuffer0 = fs.readFileSync(filename);
const sBuffer0 = new SamsaBuffer(nBuffer0.buffer);
const font = new SamsaFont(sBuffer0);

// create a SamsaInstance at a certain designspace location
const instance = font.instance(fontVariationSettings);

// create a new SamsaBuffer large enough to contain the instantiated font
const sBuffer1 = new SamsaBuffer(new ArrayBuffer(sBuffer0.byteLength * 4));

// encode the SamsaInstance as a TrueType font in the buffer
const fontLength = sBuffer1.encodeInstance(instance);

// create a Uint8Array that is a view onto the font data in the buffer
const u8View = new Uint8Array(sBuffer1.buffer, 0, fontLength);

// convert the Uint8Array to a Node buffer
const nBuffer1 = Buffer.from(u8View);

// write the buffer to disk
fs.writeFileSync("static-font.ttf", nBuffer1);

Limitations

There is currently no easy way to specify the safe size of the buffer needed to contain the font (hence the *4 in the example). The length of the variable font will usually be safe. However, if options.glyphCompression is set to false, then a significantly more memory might be needed.

Any variations handled by MVAR, cvar, GSUB, GPOS tables (and others) are not saved in the generated font. The to do list includes: