53 lines
1.7 KiB
TypeScript
53 lines
1.7 KiB
TypeScript
/** Синтетический TrueType-шрифт (только таблицы, нужные писателю PDF): ASCII и кириллица, ширина 600. Не для показа. */
|
||
export function mockTtf(): Buffer {
|
||
const segs: [number, number, number][] = [
|
||
[0x20, 0x7e, 1 - 0x20],
|
||
[0x400, 0x4ff, 100 - 0x400],
|
||
[0xffff, 0xffff, 1],
|
||
];
|
||
const n = segs.length;
|
||
const sub = Buffer.alloc(16 + 8 * n);
|
||
sub.writeUInt16BE(4, 0);
|
||
sub.writeUInt16BE(sub.length, 2);
|
||
sub.writeUInt16BE(n * 2, 6);
|
||
segs.forEach(([s, e, d], i) => {
|
||
sub.writeUInt16BE(e, 14 + 2 * i);
|
||
sub.writeUInt16BE(s, 16 + 2 * n + 2 * i);
|
||
sub.writeUInt16BE(d & 0xffff, 16 + 4 * n + 2 * i);
|
||
});
|
||
const cmap = Buffer.alloc(12);
|
||
cmap.writeUInt16BE(1, 2);
|
||
cmap.writeUInt16BE(3, 4);
|
||
cmap.writeUInt16BE(1, 6);
|
||
cmap.writeUInt32BE(12, 8);
|
||
const head = Buffer.alloc(54);
|
||
head.writeUInt16BE(1000, 18);
|
||
head.writeInt16BE(1000, 42);
|
||
const hhea = Buffer.alloc(36);
|
||
hhea.writeInt16BE(800, 4);
|
||
hhea.writeInt16BE(-200, 6);
|
||
hhea.writeUInt16BE(1, 34);
|
||
const hmtx = Buffer.alloc(4);
|
||
hmtx.writeUInt16BE(600, 0);
|
||
const tables: [string, Buffer][] = [
|
||
["cmap", Buffer.concat([cmap, sub])],
|
||
["glyf", Buffer.alloc(4)],
|
||
["head", head],
|
||
["hhea", hhea],
|
||
["hmtx", hmtx],
|
||
];
|
||
const dir = Buffer.alloc(12 + 16 * tables.length);
|
||
dir.writeUInt32BE(0x00010000, 0);
|
||
dir.writeUInt16BE(tables.length, 4);
|
||
let off = dir.length;
|
||
const body: Buffer[] = [];
|
||
tables.forEach(([tag, data], i) => {
|
||
dir.write(tag, 12 + 16 * i, "latin1");
|
||
dir.writeUInt32BE(off, 12 + 16 * i + 8);
|
||
dir.writeUInt32BE(data.length, 12 + 16 * i + 12);
|
||
body.push(data);
|
||
off += data.length;
|
||
});
|
||
return Buffer.concat([dir, ...body]);
|
||
}
|