samsa-core

Samsa docs

SamsaFont object

Description

The SamsaFont object is the fundamental object in the Samsa Core library. It is created from a binary TrueType font stored in a SamsaBuffer. Note that SamsaBuffer is a subclass of the standard JavaScript DataView. The SamsaFont object is used to create SamsaInstance objects, which are then used to render text using the SamsaInstance.renderText() method.

Constructor

SamsaFont()

Instance properties

Instance methods

Examples

The essential steps are to obtain a SamsaBuffer that contains a TrueType font, and make a SamsaFont from it.

const buf = new SamsaBuffer(arrayBuffer); // arrayBuffer contains a TrueType font
const font = new SamsaFont(buf);

There are numerous ways to get a font file into an ArrayBuffer. Common ways on the web are fetch() and File drag and drop. Common ways in Node.js are fetch and fs.readFile().

Example using fetch() (works on the web and Node.js):

const fontUrl = "https://example.com/font.ttf";

fetch(fontUrl)
	.then(response => {
		if (!response.ok) {
			throw new Error("Failed to fetch font");
		}

		const arrayBuffer = response.arrayBuffer();
		const buf = new SamsaBuffer(arrayBuffer);
		const font = new SamsaFont(buf);

		// do stuff with the font here

    });

Example using fs.readFileSync() (works in Node.js):

const fs = require("fs");
const fontPath = "path/to/font.ttf";

const data = fs.readFileSync(fontPath);
const buf = new SamsaBuffer(data.buffer);
const font = new SamsaFont(buf);

// do stuff with the font here