-
Notifications
You must be signed in to change notification settings - Fork 0
Getting started
Integrating Derakuma is easy on your Javascript/Typescript web app (frameworks like React, Vue, etc, can be used but not entirely necessary), or on your Node.js/Bun app.
This guide may be only for novices looking on how to integrate. For detailed parts, refer to other sections in this wiki.
If you wish to integrate Derakuma into Node/Bun, you may do so. Depending on your package manager, you can use either:
npm i derakuma
or:
yarn add derakuma
If you are using Bun:
bun add derakuma
Derakuma not only supports Node/Bun, but can be accessed on the web. Simply put:
<script src="https://unpkg.com/derakuma@latest/dist/web.min.js" crossorigin="anonymous"></script>on either <head> or <body>.
Derakuma has TypeScript declarations already built in, so you can code your project/app involving Derakuma, in Typescript.
import { DerakumaParser } from 'derakuma';
(async () => {
const myFont = new DerakumaParser('https://example.com/myfont.bene');
await myFont.ready();
const letterA = myFont.getGlyph('A'); // Gets the glyph sets for A, which is U+0041.
console.log(letterA);
// Returns a series of drawing commands (which is PenCommand[])
/* Example: [
{ command: 'PD', x: 0.86, y: 2.57 },
{ command: 'MP', x: 5.14, y: 2.57 },
{ command: 'PU', x: 5.14, y: 2.57 },
{ command: 'PD', x: 0, y: 0 },
{ command: 'MP', x: 3, y: 9 },
{ command: 'MP', x: 6, y: 0 },
{ command: 'PU', x: 6, y: 0 }
] */
})();Derakuma has two methods when loading a font, FETCH and FILE. These can be supplied by importing the FontLoadMethod enum, and can be supplied after the font path.
The difference between FETCH and FILE is the method of loading the .bene font. FETCH does a web request in order to fetch your fonts. This is suitable if your font is fetched from an external URL. FILE, on the other hand, fetches your fonts from your disk. This is significantly faster than FETCH but the main drawback is that you are unable to load external fonts on other networks. However, without anything, Derakuma will use FETCH automatically.
When FILE is selected, you do not need to append await DerakumaParser.ready(); Derakuma will fetch your font files synchronously, so you can start working away. If your font is encoded differently than UTF-8 (e.g. UTF-16LE), you may do so! Append your encoding (part of Node's BufferEncoding) after the font loading method. If no encoding is supplied, Derakuma will read in UTF-8 mode by default.
Here's an example of FILE in action (using path).
import { DerakumaParser, FontLoadMethod } from 'derakuma';
import * as path from 'path'
const myFont = new DerakumaParser('https://example.com/myfont.bene', FontLoadMethod.FILE, 'UTF-16LE');
// Same as above
const letterA = myFont.getGlyph('A');
console.log(letterA);Integrating in web is vastly different than the privilege you get in Node/Bun. Thankfully, Derakuma is also built for the web, which means you can use the normal Derakuma code you had, but with a catch.
- You are only locked on
FETCHmode. - You must wrap Derakuma asynchronously.
You can still initialize Derakuma out of the box, like you would do, just with the caveats highlighted
let myFont; // Have a reference ready
(async () => {
myFont = new DerakumaParser('https://example.com/myfont.bene');
await myFont.ready();
const letterA = myFont.getGlyph('A'); // Gets the glyph sets for A, which is U+0041.
console.log(letterA);
// Returns a series of drawing commands
})();You can use a variety of methods to draw the glyphs and you only need to pick only one that suits for your application. However, in this example, we will keep it simple and use Canvas2D for now, although you can use other rendering methods (e.g. WebGL, WebGPU) but consider this as a starting point.