@btps/boundview 1.2.0-rc.3 → 1.2.0-rc.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/glyphs/arimo-more.json +1 -0
- package/dist/glyphs/caladea-more.json +1 -0
- package/dist/glyphs/carlito-more.json +1 -0
- package/dist/glyphs/caveat-more.json +1 -0
- package/dist/glyphs/cousine-more.json +1 -0
- package/dist/glyphs/tinos-more.json +1 -0
- package/dist/glyphs.d.ts +7 -2
- package/dist/glyphs.js +41 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +4 -3
- package/dist/render.js +15 -3
- package/dist/signature.d.ts +35 -0
- package/dist/signature.js +86 -0
- package/dist/snapshot/bind.d.ts +13 -4
- package/dist/snapshot/bind.js +334 -111
- package/dist/snapshot/decor.d.ts +70 -0
- package/dist/snapshot/decor.js +644 -0
- package/dist/snapshot/fonts.d.ts +28 -3
- package/dist/snapshot/fonts.js +23 -4
- package/dist/snapshot/index.d.ts +46 -1
- package/dist/snapshot/index.js +1268 -182
- package/dist/snapshot/layers.d.ts +32 -0
- package/dist/snapshot/layers.js +232 -0
- package/dist/snapshot/woff2.d.ts +10 -0
- package/dist/snapshot/woff2.js +391 -0
- package/dist/svg.d.ts +1 -3
- package/dist/svg.js +39 -26
- package/package.json +7 -2
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2025 Bhupendra Tamang
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*/
|
|
7
|
+
import { type Matrix } from './svgPath.js';
|
|
8
|
+
export type Flat = {
|
|
9
|
+
/** The element's transform on the page, its ancestors' included; undefined where none. */
|
|
10
|
+
matrixOf: (element: Element) => DOMMatrix | undefined;
|
|
11
|
+
/** The outermost positioned element it is in (the page's own root aside): it paints after the page's text. */
|
|
12
|
+
groupOf: (element: Element) => Element | undefined;
|
|
13
|
+
/** Puts every transform back. */
|
|
14
|
+
restore: () => void;
|
|
15
|
+
};
|
|
16
|
+
/** Sets the page's transforms aside, keeping each as a matrix about its origin, in the page's coordinates. */
|
|
17
|
+
export declare const flatten: (root: HTMLElement) => Flat;
|
|
18
|
+
/**
|
|
19
|
+
* ::before and ::after made real while the page is measured: each a span with the pseudo-element's own computed style
|
|
20
|
+
* and content, where the browser put it, the pseudo-element itself hidden. The layout is the same; its box and its
|
|
21
|
+
* text are then read like any other. Content that is strings and attr() is made; a counter or an image is left as it is.
|
|
22
|
+
*/
|
|
23
|
+
export declare const materialize: (root: HTMLElement) => (() => void);
|
|
24
|
+
export declare const isIdentity: (m: DOMMatrix | undefined) => boolean;
|
|
25
|
+
/** Upright: moved and scaled the same both ways, never turned or slanted (what a fact may be). */
|
|
26
|
+
export declare const isUpright: (m: DOMMatrix) => boolean;
|
|
27
|
+
export declare const asMatrix: (m: DOMMatrix) => Matrix;
|
|
28
|
+
/** Path data through a matrix. */
|
|
29
|
+
export declare const transformPath: (d: string, m: DOMMatrix) => string | undefined;
|
|
30
|
+
/** How much a matrix scales lengths, on average: a stroke's width through it. */
|
|
31
|
+
export declare const scaleOf: (m: DOMMatrix) => number;
|
|
32
|
+
export declare const apply: (m: DOMMatrix, x: number, y: number) => [number, number];
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2025 Bhupendra Tamang
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*/
|
|
7
|
+
/*
|
|
8
|
+
* Transforms and paint order. The page is measured with its CSS transforms
|
|
9
|
+
* set aside (a transform never moves anything else), and each element's
|
|
10
|
+
* transform kept as a matrix on the page: its shapes, images and words are
|
|
11
|
+
* then carried through that matrix, as the browser carries them. And the
|
|
12
|
+
* order the browser paints in: what is positioned (absolute, relative, fixed,
|
|
13
|
+
* sticky) paints after the page's own boxes and text, in its own order.
|
|
14
|
+
*/
|
|
15
|
+
import { normalizePath, pathData } from './svgPath.js';
|
|
16
|
+
const PROPERTIES = ['transform', 'translate', 'rotate', 'scale'];
|
|
17
|
+
const length = (value, of) => (value.endsWith('%') ? (parseFloat(value) / 100) * of : parseFloat(value) || 0);
|
|
18
|
+
/** Sets the page's transforms aside, keeping each as a matrix about its origin, in the page's coordinates. */
|
|
19
|
+
export const flatten = (root) => {
|
|
20
|
+
const found = new Map();
|
|
21
|
+
for (const element of root.querySelectorAll('*')) {
|
|
22
|
+
// An SVG's own transforms are its drawing's (its outlines carry them); its box's transform is the page's.
|
|
23
|
+
if (element.closest('svg') && element.tagName.toLowerCase() !== 'svg')
|
|
24
|
+
continue;
|
|
25
|
+
const css = getComputedStyle(element);
|
|
26
|
+
if (PROPERTIES.every((property) => css[property] === 'none' || css[property] === ''))
|
|
27
|
+
continue;
|
|
28
|
+
found.set(element, { transform: css.transform, translate: css.translate, rotate: css.rotate, scale: css.scale, origin: css.transformOrigin });
|
|
29
|
+
}
|
|
30
|
+
const saved = [];
|
|
31
|
+
for (const element of found.keys()) {
|
|
32
|
+
const style = element.style;
|
|
33
|
+
for (const property of PROPERTIES) {
|
|
34
|
+
saved.push([element, property, style.getPropertyValue(property), style.getPropertyPriority(property)]);
|
|
35
|
+
style.setProperty(property, 'none', 'important');
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const page = root.getBoundingClientRect();
|
|
39
|
+
const local = new Map();
|
|
40
|
+
for (const [element, values] of found) {
|
|
41
|
+
const rect = element.getBoundingClientRect();
|
|
42
|
+
const [ox = '0', oy = '0'] = values.origin.split(/\s+/);
|
|
43
|
+
const cx = rect.left - page.left + length(ox, rect.width);
|
|
44
|
+
const cy = rect.top - page.top + length(oy, rect.height);
|
|
45
|
+
// CSS's order: translate, rotate, scale, then transform.
|
|
46
|
+
let m = new DOMMatrix();
|
|
47
|
+
if (values.translate && values.translate !== 'none') {
|
|
48
|
+
const [tx = '0', ty = '0'] = values.translate.split(/\s+/);
|
|
49
|
+
m = m.translate(length(tx, rect.width), length(ty, rect.height));
|
|
50
|
+
}
|
|
51
|
+
if (values.rotate && values.rotate !== 'none') {
|
|
52
|
+
const parts = values.rotate.split(/\s+/);
|
|
53
|
+
const angle = parts.at(-1) ?? '0deg';
|
|
54
|
+
const degrees = angle.endsWith('turn') ? parseFloat(angle) * 360 : angle.endsWith('rad') ? (parseFloat(angle) * 180) / Math.PI : parseFloat(angle);
|
|
55
|
+
// About an axis, as `rotate: y 25deg` or `rotate: 1 1 0 30deg` give one; about z, the page's own, otherwise.
|
|
56
|
+
const named = { x: [1, 0, 0], y: [0, 1, 0], z: [0, 0, 1] };
|
|
57
|
+
const [ax, ay, az] = parts.length === 2 ? (named[parts[0]] ?? [0, 0, 1]) : parts.length === 4 ? parts.slice(0, 3).map(Number) : [0, 0, 1];
|
|
58
|
+
m = m.rotateAxisAngle(ax, ay, az, degrees);
|
|
59
|
+
}
|
|
60
|
+
if (values.scale && values.scale !== 'none') {
|
|
61
|
+
const [sx = 1, sy = sx] = values.scale.split(/\s+/).map(Number);
|
|
62
|
+
m = m.scale(sx, sy);
|
|
63
|
+
}
|
|
64
|
+
if (values.transform && values.transform !== 'none')
|
|
65
|
+
m = m.multiply(new DOMMatrix(values.transform));
|
|
66
|
+
// A 3D transform drawn flat: its 2D part, taken before the origin is placed, so its perspective is dropped rather
|
|
67
|
+
// than mixed into where and how big the element is drawn.
|
|
68
|
+
const flat = new DOMMatrix([m.a, m.b, m.c, m.d, m.e, m.f]);
|
|
69
|
+
local.set(element, new DOMMatrix().translate(cx, cy).multiply(flat).translate(-cx, -cy));
|
|
70
|
+
}
|
|
71
|
+
const matrices = new Map();
|
|
72
|
+
const matrixOf = (element) => {
|
|
73
|
+
if (element === root || !root.contains(element))
|
|
74
|
+
return undefined;
|
|
75
|
+
if (matrices.has(element))
|
|
76
|
+
return matrices.get(element);
|
|
77
|
+
const parent = element.parentElement ? matrixOf(element.parentElement) : undefined;
|
|
78
|
+
const own = local.get(element);
|
|
79
|
+
const m = own ? (parent ? parent.multiply(own) : own) : parent;
|
|
80
|
+
matrices.set(element, m);
|
|
81
|
+
return m;
|
|
82
|
+
};
|
|
83
|
+
const groups = new Map();
|
|
84
|
+
const groupOf = (element) => {
|
|
85
|
+
if (element === root || !root.contains(element))
|
|
86
|
+
return undefined;
|
|
87
|
+
if (groups.has(element))
|
|
88
|
+
return groups.get(element);
|
|
89
|
+
const outer = element.parentElement ? groupOf(element.parentElement) : undefined;
|
|
90
|
+
const group = outer ?? (getComputedStyle(element).position !== 'static' ? element : undefined);
|
|
91
|
+
groups.set(element, group);
|
|
92
|
+
return group;
|
|
93
|
+
};
|
|
94
|
+
return {
|
|
95
|
+
matrixOf,
|
|
96
|
+
groupOf,
|
|
97
|
+
restore: () => {
|
|
98
|
+
for (const [element, property, value, priority] of saved) {
|
|
99
|
+
if (value)
|
|
100
|
+
element.style.setProperty(property, value, priority);
|
|
101
|
+
else
|
|
102
|
+
element.style.removeProperty(property);
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* ::before and ::after made real while the page is measured: each a span with the pseudo-element's own computed style
|
|
109
|
+
* and content, where the browser put it, the pseudo-element itself hidden. The layout is the same; its box and its
|
|
110
|
+
* text are then read like any other. Content that is strings and attr() is made; a counter or an image is left as it is.
|
|
111
|
+
*/
|
|
112
|
+
export const materialize = (root) => {
|
|
113
|
+
const made = [];
|
|
114
|
+
const marker = 'data-bv-pseudo';
|
|
115
|
+
for (const host of [root, ...root.querySelectorAll('*')]) {
|
|
116
|
+
if (host.closest('svg') || ['img', 'input', 'textarea', 'select', 'br', 'hr'].includes(host.tagName.toLowerCase()))
|
|
117
|
+
continue;
|
|
118
|
+
for (const which of ['::before', '::after']) {
|
|
119
|
+
const css = getComputedStyle(host, which);
|
|
120
|
+
const content = css.content;
|
|
121
|
+
if (!content || content === 'none' || content === 'normal')
|
|
122
|
+
continue;
|
|
123
|
+
// "a" "b" attr(x): joined; anything else (counter(), url(), quotes) is left to the browser alone.
|
|
124
|
+
let text = '';
|
|
125
|
+
let readable = true;
|
|
126
|
+
for (const [, quoted, attr] of content.matchAll(/"((?:[^"\\]|\\.)*)"|attr\(([\w-]+)\)|\S+/g)) {
|
|
127
|
+
if (quoted !== undefined)
|
|
128
|
+
text += quoted.replace(/\\(.)/g, '$1');
|
|
129
|
+
else if (attr !== undefined)
|
|
130
|
+
text += host.getAttribute(attr) ?? '';
|
|
131
|
+
else
|
|
132
|
+
readable = false;
|
|
133
|
+
}
|
|
134
|
+
if (!readable)
|
|
135
|
+
continue;
|
|
136
|
+
const span = document.createElement('span');
|
|
137
|
+
span.setAttribute(marker, which);
|
|
138
|
+
// Every computed property, so the span is laid out and painted as the pseudo-element was.
|
|
139
|
+
let style = '';
|
|
140
|
+
for (let i = 0; i < css.length; i++) {
|
|
141
|
+
const property = css[i];
|
|
142
|
+
style += `${property}:${css.getPropertyValue(property)}!important;`;
|
|
143
|
+
}
|
|
144
|
+
span.style.cssText = style;
|
|
145
|
+
span.textContent = text;
|
|
146
|
+
host.setAttribute(`${marker}-${which.slice(2)}`, '');
|
|
147
|
+
if (which === '::before')
|
|
148
|
+
host.insertBefore(span, host.firstChild);
|
|
149
|
+
else
|
|
150
|
+
host.appendChild(span);
|
|
151
|
+
made.push({ span, host });
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
// ::first-letter: where it is styled apart from its block, the letter wrapped in a span with its style, and the
|
|
155
|
+
// pseudo-element made to take that span's style (so nothing is applied twice).
|
|
156
|
+
const letters = [];
|
|
157
|
+
// Only what the letter inherits can differ from its block without a rule of its own; a float is its own.
|
|
158
|
+
const LETTER = ['fontSize', 'fontWeight', 'fontStyle', 'fontFamily', 'color', 'textTransform', 'letterSpacing'];
|
|
159
|
+
for (const host of [root, ...root.querySelectorAll('*')]) {
|
|
160
|
+
if (host.closest('svg') || getComputedStyle(host).display.startsWith('inline'))
|
|
161
|
+
continue;
|
|
162
|
+
const own = getComputedStyle(host);
|
|
163
|
+
const letter = getComputedStyle(host, '::first-letter');
|
|
164
|
+
if (LETTER.every((key) => letter[key] === own[key]) && letter.float === 'none')
|
|
165
|
+
continue;
|
|
166
|
+
const walker = document.createTreeWalker(host, NodeFilter.SHOW_TEXT);
|
|
167
|
+
let text;
|
|
168
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
|
|
169
|
+
if (node.textContent?.trim()) {
|
|
170
|
+
text = node;
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (!text?.parentElement || text.parentElement.closest(`[${marker}]`))
|
|
175
|
+
continue;
|
|
176
|
+
const at = text.textContent?.search(/\S/) ?? 0;
|
|
177
|
+
// The letter and the punctuation before it, as ::first-letter takes them.
|
|
178
|
+
const first = /^[\p{P}]*\S/u.exec((text.textContent ?? '').slice(at))?.[0] ?? '';
|
|
179
|
+
if (!first)
|
|
180
|
+
continue;
|
|
181
|
+
const rest = text.splitText(at);
|
|
182
|
+
rest.splitText(first.length);
|
|
183
|
+
const span = document.createElement('span');
|
|
184
|
+
span.setAttribute(`${marker}-letter`, '');
|
|
185
|
+
let style = '';
|
|
186
|
+
for (let i = 0; i < letter.length; i++) {
|
|
187
|
+
const property = letter[i];
|
|
188
|
+
style += `${property}:${letter.getPropertyValue(property)}!important;`;
|
|
189
|
+
}
|
|
190
|
+
span.style.cssText = style;
|
|
191
|
+
rest.replaceWith(span);
|
|
192
|
+
span.appendChild(rest);
|
|
193
|
+
host.setAttribute(`${marker}-first`, '');
|
|
194
|
+
letters.push({ span, text, host });
|
|
195
|
+
}
|
|
196
|
+
if (made.length === 0 && letters.length === 0)
|
|
197
|
+
return () => undefined;
|
|
198
|
+
const sheet = document.createElement('style');
|
|
199
|
+
sheet.textContent =
|
|
200
|
+
`[${marker}-before]::before,[${marker}-after]::after{content:none!important;display:none!important}` +
|
|
201
|
+
`[${marker}-first]::first-letter{font:inherit!important;color:inherit!important;float:none!important;line-height:inherit!important;` +
|
|
202
|
+
`vertical-align:inherit!important;background:transparent!important;text-transform:inherit!important;letter-spacing:inherit!important;` +
|
|
203
|
+
`margin:0!important;padding:0!important;border:0!important}`;
|
|
204
|
+
document.head.appendChild(sheet);
|
|
205
|
+
return () => {
|
|
206
|
+
for (const { span, host } of made) {
|
|
207
|
+
span.remove();
|
|
208
|
+
host.removeAttribute(`${marker}-before`);
|
|
209
|
+
host.removeAttribute(`${marker}-after`);
|
|
210
|
+
}
|
|
211
|
+
for (const { span, text, host } of letters) {
|
|
212
|
+
// The letter back in its text, as it was.
|
|
213
|
+
const letterText = span.firstChild;
|
|
214
|
+
span.replaceWith(...(letterText ? [letterText] : []));
|
|
215
|
+
host.removeAttribute(`${marker}-first`);
|
|
216
|
+
text.parentElement?.normalize();
|
|
217
|
+
}
|
|
218
|
+
sheet.remove();
|
|
219
|
+
};
|
|
220
|
+
};
|
|
221
|
+
export const isIdentity = (m) => !m || (Math.abs(m.a - 1) < 1e-6 && Math.abs(m.b) < 1e-6 && Math.abs(m.c) < 1e-6 && Math.abs(m.d - 1) < 1e-6 && Math.abs(m.e) < 1e-3 && Math.abs(m.f) < 1e-3);
|
|
222
|
+
/** Upright: moved and scaled the same both ways, never turned or slanted (what a fact may be). */
|
|
223
|
+
export const isUpright = (m) => Math.abs(m.b) < 1e-6 && Math.abs(m.c) < 1e-6 && m.a > 0 && Math.abs(m.a - m.d) < 1e-6;
|
|
224
|
+
export const asMatrix = (m) => [m.a, m.b, m.c, m.d, m.e, m.f];
|
|
225
|
+
/** Path data through a matrix. */
|
|
226
|
+
export const transformPath = (d, m) => {
|
|
227
|
+
const commands = normalizePath(d);
|
|
228
|
+
return commands ? pathData(commands, asMatrix(m)) : undefined;
|
|
229
|
+
};
|
|
230
|
+
/** How much a matrix scales lengths, on average: a stroke's width through it. */
|
|
231
|
+
export const scaleOf = (m) => Math.sqrt(Math.abs(m.a * m.d - m.b * m.c));
|
|
232
|
+
export const apply = (m, x, y) => [m.a * x + m.c * y + m.e, m.b * x + m.d * y + m.f];
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2025 Bhupendra Tamang
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*/
|
|
7
|
+
/** Whether `bytes` is a WOFF2 font file. */
|
|
8
|
+
export declare const isWoff2: (bytes: ArrayBuffer) => boolean;
|
|
9
|
+
/** The OpenType font a WOFF2 file holds. `decompress` is Brotli's. */
|
|
10
|
+
export declare const woff2ToSfnt: (file: ArrayBuffer, decompress: (data: Uint8Array) => Uint8Array) => ArrayBuffer;
|
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) 2025 Bhupendra Tamang
|
|
4
|
+
* Licensed under the Apache License, Version 2.0
|
|
5
|
+
* https://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*/
|
|
7
|
+
/*
|
|
8
|
+
* A WOFF2 font file as the OpenType font it compresses (W3C WOFF2, section 5),
|
|
9
|
+
* so a page that serves its fonts as woff2, as most do, has them read like any
|
|
10
|
+
* other: its tables Brotli-decompressed, and its glyf, loca and hmtx rebuilt
|
|
11
|
+
* from their transformed forms. Font collections are not read.
|
|
12
|
+
*/
|
|
13
|
+
const KNOWN_TAGS = [
|
|
14
|
+
'cmap', 'head', 'hhea', 'hmtx', 'maxp', 'name', 'OS/2', 'post', 'cvt ', 'fpgm', 'glyf', 'loca', 'prep', 'CFF ', 'VORG', 'EBDT',
|
|
15
|
+
'EBLC', 'gasp', 'hdmx', 'kern', 'LTSH', 'PCLT', 'VDMX', 'vhea', 'vmtx', 'BASE', 'GDEF', 'GPOS', 'GSUB', 'EBSC', 'JSTF', 'MATH',
|
|
16
|
+
'CBDT', 'CBLC', 'COLR', 'CPAL', 'SVG ', 'sbix', 'acnt', 'avar', 'bdat', 'bloc', 'bsln', 'cvar', 'fdsc', 'feat', 'fmtx', 'fvar',
|
|
17
|
+
'gvar', 'hsty', 'just', 'lcar', 'mort', 'morx', 'opbd', 'prop', 'trak', 'Zapf', 'Silf', 'Glat', 'Gloc', 'Feat', 'Sill',
|
|
18
|
+
];
|
|
19
|
+
/** Whether `bytes` is a WOFF2 font file. */
|
|
20
|
+
export const isWoff2 = (bytes) => bytes.byteLength >= 4 && new DataView(bytes).getUint32(0) === 0x774f4632; // 'wOF2'
|
|
21
|
+
class Reader {
|
|
22
|
+
view;
|
|
23
|
+
at = 0;
|
|
24
|
+
constructor(view) {
|
|
25
|
+
this.view = view;
|
|
26
|
+
}
|
|
27
|
+
u8() {
|
|
28
|
+
return this.view.getUint8(this.at++);
|
|
29
|
+
}
|
|
30
|
+
u16() {
|
|
31
|
+
const value = this.view.getUint16(this.at);
|
|
32
|
+
this.at += 2;
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
i16() {
|
|
36
|
+
const value = this.view.getInt16(this.at);
|
|
37
|
+
this.at += 2;
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
u32() {
|
|
41
|
+
const value = this.view.getUint32(this.at);
|
|
42
|
+
this.at += 4;
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
base128() {
|
|
46
|
+
let value = 0;
|
|
47
|
+
for (let i = 0; i < 5; i++) {
|
|
48
|
+
const byte = this.u8();
|
|
49
|
+
if (i === 0 && byte === 0x80)
|
|
50
|
+
throw new Error('WOFF2: a UIntBase128 with a leading zero');
|
|
51
|
+
if (value & 0xfe000000)
|
|
52
|
+
throw new Error('WOFF2: a UIntBase128 too large');
|
|
53
|
+
value = (value << 7) | (byte & 0x7f);
|
|
54
|
+
if ((byte & 0x80) === 0)
|
|
55
|
+
return value >>> 0;
|
|
56
|
+
}
|
|
57
|
+
throw new Error('WOFF2: a UIntBase128 longer than 5 bytes');
|
|
58
|
+
}
|
|
59
|
+
u255() {
|
|
60
|
+
const code = this.u8();
|
|
61
|
+
if (code === 253)
|
|
62
|
+
return this.u16();
|
|
63
|
+
if (code === 255)
|
|
64
|
+
return this.u8() + 253;
|
|
65
|
+
if (code === 254)
|
|
66
|
+
return this.u8() + 506;
|
|
67
|
+
return code;
|
|
68
|
+
}
|
|
69
|
+
bytes(length) {
|
|
70
|
+
const out = new Uint8Array(this.view.buffer, this.view.byteOffset + this.at, length);
|
|
71
|
+
this.at += length;
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
const sub = (bytes, from, length) => {
|
|
76
|
+
if (from + length > bytes.length)
|
|
77
|
+
throw new Error('WOFF2: a stream runs past its table');
|
|
78
|
+
return new Reader(new DataView(bytes.buffer, bytes.byteOffset + from, length));
|
|
79
|
+
};
|
|
80
|
+
/** A growing byte buffer. */
|
|
81
|
+
class Writer {
|
|
82
|
+
bytes = new Uint8Array(1024);
|
|
83
|
+
length = 0;
|
|
84
|
+
room(extra) {
|
|
85
|
+
if (this.length + extra <= this.bytes.length)
|
|
86
|
+
return;
|
|
87
|
+
const next = new Uint8Array(Math.max(this.bytes.length * 2, this.length + extra));
|
|
88
|
+
next.set(this.bytes.subarray(0, this.length));
|
|
89
|
+
this.bytes = next;
|
|
90
|
+
}
|
|
91
|
+
u8(value) {
|
|
92
|
+
this.room(1);
|
|
93
|
+
this.bytes[this.length++] = value & 0xff;
|
|
94
|
+
}
|
|
95
|
+
u16(value) {
|
|
96
|
+
this.u8(value >> 8);
|
|
97
|
+
this.u8(value);
|
|
98
|
+
}
|
|
99
|
+
u32(value) {
|
|
100
|
+
this.u16(value >>> 16);
|
|
101
|
+
this.u16(value);
|
|
102
|
+
}
|
|
103
|
+
raw(data) {
|
|
104
|
+
this.room(data.length);
|
|
105
|
+
this.bytes.set(data, this.length);
|
|
106
|
+
this.length += data.length;
|
|
107
|
+
}
|
|
108
|
+
pad4() {
|
|
109
|
+
while (this.length % 4 !== 0)
|
|
110
|
+
this.u8(0);
|
|
111
|
+
}
|
|
112
|
+
done() {
|
|
113
|
+
return this.bytes.slice(0, this.length);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const withSign = (flag, value) => (flag & 1 ? value : -value);
|
|
117
|
+
/** One point's coordinates from the glyph stream, by its flag's triplet encoding. */
|
|
118
|
+
const triplet = (flag, glyphs) => {
|
|
119
|
+
if (flag < 10)
|
|
120
|
+
return [0, withSign(flag, ((flag & 14) << 7) + glyphs.u8())];
|
|
121
|
+
if (flag < 20)
|
|
122
|
+
return [withSign(flag, (((flag - 10) & 14) << 7) + glyphs.u8()), 0];
|
|
123
|
+
if (flag < 84) {
|
|
124
|
+
const b0 = flag - 20;
|
|
125
|
+
const b1 = glyphs.u8();
|
|
126
|
+
return [withSign(flag, 1 + (b0 & 0x30) + (b1 >> 4)), withSign(flag >> 1, 1 + ((b0 & 0x0c) << 2) + (b1 & 0x0f))];
|
|
127
|
+
}
|
|
128
|
+
if (flag < 120) {
|
|
129
|
+
const b0 = flag - 84;
|
|
130
|
+
const dx = withSign(flag, 1 + (Math.floor(b0 / 12) << 8) + glyphs.u8());
|
|
131
|
+
return [dx, withSign(flag >> 1, 1 + (((b0 % 12) >> 2) << 8) + glyphs.u8())];
|
|
132
|
+
}
|
|
133
|
+
if (flag < 124) {
|
|
134
|
+
const b0 = glyphs.u8();
|
|
135
|
+
const b1 = glyphs.u8();
|
|
136
|
+
const b2 = glyphs.u8();
|
|
137
|
+
return [withSign(flag, (b0 << 4) + (b1 >> 4)), withSign(flag >> 1, ((b1 & 0x0f) << 8) + b2)];
|
|
138
|
+
}
|
|
139
|
+
const dx = glyphs.u16();
|
|
140
|
+
return [withSign(flag, dx), withSign(flag >> 1, glyphs.u16())];
|
|
141
|
+
};
|
|
142
|
+
/** A simple glyph in the glyf table's own encoding. */
|
|
143
|
+
const simpleGlyph = (ends, points, bbox, instructions, overlap) => {
|
|
144
|
+
const out = new Writer();
|
|
145
|
+
out.u16(ends.length);
|
|
146
|
+
for (const value of bbox)
|
|
147
|
+
out.u16(value);
|
|
148
|
+
for (const end of ends)
|
|
149
|
+
out.u16(end);
|
|
150
|
+
out.u16(instructions.length);
|
|
151
|
+
out.raw(instructions);
|
|
152
|
+
const xs = new Writer();
|
|
153
|
+
const ys = new Writer();
|
|
154
|
+
let [x, y] = [0, 0];
|
|
155
|
+
points.forEach((point, i) => {
|
|
156
|
+
let flag = point.on ? 0x01 : 0;
|
|
157
|
+
if (i === 0 && overlap)
|
|
158
|
+
flag |= 0x40;
|
|
159
|
+
const dx = point.x - x;
|
|
160
|
+
const dy = point.y - y;
|
|
161
|
+
if (dx === 0)
|
|
162
|
+
flag |= 0x10;
|
|
163
|
+
else if (dx >= -255 && dx <= 255) {
|
|
164
|
+
flag |= 0x02 | (dx > 0 ? 0x10 : 0);
|
|
165
|
+
xs.u8(Math.abs(dx));
|
|
166
|
+
}
|
|
167
|
+
else
|
|
168
|
+
xs.u16(dx);
|
|
169
|
+
if (dy === 0)
|
|
170
|
+
flag |= 0x20;
|
|
171
|
+
else if (dy >= -255 && dy <= 255) {
|
|
172
|
+
flag |= 0x04 | (dy > 0 ? 0x20 : 0);
|
|
173
|
+
ys.u8(Math.abs(dy));
|
|
174
|
+
}
|
|
175
|
+
else
|
|
176
|
+
ys.u16(dy);
|
|
177
|
+
out.u8(flag);
|
|
178
|
+
[x, y] = [point.x, point.y];
|
|
179
|
+
});
|
|
180
|
+
out.raw(xs.done());
|
|
181
|
+
out.raw(ys.done());
|
|
182
|
+
return out.done();
|
|
183
|
+
};
|
|
184
|
+
/** The glyf and loca tables from their WOFF2 transform (section 5.1), and each glyph's xMin for hmtx. */
|
|
185
|
+
const rebuildGlyf = (data) => {
|
|
186
|
+
const head = sub(data, 0, 36);
|
|
187
|
+
head.u16(); // version
|
|
188
|
+
const options = head.u16();
|
|
189
|
+
const numGlyphs = head.u16();
|
|
190
|
+
const indexFormat = head.u16();
|
|
191
|
+
const sizes = [head.u32(), head.u32(), head.u32(), head.u32(), head.u32(), head.u32(), head.u32()];
|
|
192
|
+
let offset = 36;
|
|
193
|
+
const streams = sizes.map((size) => {
|
|
194
|
+
const stream = sub(data, offset, size);
|
|
195
|
+
offset += size;
|
|
196
|
+
return stream;
|
|
197
|
+
});
|
|
198
|
+
const [contours, pointCounts, flags, glyphs, composites, bboxes, instructionStream] = streams;
|
|
199
|
+
const overlapBits = options & 1 ? sub(data, offset, (numGlyphs + 7) >> 3).bytes((numGlyphs + 7) >> 3) : undefined;
|
|
200
|
+
const bitmapLength = ((numGlyphs + 31) >> 5) << 2;
|
|
201
|
+
const bboxBitmap = bboxes.bytes(bitmapLength);
|
|
202
|
+
const hasBbox = (i) => ((bboxBitmap[i >> 3] ?? 0) & (0x80 >> (i & 7))) !== 0;
|
|
203
|
+
const glyf = new Writer();
|
|
204
|
+
const loca = [];
|
|
205
|
+
const xMins = [];
|
|
206
|
+
for (let i = 0; i < numGlyphs; i++) {
|
|
207
|
+
loca.push(glyf.length);
|
|
208
|
+
const count = contours.i16();
|
|
209
|
+
if (count === 0) {
|
|
210
|
+
xMins.push(0);
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
if (count === -1) {
|
|
214
|
+
// A composite: its components as they are, its bounding box always given.
|
|
215
|
+
const start = composites.at;
|
|
216
|
+
let more = true;
|
|
217
|
+
let instructed = false;
|
|
218
|
+
while (more) {
|
|
219
|
+
const flag = composites.u16();
|
|
220
|
+
composites.u16();
|
|
221
|
+
composites.at += flag & 0x0001 ? 4 : 2;
|
|
222
|
+
composites.at += flag & 0x0008 ? 2 : flag & 0x0040 ? 4 : flag & 0x0080 ? 8 : 0;
|
|
223
|
+
instructed ||= (flag & 0x0100) !== 0;
|
|
224
|
+
more = (flag & 0x0020) !== 0;
|
|
225
|
+
}
|
|
226
|
+
const components = new Uint8Array(composites.view.buffer, composites.view.byteOffset + start, composites.at - start);
|
|
227
|
+
if (!hasBbox(i))
|
|
228
|
+
throw new Error('WOFF2: a composite glyph without its bounding box');
|
|
229
|
+
const box = [bboxes.i16(), bboxes.i16(), bboxes.i16(), bboxes.i16()];
|
|
230
|
+
glyf.u16(0xffff);
|
|
231
|
+
for (const value of box)
|
|
232
|
+
glyf.u16(value);
|
|
233
|
+
glyf.raw(components);
|
|
234
|
+
if (instructed) {
|
|
235
|
+
const length = glyphs.u255();
|
|
236
|
+
glyf.u16(length);
|
|
237
|
+
glyf.raw(instructionStream.bytes(length));
|
|
238
|
+
}
|
|
239
|
+
glyf.pad4();
|
|
240
|
+
xMins.push(box[0]);
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
const ends = [];
|
|
244
|
+
let total = 0;
|
|
245
|
+
for (let c = 0; c < count; c++) {
|
|
246
|
+
total += pointCounts.u255();
|
|
247
|
+
ends.push(total - 1);
|
|
248
|
+
}
|
|
249
|
+
const points = [];
|
|
250
|
+
let [x, y] = [0, 0];
|
|
251
|
+
for (let p = 0; p < total; p++) {
|
|
252
|
+
const flag = flags.u8();
|
|
253
|
+
const [dx, dy] = triplet(flag & 0x7f, glyphs);
|
|
254
|
+
x += dx;
|
|
255
|
+
y += dy;
|
|
256
|
+
points.push({ x, y, on: (flag & 0x80) === 0 });
|
|
257
|
+
}
|
|
258
|
+
const instructionLength = glyphs.u255();
|
|
259
|
+
const instructions = instructionStream.bytes(instructionLength);
|
|
260
|
+
let box;
|
|
261
|
+
if (hasBbox(i))
|
|
262
|
+
box = [bboxes.i16(), bboxes.i16(), bboxes.i16(), bboxes.i16()];
|
|
263
|
+
else {
|
|
264
|
+
const px = points.map((point) => point.x);
|
|
265
|
+
const py = points.map((point) => point.y);
|
|
266
|
+
box = [Math.min(...px), Math.min(...py), Math.max(...px), Math.max(...py)];
|
|
267
|
+
}
|
|
268
|
+
const overlap = overlapBits !== undefined && ((overlapBits[i >> 3] ?? 0) & (0x80 >> (i & 7))) !== 0;
|
|
269
|
+
glyf.raw(simpleGlyph(ends, points, box, instructions, overlap));
|
|
270
|
+
glyf.pad4();
|
|
271
|
+
xMins.push(box[0]);
|
|
272
|
+
}
|
|
273
|
+
loca.push(glyf.length);
|
|
274
|
+
const locaOut = new Writer();
|
|
275
|
+
for (const at of loca) {
|
|
276
|
+
if (indexFormat === 0)
|
|
277
|
+
locaOut.u16(at >> 1);
|
|
278
|
+
else
|
|
279
|
+
locaOut.u32(at);
|
|
280
|
+
}
|
|
281
|
+
return { glyf: glyf.done(), loca: locaOut.done(), xMins };
|
|
282
|
+
};
|
|
283
|
+
/** The hmtx table from its WOFF2 transform (section 5.4): left side bearings left out are each glyph's xMin. */
|
|
284
|
+
const rebuildHmtx = (data, numGlyphs, numberOfHMetrics, xMins) => {
|
|
285
|
+
const reader = sub(data, 0, data.length);
|
|
286
|
+
const flags = reader.u8();
|
|
287
|
+
const advances = Array.from({ length: numberOfHMetrics }, () => reader.u16());
|
|
288
|
+
const proportional = Array.from({ length: numberOfHMetrics }, (_, i) => (flags & 1 ? (xMins[i] ?? 0) : reader.i16()));
|
|
289
|
+
const monospaced = Array.from({ length: numGlyphs - numberOfHMetrics }, (_, i) => flags & 2 ? (xMins[numberOfHMetrics + i] ?? 0) : reader.i16());
|
|
290
|
+
const out = new Writer();
|
|
291
|
+
advances.forEach((advance, i) => {
|
|
292
|
+
out.u16(advance);
|
|
293
|
+
out.u16(proportional[i]);
|
|
294
|
+
});
|
|
295
|
+
for (const bearing of monospaced)
|
|
296
|
+
out.u16(bearing);
|
|
297
|
+
return out.done();
|
|
298
|
+
};
|
|
299
|
+
const checksum = (bytes) => {
|
|
300
|
+
const padded = new Uint8Array((bytes.length + 3) & ~3);
|
|
301
|
+
padded.set(bytes);
|
|
302
|
+
const view = new DataView(padded.buffer);
|
|
303
|
+
let sum = 0;
|
|
304
|
+
for (let i = 0; i < padded.length; i += 4)
|
|
305
|
+
sum = (sum + view.getUint32(i)) >>> 0;
|
|
306
|
+
return sum;
|
|
307
|
+
};
|
|
308
|
+
/** The OpenType font a WOFF2 file holds. `decompress` is Brotli's. */
|
|
309
|
+
export const woff2ToSfnt = (file, decompress) => {
|
|
310
|
+
const reader = new Reader(new DataView(file));
|
|
311
|
+
if (reader.u32() !== 0x774f4632)
|
|
312
|
+
throw new Error('WOFF2: not a WOFF2 file');
|
|
313
|
+
const flavor = reader.u32();
|
|
314
|
+
if (flavor === 0x74746366)
|
|
315
|
+
throw new Error('WOFF2: a font collection is not read');
|
|
316
|
+
reader.u32(); // length
|
|
317
|
+
const numTables = reader.u16();
|
|
318
|
+
reader.u16(); // reserved
|
|
319
|
+
reader.u32(); // totalSfntSize
|
|
320
|
+
const compressedSize = reader.u32();
|
|
321
|
+
reader.at = 48;
|
|
322
|
+
const tables = [];
|
|
323
|
+
let at = 0;
|
|
324
|
+
for (let i = 0; i < numTables; i++) {
|
|
325
|
+
const flag = reader.u8();
|
|
326
|
+
const known = flag & 0x3f;
|
|
327
|
+
const tag = known === 63 ? String.fromCharCode(reader.u8(), reader.u8(), reader.u8(), reader.u8()) : KNOWN_TAGS[known];
|
|
328
|
+
if (tag === undefined)
|
|
329
|
+
throw new Error('WOFF2: an unknown table tag');
|
|
330
|
+
const version = flag >> 6;
|
|
331
|
+
const length = reader.base128();
|
|
332
|
+
const transformed = tag === 'glyf' || tag === 'loca' ? version === 0 : tag === 'hmtx' ? version === 1 : false;
|
|
333
|
+
if (version !== 0 && tag !== 'glyf' && tag !== 'loca' && tag !== 'hmtx')
|
|
334
|
+
throw new Error(`WOFF2: ${tag} has an unknown transform`);
|
|
335
|
+
const size = transformed ? reader.base128() : length;
|
|
336
|
+
tables.push({ tag, length, transformed, offset: at, size });
|
|
337
|
+
at += size;
|
|
338
|
+
}
|
|
339
|
+
const data = decompress(new Uint8Array(file, reader.at, compressedSize));
|
|
340
|
+
if (data.length < at)
|
|
341
|
+
throw new Error('WOFF2: the compressed tables are shorter than their directory');
|
|
342
|
+
const raw = (table) => data.subarray(table.offset, table.offset + table.size);
|
|
343
|
+
const named = (tag) => tables.find((table) => table.tag === tag);
|
|
344
|
+
const out = new Map();
|
|
345
|
+
for (const table of tables)
|
|
346
|
+
if (!table.transformed)
|
|
347
|
+
out.set(table.tag, raw(table));
|
|
348
|
+
const glyfTable = named('glyf');
|
|
349
|
+
let xMins = [];
|
|
350
|
+
if (glyfTable?.transformed) {
|
|
351
|
+
const rebuilt = rebuildGlyf(raw(glyfTable));
|
|
352
|
+
out.set('glyf', rebuilt.glyf);
|
|
353
|
+
out.set('loca', rebuilt.loca);
|
|
354
|
+
xMins = rebuilt.xMins;
|
|
355
|
+
}
|
|
356
|
+
const hmtx = named('hmtx');
|
|
357
|
+
if (hmtx?.transformed) {
|
|
358
|
+
const maxp = out.get('maxp');
|
|
359
|
+
const hhea = out.get('hhea');
|
|
360
|
+
if (!maxp || !hhea)
|
|
361
|
+
throw new Error('WOFF2: hmtx is transformed without maxp and hhea');
|
|
362
|
+
const numGlyphs = new DataView(maxp.buffer, maxp.byteOffset).getUint16(4);
|
|
363
|
+
const numberOfHMetrics = new DataView(hhea.buffer, hhea.byteOffset).getUint16(34);
|
|
364
|
+
out.set('hmtx', rebuildHmtx(raw(hmtx), numGlyphs, numberOfHMetrics, xMins));
|
|
365
|
+
}
|
|
366
|
+
// The font as an OpenType file: its table directory, in tag order, then each table on a 4-byte boundary.
|
|
367
|
+
const tags = [...out.keys()].sort();
|
|
368
|
+
const sfnt = new Writer();
|
|
369
|
+
const search = 2 ** Math.floor(Math.log2(tags.length));
|
|
370
|
+
sfnt.u32(flavor);
|
|
371
|
+
sfnt.u16(tags.length);
|
|
372
|
+
sfnt.u16(search * 16);
|
|
373
|
+
sfnt.u16(Math.log2(search));
|
|
374
|
+
sfnt.u16(tags.length * 16 - search * 16);
|
|
375
|
+
let offset = 12 + tags.length * 16;
|
|
376
|
+
for (const tag of tags) {
|
|
377
|
+
const bytes = out.get(tag);
|
|
378
|
+
for (const char of tag)
|
|
379
|
+
sfnt.u8(char.charCodeAt(0));
|
|
380
|
+
sfnt.u32(checksum(bytes));
|
|
381
|
+
sfnt.u32(offset);
|
|
382
|
+
sfnt.u32(bytes.length);
|
|
383
|
+
offset += (bytes.length + 3) & ~3;
|
|
384
|
+
}
|
|
385
|
+
for (const tag of tags) {
|
|
386
|
+
sfnt.raw(out.get(tag));
|
|
387
|
+
sfnt.pad4();
|
|
388
|
+
}
|
|
389
|
+
const bytes = sfnt.done();
|
|
390
|
+
return bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.length);
|
|
391
|
+
};
|
package/dist/svg.d.ts
CHANGED
|
@@ -7,13 +7,11 @@
|
|
|
7
7
|
import type { BTPBoundView } from '@btps/sdk/boundview';
|
|
8
8
|
import { type BundledFonts } from './glyphs.js';
|
|
9
9
|
type Doc = Record<string, unknown>;
|
|
10
|
-
/** A drawn signature's stroke, in units of its 1,000 by 300 canvas. */
|
|
11
|
-
export declare const SIGNATURE_STROKE = 8;
|
|
12
10
|
/**
|
|
13
11
|
* The drawing for a BoundView that passed every check, with each binding
|
|
14
12
|
* filled from `document`. Never call it on one that did not: `renderBoundView`
|
|
15
13
|
* checks first. `bundled` holds the bundled fonts it needs
|
|
16
|
-
* (`loadBundledFonts(bundledFontsFor(document, view))`); most need none.
|
|
14
|
+
* (`loadBundledFonts(bundledFontsFor(document, view), bundledCharactersFor(document, view))`); most need none.
|
|
17
15
|
*/
|
|
18
16
|
export declare const boundViewToSvg: (document: Doc, view: BTPBoundView, bundled?: BundledFonts) => string;
|
|
19
17
|
export {};
|