-
Notifications
You must be signed in to change notification settings - Fork 18
/
sketch2.ts
83 lines (73 loc) · 2.28 KB
/
sketch2.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//#!tsc && NODE_PATH=dist/src node dist/sketch2.js
import {
Point,
Rect,
TextHjustify,
TextVjustify,
Transform,
DECIDEG2RAD,
TextAngle,
} from "./src/kicad_common";
import { StrokeFont } from "./src/kicad_strokefont";
import { Plotter, CanvasPlotter } from "./src/kicad_plotter";
import * as fs from "fs";
{
const font = StrokeFont.instance;
const width = 2000, height = 2000;
const Canvas = require('canvas');
const canvas = Canvas.createCanvas ? Canvas.createCanvas(width, height) : new Canvas(width, height);
const ctx = canvas.getContext('2d');
ctx.strokeStyle = "#666666";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(canvas.width / 2, 0);
ctx.lineTo(canvas.width / 2, canvas.height);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, canvas.height / 2);
ctx.lineTo(canvas.width, canvas.height / 2);
ctx.stroke();
ctx.lineCap = "round";
ctx.lineJoin = 'round';
// ctx.translate(canvas.width / 2, canvas.height / 2);
const plotter = new CanvasPlotter(ctx);
const text = 'jeyjmcNV';
const size = 100;
const lineWidth = 20;
const bold = false;
const italic = false;
const pos = { x: canvas.width / 2, y: canvas.height / 2 };
const vjustify = TextVjustify.CENTER;
{
const boundingbox = font.computeStringBoundaryLimits(text, size, lineWidth, italic);
ctx.save();
ctx.translate(pos.x, pos.y);
ctx.translate(0, size / 2);
ctx.fillStyle = "rgba(255, 0, 0, 0.3)";
ctx.fillRect(0, 0, boundingbox.width, -boundingbox.height);
ctx.fillStyle = "rgba(0, 0, 255, 0.3)";
ctx.fillRect(0, 0, boundingbox.width, boundingbox.topLimit);
ctx.fillRect(0, 0, boundingbox.width, boundingbox.bottomLimit);
{
const n = text.charCodeAt(0) - ' '.charCodeAt(0);
const glyph = font.glyphs[n];
console.log(JSON.stringify(glyph));
ctx.fillStyle = "rgba(0, 255, 0, 0.3)";
ctx.fillRect(
glyph.boundingBox.pos1.x * size,
glyph.boundingBox.pos1.y * size,
glyph.boundingBox.width * size,
glyph.boundingBox.height * size
);
ctx.restore();
}
}
font.drawText(plotter, pos, text, size, lineWidth, TextAngle.HORIZ, TextHjustify.LEFT, vjustify, italic, bold);
const out = fs.createWriteStream('text.png'), stream = canvas.pngStream();
stream.on('data', function (chunk: any) {
out.write(chunk);
});
stream.on('end', function(){
console.log('saved png');
});
}