SLF63 Single Line Font
A port of the "SLF63" single-line font format (bespoke, "Hershey"-like) for Turtletoy.
Log in to post a comment.
// a turtletoy conversion of "SLF63" single-line fonts
// see https://github.com/davebollinger/SLF63
// (a nearly straight port: not optimized for size or etc)
// there are other fonts in the repo that could easily be swapped here
const SLF63_EUROY_JSON = {
"type": "SLF63",
"name": "EUROY",
"cite": "@davebollinger 2026",
"cols": 7,
"rows": 9,
"capheight": 6,
"xheight": 4,
"baserow": 6,
"glyphs": {
" ": {"strokes":[],"xspan":[1.5,4.5]},
"!": {"strokes":[[[3,0],[3,4.25]],[[2.5,5.25],[3.5,5.25],[3.5,6],[2.5,6],[2.5,5.25]]],"xspan":[2.5,3.5]},
"\"": {"strokes":[[[1.5,0],[1.5,2]],[[4.5,0],[4.5,2]]],"xspan":[1.5,4.5]},
"#": {"strokes":[[[2,0],[1.5,6]],[[4.5,0],[4,6]],[[0,2],[6,2]],[[0,4],[6,4]]],"xspan":[0,6]},
"$": {"strokes":[[[6,0.75],[5,0],[1,0],[0,0.75],[0,2.25],[1,3],[5,3],[6,3.75],[6,5.25],[5,6],[1,6],[0,5.25]],[[3,0],[3,6]]],"xspan":[0,6]},
"%": {"strokes":[[[0.5,6],[5.5,0]],[[0.5,0],[2,0],[2.5,0.5],[2.5,1.5],[2,2],[0.5,2],[0,1.5],[0,0.5],[0.5,0]],[[4,4],[5.5,4],[6,4.5],[6,5.5],[5.5,6],[4,6],[3.5,5.5],[3.5,4.5],[4,4]]],"xspan":[0,6]},
"&": {"strokes":[[[6,6],[0.5,2],[0.5,0.75],[1.5,0],[3.5,0],[4.5,0.75],[4.5,2],[0,4],[0,5.25],[1,6],[4,6],[6,4]]],"xspan":[0,6]},
"'": {"strokes":[[[3,0],[3,2]]],"xspan":[2.5,3.5]},
"(": {"strokes":[[[4.5,0],[2.5,1],[2,2.25],[2,4.75],[2.5,6],[4.5,7]]],"xspan":[2,4.5]},
")": {"strokes":[[[1.5,0],[3.5,1],[4,2.25],[4,4.75],[3.5,6],[1.5,7]]],"xspan":[1.5,4]},
"*": {"strokes":[[[3,0.5],[3,5.5]],[[1,1.5],[5,4.5]],[[5,1.5],[1,4.5]]],"xspan":[1,5]},
"+": {"strokes":[[[3,1],[3,5]],[[0.5,3],[5.5,3]]],"xspan":[0.5,5.5]},
",": {"strokes":[[[2.5,5],[3.5,5],[3.5,6],[2,7]]],"xspan":[2,3.5]},
"-": {"strokes":[[[0.5,3],[5.5,3]]],"xspan":[0.5,5.5]},
".": {"strokes":[[[2.5,5.25],[3.5,5.25],[3.5,6],[2.5,6],[2.5,5.25]]],"xspan":[2.5,3.5]},
"/": {"strokes":[[[0.5,6],[5.5,0]]],"xspan":[0.5,5.5]},
"0": {"strokes":[[[1,0],[5,0],[6,0.75],[6,5.25],[5,6],[1,6],[0,5.25],[0,0.75],[1,0]]],"xspan":[0,6]},
"1": {"strokes":[[[1.5,1.25],[3,0],[3,6]],[[1,6],[5,6]]],"xspan":[1,5]},
"2": {"strokes":[[[0,1],[1,0],[5,0],[6,0.75],[6,2.25],[5,3],[1,3],[0,3.75],[0,6],[6,6]]],"xspan":[0,6]},
"3": {"strokes":[[[0,0.75],[1,0],[5,0],[6,0.75],[6,2.25],[5,3],[2.5,3]],[[5,3],[6,3.75],[6,5.25],[5,6],[1,6],[0,5.25]]],"xspan":[0,6]},
"4": {"strokes":[[[4.5,6],[4.5,0],[3.5,0],[0,3.75],[0,4.25],[6,4.25]]],"xspan":[0,6]},
"5": {"strokes":[[[6,0],[0.5,0],[0.5,2.75],[5,2.75],[6,3.5],[6,5.25],[5,6],[1,6],[0,5.25]]],"xspan":[0,6]},
"6": {"strokes":[[[6,0.75],[5,0],[1,0],[0,0.75],[0,5.25],[1,6],[5,6],[6,5.25],[6,3.5],[5,2.75],[0,2.75]]],"xspan":[0,6]},
"7": {"strokes":[[[0,0],[6,0],[6,0.75],[2.5,4.5],[2.5,6]]],"xspan":[0,6]},
"8": {"strokes":[[[1,0],[5,0],[5.75,0.75],[5.75,2.25],[5,3],[1,3],[0.25,2.25],[0.25,0.75],[1,0]],[[1,3],[0,3.75],[0,5.25],[1,6],[5,6],[6,5.25],[6,3.75],[5,3]]],"xspan":[0,6]},
"9": {"strokes":[[[6,3.25],[1,3.25],[0,2.5],[0,0.75],[1,0],[5,0],[6,0.75],[6,5.25],[5,6],[1,6],[0,5.25]]],"xspan":[0,6]},
":": {"strokes":[[[2.5,1.5],[3.5,1.5],[3.5,2.25],[2.5,2.25],[2.5,1.5]],[[2.5,5.25],[3.5,5.25],[3.5,6],[2.5,6],[2.5,5.25]]],"xspan":[2.5,3.5]},
";": {"strokes":[[[2.5,1.5],[3.5,1.5],[3.5,2.25],[2.5,2.25],[2.5,1.5]],[[2.5,5],[3.5,5],[3.5,6],[2,7]]],"xspan":[2,3.5]},
"<": {"strokes":[[[5.5,1],[0.5,3],[5.5,5]]],"xspan":[0.5,5.5]},
"=": {"strokes":[[[0.5,2.25],[5.5,2.25]],[[0.5,3.75],[5.5,3.75]]],"xspan":[0.5,5.5]},
">": {"strokes":[[[0.5,1],[5.5,3],[0.5,5]]],"xspan":[0.5,5.5]},
"?": {"strokes":[[[0,0.75],[1,0],[5,0],[6,0.75],[6,2.25],[5,3],[3,3],[3,4.25]],[[2.5,5.25],[3.5,5.25],[3.5,6],[2.5,6],[2.5,5.25]]],"xspan":[0,6]},
"@": {"strokes":[[[4.5,4],[2.5,4],[2,3.5],[2,2.5],[2.5,2],[4.5,2],[4.5,4.5],[5.5,4.5],[6,4],[6,0.75],[5,0],[1,0],[0,0.75],[0,5.25],[1,6],[5,6]]],"xspan":[0,6]},
"A": {"strokes":[[[0,6],[2.25,0],[3.75,0],[6,6]],[[0.75,4],[5.25,4]]],"xspan":[0,6]},
"B": {"strokes":[[[0.5,0],[0.5,6]],[[0.5,0],[4.5,0],[5.5,0.75],[5.5,2.25],[4.5,3],[0.5,3]],[[4.5,3],[6,3.75],[6,5.25],[5,6],[0.5,6]]],"xspan":[0.5,6]},
"C": {"strokes":[[[6,1],[5,0],[1,0],[0,0.75],[0,5.25],[1,6],[5,6],[6,5]]],"xspan":[0,6]},
"D": {"strokes":[[[0.5,0],[0.5,6]],[[0.5,0],[4.5,0],[6,1],[6,5],[4.5,6],[0.5,6]]],"xspan":[0.5,6]},
"E": {"strokes":[[[6,0],[0.5,0],[0.5,6],[6,6]],[[0.5,3],[5,3]]],"xspan":[0.5,6]},
"F": {"strokes":[[[6,0],[0.5,0],[0.5,6]],[[0.5,3],[5,3]]],"xspan":[0.5,6]},
"G": {"strokes":[[[6,1],[5,0],[1,0],[0,0.75],[0,5.25],[1,6],[5,6],[6,5.25],[6,3.25],[3.5,3.25]]],"xspan":[0,6]},
"H": {"strokes":[[[0.5,0],[0.5,6]],[[5.5,0],[5.5,6]],[[0.5,3],[5.5,3]]],"xspan":[0.5,5.5]},
"I": {"strokes":[[[3,0],[3,6]]],"xspan":[3,3]},
"J": {"strokes":[[[5.5,0],[5.5,5.25],[4.5,6],[1,6],[0,5.25],[0,4]]],"xspan":[0,5.5]},
"K": {"strokes":[[[0.5,0],[0.5,6]],[[6,0],[1.75,3],[0.5,3]],[[2.25,2.75],[6,6]]],"xspan":[0.5,6]},
"L": {"strokes":[[[0.5,0],[0.5,6],[6,6]]],"xspan":[0.5,6]},
"M": {"strokes":[[[0,6],[0,0],[0.75,0],[3,3.75],[5.25,0],[6,0],[6,6]]],"xspan":[0,6]},
"N": {"strokes":[[[0.5,6],[0.5,0],[1,0],[5,6],[5.5,6],[5.5,0]]],"xspan":[0.5,5.5]},
"O": {"strokes":[[[1,0],[5,0],[6,0.75],[6,5.25],[5,6],[1,6],[0,5.25],[0,0.75],[1,0]]],"xspan":[0,6]},
"P": {"strokes":[[[0.5,6],[0.5,0],[5,0],[6,0.75],[6,2.5],[5,3.25],[0.5,3.25]]],"xspan":[0.5,6]},
"Q": {"strokes":[[[1,0],[5,0],[6,0.75],[6,5.25],[5,6],[1,6],[0,5.25],[0,0.75],[1,0]],[[3.5,4.5],[6,6.5]]],"xspan":[0,6]},
"R": {"strokes":[[[0.5,6],[0.5,0],[5,0],[6,0.75],[6,2.25],[5,3],[0.5,3]],[[3.5,3],[6,6]]],"xspan":[0.5,6]},
"S": {"strokes":[[[6,0.75],[5,0],[1,0],[0,0.75],[0,2.25],[1,3],[5,3],[6,3.75],[6,5.25],[5,6],[1,6],[0,5.25]]],"xspan":[0,6]},
"T": {"strokes":[[[0,0],[6,0]],[[3,0],[3,6]]],"xspan":[0,6]},
"U": {"strokes":[[[0.5,0],[0.5,5.25],[1.5,6],[4.5,6],[5.5,5.25],[5.5,0]]],"xspan":[0.5,5.5]},
"V": {"strokes":[[[0,0],[2.5,6],[3.5,6],[6,0]]],"xspan":[0,6]},
"W": {"strokes":[[[0,0],[1.25,6],[2,6],[3,3],[4,6],[4.75,6],[6,0]]],"xspan":[0,6]},
"X": {"strokes":[[[0,0],[6,6]],[[6,0],[0,6]]],"xspan":[0,6]},
"Y": {"strokes":[[[0,0],[3,3.25],[6,0]],[[3,3.25],[3,6]]],"xspan":[0,6]},
"Z": {"strokes":[[[0,0],[6,0],[0,6],[6,6]]],"xspan":[0,6]},
"[": {"strokes":[[[4.5,0],[2,0],[2,7],[4.5,7]]],"xspan":[2,4.5]},
"\\": {"strokes":[[[0.5,0],[5.5,6]]],"xspan":[0.5,5.5]},
"]": {"strokes":[[[1.5,0],[4,0],[4,7],[1.5,7]]],"xspan":[1.5,4]},
"^": {"strokes":[[[0.5,2.5],[3,0],[5.5,2.5]]],"xspan":[0.5,5.5]},
"_": {"strokes":[[[0,7],[6,7]]],"xspan":[0,6]},
"`": {"strokes":[[[2,0],[3.5,1.5]]],"xspan":[2,3.5]},
"a": {"strokes":[[[0.5,2.5],[1.25,2],[4.75,2],[5.5,2.75],[5.5,6]],[[5.5,3.75],[1.25,3.75],[0.5,4.25],[0.5,5.5],[1.25,6],[4.75,6],[5.5,5.5]]],"xspan":[0.5,5.5]},
"b": {"strokes":[[[0.5,0],[0.5,6]],[[0.5,2],[4.75,2],[5.5,2.75],[5.5,5.25],[4.75,6],[0.5,6]]],"xspan":[0.5,5.5]},
"c": {"strokes":[[[5.5,2.75],[4.75,2],[1.25,2],[0.5,2.75],[0.5,5.25],[1.25,6],[4.75,6],[5.5,5.25]]],"xspan":[0.5,5.5]},
"d": {"strokes":[[[5.5,0],[5.5,6]],[[5.5,2],[1.25,2],[0.5,2.75],[0.5,5.25],[1.25,6],[5.5,6]]],"xspan":[0.5,5.5]},
"e": {"strokes":[[[0.5,4],[5.5,4],[5.5,2.75],[4.75,2],[1.25,2],[0.5,2.75],[0.5,5.25],[1.25,6],[4.75,6],[5.5,5.5]]],"xspan":[0.5,5.5]},
"f": {"strokes":[[[5,0],[2.75,0],[2,0.75],[2,6]],[[1,2.25],[4.5,2.25]]],"xspan":[1,5]},
"g": {"strokes":[[[5.5,2],[1.25,2],[0.5,2.75],[0.5,5.25],[1.25,6],[5.5,6]],[[5.5,2],[5.5,7.25],[4.75,8],[1.25,8],[0.5,7.5]]],"xspan":[0.5,5.5]},
"h": {"strokes":[[[0.5,0],[0.5,6]],[[0.5,2],[4.75,2],[5.5,2.75],[5.5,6]]],"xspan":[0.5,5.5]},
"i": {"strokes":[[[3,0.5],[3,1]],[[3,2],[3,6]]],"xspan":[3,3]},
"j": {"strokes":[[[4,0.5],[4,1]],[[4,2],[4,7.25],[3.25,8],[1.5,8],[1,7.5]]],"xspan":[1,4]},
"k": {"strokes":[[[0.5,0],[0.5,6]],[[5.5,2],[1.5,4],[0.5,4]],[[2,3.75],[5.5,6]]],"xspan":[0.5,5.5]},
"l": {"strokes":[[[3,0],[3,5.5],[3.75,6]]],"xspan":[3,3.75]},
"m": {"strokes":[[[0,2],[0,6]],[[0,2],[2.5,2],[3,2.5],[3,6]],[[3,2.5],[3.5,2],[5.25,2],[6,2.75],[6,6]]],"xspan":[0,6]},
"n": {"strokes":[[[0.5,2],[0.5,6]],[[0.5,2],[4.75,2],[5.5,2.75],[5.5,6]]],"xspan":[0.5,5.5]},
"o": {"strokes":[[[1.25,2],[4.75,2],[5.5,2.75],[5.5,5.25],[4.75,6],[1.25,6],[0.5,5.25],[0.5,2.75],[1.25,2]]],"xspan":[0.5,5.5]},
"p": {"strokes":[[[0.5,2],[0.5,8]],[[0.5,2],[4.75,2],[5.5,2.75],[5.5,5.25],[4.75,6],[0.5,6]]],"xspan":[0.5,5.5]},
"q": {"strokes":[[[5.5,2],[5.5,8]],[[5.5,2],[1.25,2],[0.5,2.75],[0.5,5.25],[1.25,6],[5.5,6]]],"xspan":[0.5,5.5]},
"r": {"strokes":[[[1,2],[1,6]],[[1,2],[4.5,2],[5,2.5],[5,3]]],"xspan":[1,5]},
"s": {"strokes":[[[5.5,2.5],[4.75,2],[1.25,2],[0.5,2.5],[0.5,3.5],[1.25,4],[4.75,4],[5.5,4.5],[5.5,5.5],[4.75,6],[1.25,6],[0.5,5.5]]],"xspan":[0.5,5.5]},
"t": {"strokes":[[[2.25,0.5],[2.25,5.25],[3,6],[5,6]],[[1,2.25],[4.5,2.25]]],"xspan":[1,5]},
"u": {"strokes":[[[0.5,2],[0.5,5.25],[1.25,6],[5.5,6]],[[5.5,2],[5.5,6]]],"xspan":[0.5,5.5]},
"v": {"strokes":[[[0.5,2],[2.5,6],[3.5,6],[5.5,2]]],"xspan":[0.5,5.5]},
"w": {"strokes":[[[0,2],[1.25,6],[2,6],[3,3.5],[4,6],[4.75,6],[6,2]]],"xspan":[0,6]},
"x": {"strokes":[[[0.5,2],[5.5,6]],[[5.5,2],[0.5,6]]],"xspan":[0.5,5.5]},
"y": {"strokes":[[[0.5,2],[0.5,5.25],[1.25,6],[5.5,6]],[[5.5,2],[5.5,7.25],[4.75,8],[1.25,8],[0.5,7.5]]],"xspan":[0.5,5.5]},
"z": {"strokes":[[[0.5,2],[5.5,2],[0.5,6],[5.5,6]]],"xspan":[0.5,5.5]},
"{": {"strokes":[[[4.5,0],[3,0.5],[3,3],[1.5,3.5],[3,4],[3,6.5],[4.5,7]]],"xspan":[1.5,4.5]},
"|": {"strokes":[[[3,0],[3,7]]],"xspan":[3,3]},
"}": {"strokes":[[[1.5,0],[3,0.5],[3,3],[4.5,3.5],[3,4],[3,6.5],[1.5,7]]],"xspan":[1.5,4.5]},
"~": {"strokes":[[[0.5,3.5],[2,2.5],[4,3.5],[5.5,2.5]]],"xspan":[0.5,5.5]}
}
};
// NB re turtletoy: nearly verbatim from repo's SLF63.js
/**
* A class for handling Single-Line Font data in SLF63 format
*/
class SLF63 {
static BLANK_GLYPH = { strokes: [], xspan:[0,6] };
constructor(fontjson) {
Object.assign(this, fontjson); // just a decorator
}
/**
* Converts a string of text into an array of polylines using the font's glyphs.
* Low-level - primarily for internal use.
* Think of this as the "layout engine" that other routines would then "render" its results.
* @param txt {string} - The text string to convert
* @param x {number} - The x-coordinate for the left of the rendered text
* @param y {number} - The y-coordinate for the top of the rendered text
* @param capHeight {number} - The font size in output units (default: 10)
* @param options {object} - An optional dictionary of various styling options:
* @param options.alignX {string} - x-alignment: left (default), center, right
* @param options.alignY {string} - y-alignment: top (default), middle (of cap height), center (of full height), baseline, bottom
* @param options.bold {boolean} - if true doubles polylines to mimic bold, if false (default) does not double polylines
* @param options.boldOffset {number} - the x-offset to use when doubling polylines for bold (default: 0.2)
* @param options.italic {boolean} - if true shears polylines to mimic italic, if false (default) does not shear polylines
* @param options.italicShear {number} - the degree of shear when italic is used, 0:no shear, 1:45-degree shear, -1:-45-degree shear (default: 0.3)
* @param options.letterSpacing {number} - the spacing between letters (default: fontsize/6*2) in output units
* @param options.proportional {boolean} - if true (default) uses proportional glyph widths, if false uses full glyph widths
* @param options.scaleX {number} - additional x-coordinate scaling (default: 1.0)
* @param options.scaleY {number} - additional y-coordinate scaling (default: 1.0)
* @returns {array} - Array of polylines in output units
*/
textToPolylines(txt, x, y, capHeight=10, options={}) {
if (!txt || txt.length === 0) return [];
// normalize internal-grid-size to output-font-size
// NB re turtletoy: maybe(?) renormalize this so that fontsizes "make sense"
let unitScale = capHeight / this.capheight;
// retrieve options:
let alignX = options.alignX !== undefined ? options.alignX : "left";
let alignY = options.alignY != undefined ? options.alignY : "top";
let bold = options.bold != undefined ? options.bold : false;
let boldOffset = options.boldOffset !== undefined ? options.boldOffset : 0.2; // modified for turtletoy
let italic = options.italic != undefined ? options.italic : false;
let italicShear = options.italicShear !== undefined ? options.italicShear : 0.3;
let letterSpacing = options.letterSpacing !== undefined ? options.letterSpacing : unitScale * 2.0;
let proportional = options.proportional !== undefined ? options.proportional : true;
let scaleX = options.scaleX != undefined ? options.scaleX : 1;
let scaleY = options.scaleY != undefined ? options.scaleY : 1;
// precalc metrics
let cursorX = x;
let totalSpanRows = this.rows-1;
if (alignY=="middle") y-=(this.capheight/2)*unitScale*scaleY;
else if (alignY=="center") y-=(totalSpanRows/2)*unitScale*scaleY;
else if (alignY=="baseline") y-=(this.capheight)*unitScale*scaleY;
else if (alignY=="bottom") y-=totalSpanRows*unitScale*scaleY;
let baselineY = y + this.baserow * unitScale * scaleY;
let stepAdvance = letterSpacing * scaleX;
let boldAdvance = bold ? boldOffset : 0;
// actual processing:
let lines = [];
for (let i = 0; i < txt.length; i++) {
let ch = txt[i];
let glyph = this.glyphs[ch] || SLF63.BLANK_GLYPH;
let strokes = glyph.strokes || [];
let gMinX = proportional ? glyph.xspan[0] : 0;
let gMaxX = proportional ? glyph.xspan[1] : (this.cols - 1);
let gWidth = Math.max(0, gMaxX - gMinX);
for (let line of strokes) {
if (line.length < 2) continue;
let pts = line.map(pt => {
let shearOffset = italic ? (this.baserow - pt[1]) * italicShear * unitScale * scaleY : 0;
return [
cursorX + (pt[0] - gMinX) * unitScale * scaleX + shearOffset,
baselineY + (pt[1] - this.baserow) * unitScale * scaleY
];
});
lines.push({ points: pts, char: ch });
if (bold) {
let opts = pts.map(pt => ([ pt[0]+boldOffset, pt[1] ]));
lines.push({ points: opts, char: ch });
}
}
cursorX += gWidth * unitScale * scaleX + boldAdvance + stepAdvance;
}
// horizontal alignment is post-processing
if (alignX === "center" || alignX === "right") {
let totalWidth = Math.max(0, cursorX - stepAdvance - x);
let shiftX = alignX === "center" ? -totalWidth / 2 : -totalWidth;
for (let line of lines) {
for (let pt of line.points) {
pt[0] += shiftX;
}
}
}
return lines;
} // textToPolylines()
/*
* THAT'S ABOUT IT FOR THE ACTUAL FONT ITSELF
* THE REST BELOW IS JUST OPTIONAL "HELPERS"
*/
/**
* Renders an array of polylines (with p5js).
* @param lines {array} - Array of polylines to render
*/
drawPolylines(lines) {
// modified for turtletoy
for (let line of lines) {
if (line.points.length < 2) continue;
turtle.jump(line.points[0][0], line.points[0][1]);
for (let i=1; i<line.points.length; i++) {
let pt = line.points[i];
turtle.goto(pt[0], pt[1]);
}
}
}
/**
* Renders a string of text as polylines using the font's glyphs.
* @param txt {string} - The text string to convert
* @param x {number} - The x-coordinate for the left of the rendered text
* @param y {number} - The y-coordinate for the top of the rendered text
* @param capHeight {number} - The font size in output units (default: 10)
* @param options {object} - An optional dictionary of various styling options
* @see textToPolylines()
*/
text(txt, x, y, capHeight=10, options={}) {
let lines = this.textToPolylines(txt, x, y, capHeight, options);
this.drawPolylines(lines);
}
}
const turtle = new Turtle();
const font = new SLF63(SLF63_EUROY_JSON);
const txt = "The quick brown fox jumps over the lazy dog."; // type=string
const tx = 0, ty = -40;
font.text("SLF63", tx, ty-10, 12, {alignX:"center"});
font.text(txt, tx, ty+10, 4, {alignX:"center"});
font.text(txt, tx, ty+20, 4, {alignX:"center", italic:true});
font.text(txt, tx, ty+30, 4, {alignX:"center", bold:true});
font.text(txt, tx, ty+40, 4, {alignX:"center", italic:true, italicShear:-0.5});
font.text(txt, tx, ty+50, 3, {alignX:"center"});
font.text(txt, tx, ty+60, 2, {alignX:"center"});
font.text(txt, tx, ty+70, 1, {alignX:"center"});
font.text("0123456789<>(){}[]|\\@#$%^&* .. etc", tx, ty+80, 4, {alignX:"center"});
function walk(i) { return false; }