@nocker/font
v0.2.0
Published
Binary pixel fonts and Canvas rendering for games
Readme
@nocker/font
Binary pixel fonts and dependency-free Canvas rendering, in two sizes: 16×16 and 8×8. No React or DOM initialization on import.
Install:
npm install @nocker/fontImport the size you use. Each path carries its own font, so only that size's data is loaded:
import { renderText, layoutText, measureGlyph } from "@nocker/font/16x16"
// or: from "@nocker/font/8x8"
const canvas = document.querySelector<HTMLCanvasElement>("canvas")!
canvas.width = 960
canvas.height = 240
const context = canvas.getContext("2d")!
renderText(context, "こんにちワールド、はじめもして。", {
x: 8,
y: 8,
scale: 3,
letterSpacing: 1,
maxWidth: 304,
color: "#ffffff",
})
const layout = layoutText("ふぉんと Font", { maxWidth: 1000, letterSpacing: 1 })
// width=112, height=16, lineHeight=20 (logical pixels, 16x16)
const ink = measureGlyph("あ")
// {count, x, y, width, height}; computed from filled cellsTo use both sizes, import each under a name: import * as small from "@nocker/font/8x8".
| Path | Cell | Advance (full / ASCII) | Default line gap | Source |
| --- | --- | --- | --- | --- |
| @nocker/font/16x16 | 16×16 | 16 / 8 | 4 | DotGothic16 (SIL OFL 1.1) |
| @nocker/font/8x8 | 8×8 | 8 / 4 | 2 | Misaki Gothic by Num Kadoma |
Each path exports renderText(context, text, options), layoutText(text, options), drawText(canvas, text, options), measureGlyph(character) and font (the glyphs, as pixels[y][x] of 0 and 1).
renderText keeps the caller's canvas size, existing pixels and context state. It draws in the current context transform. Coordinates and maxWidth use logical pixels; scale converts them to canvas pixels. Supply integer x/y/scale/spacing, nonnegative spacing/gap and scale >= 1.
layoutText returns lines, width, height and lineHeight. Width uses pen advances, includes whitespace, and excludes trailing letterSpacing. Lines wrap by glyph, not by English word. Height is cell * lines + lineGap * (lines - 1). Small kana keep a full-width advance; their ink is smaller within the cell.
Text normalizes to NFC. Missing glyphs use ?; kanji are not included.
drawText is a preview helper: it resizes and clears the canvas, paints light text and returns width, height and line count. Use renderText to draw into an existing game.
The root path @nocker/font still exports pixel16, pixel8 and the same functions taking the font as their first argument after the context (renderText(context, pixel16, text, options)); it loads both sizes.
Raw JSON (16×16): @nocker/font/data. Each glyph row is hex, the left pixel in the high bit. License text (16×16): @nocker/font/license.
License
- 16×16: SIL OFL 1.1. Based on DotGothic16, Copyright 2020 The DotGothic16 Project Authors. Full license and upstream copyright are in
data/ofl.txtand embedded in the JSON. Preserve them on redistribution. Do not sell the font on its own. - 8×8: based on Misaki Gothic (美咲ゴシック), Copyright (C) 2002-2021 Num Kadoma. Its license, embedded in the JSON: free software; unlimited permission to use, copy and distribute, with or without modification, commercially or noncommercially; provided as is, without warranty.
