xxxxxxxxxx1
81
// displaying hershey fonts from https://github.com/techninja/hersheytextjs in// p5js// convert a hersheytextjs SVG path to a list of opentype-esque commandsfunction pathCommands(s, offset) { console.log(s, offset); let instr = []; let mode = ''; for (let t of s.split(' ')) { if (t.charAt(0) == 'M' || t.charAt(0) == 'L') { mode = t.charAt(0); t = t.substr(1); } let coords = t.split(','); if (mode == 'M') { instr.push({type: 'M', 'x': int(coords[0])+offset, 'y': int(coords[1])}); } else if (mode == 'L') { instr.push({type: 'L', 'x': int(coords[0])+offset, 'y': int(coords[1])}); } } return instr;}// produce an array of commands for the given stringfunction pathCommandsForText(font, s) { console.log(font, s); let commands = []; let offset = 0; for (let i = 0; i < s.length; i++) { let cidx = s.charCodeAt(i) - 33; if (cidx >= 0) { Array.prototype.push.apply( commands, pathCommands(font.chars[cidx].d, offset)); offset += int(font.chars[cidx].o) * 2; } else { offset += 10; } } return commands;}// draw commandsfunction drawPath(cmds) { // current pen position let cx = 0; let cy = 0; // start position of current contour let startX = 0; let startY = 0; for (let cmd of cmds) { switch (cmd.type) { case 'M': startX = cmd.x; startY = cmd.y; cx = cmd.x; cy = cmd.y; break; case 'L': line(cx, cy, cmd.x, cmd.y); cx = cmd.x; cy = cmd.y; break; } }}let hershey;function preload() { hershey = loadJSON("hersheytext.json");}function setup() { createCanvas(400, 400); strokeWeight(3); scale(2); drawPath(pathCommandsForText(hershey.futural, "hello there")); noLoop();}