harmonink 0.1.2 → 0.3.0
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/README.md +55 -3
- package/package.json +8 -2
- package/src/glyf.js +1 -0
- package/src/live.js +20 -1
- package/src/normalize.js +21 -1
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ import { measure, normalize, toCSS } from 'harmonink';
|
|
|
13
13
|
|
|
14
14
|
const m = measure(readFileSync('EBGaramond.woff2'));
|
|
15
15
|
const rules = normalize(m);
|
|
16
|
-
// { sizeAdjust:
|
|
16
|
+
// { sizeAdjust: 123.46, lineHeight: 27, letterSpacing: 0, fontWeight: 300, … }
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
```
|
|
@@ -47,9 +47,16 @@ after its family (`.eb-garamond`) so the blocks do not overwrite each other.
|
|
|
47
47
|
no brotli. Rasterises the glyph rather than reading `actualBoundingBox*`, which
|
|
48
48
|
Chromium quantises to 1/64 em on Linux and Safari to 1/64 px; advances still come
|
|
49
49
|
from `measureText().width`, which is exact to five decimal places. Flagged
|
|
50
|
-
`approximate: true`.
|
|
50
|
+
`approximate: true`. A variable font is measured at weight 400, or at the nearest
|
|
51
|
+
weight its `@font-face` rule declares when that leaves 400 out. An optical-size
|
|
52
|
+
axis is read at the file's default, as `measure` reads it, in Chromium and Safari.
|
|
53
|
+
Firefox sets that axis from the font size, so there the result carries
|
|
54
|
+
`opticalSizeMoves: true` and its metrics are those of the top of the axis; to
|
|
55
|
+
hold it in Firefox, give the `FontFace` the file's own default in its descriptors,
|
|
56
|
+
which for Inter is `variationSettings: '"opsz" 14'`.
|
|
51
57
|
- `normalize(metrics, opts)` → CSS rules. Pass `category` to override the
|
|
52
|
-
measured one,
|
|
58
|
+
measured one, `lineMode: 'hold'` to pin line height to a grid, or `reference`
|
|
59
|
+
to harmonize the face to a reference face instead.
|
|
53
60
|
- `classify(metrics)` → the measured category.
|
|
54
61
|
- `weightFor(rules, baseWeight, steps)` → the weight correction applied to a weight
|
|
55
62
|
you chose, not just to 400. Snaps to 100s, or to `steps` when you know which
|
|
@@ -92,6 +99,48 @@ broke for.
|
|
|
92
99
|
normalize(m, { lineMode: 'hold', gridBox: 24 });
|
|
93
100
|
```
|
|
94
101
|
|
|
102
|
+
## Harmonizing to a reference face
|
|
103
|
+
|
|
104
|
+
Normalizing corrects every face against harmonink's own fixed targets, so a readable
|
|
105
|
+
body sans is still changed: Roboto gets +0.0111em of letter spacing. To harmonize is
|
|
106
|
+
to bring a companion face to a reference face in size instead, and to leave its letter
|
|
107
|
+
spacing as drawn.
|
|
108
|
+
|
|
109
|
+
```js
|
|
110
|
+
const body = measure(readFileSync('Inter.woff2')); // the reference face
|
|
111
|
+
|
|
112
|
+
normalize(measure(readFileSync('Roboto.woff2')), { reference: body });
|
|
113
|
+
// { sizeAdjust: 100, letterSpacing: 0, harmonized: true, referenceRefused: null, … }
|
|
114
|
+
normalize(measure(readFileSync('EBGaramond.woff2')), { reference: body });
|
|
115
|
+
// { sizeAdjust: 124.91, letterSpacing: 0, harmonized: true, … }
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
The reference's x-height takes the place of 0.54 wherever the size rules use it: the
|
|
119
|
+
±0.04 band, the cap-anchor aim and the line-height formula. The 90% floor stays.
|
|
120
|
+
Line height and weight follow from the size as they do when normalizing. A harmonized
|
|
121
|
+
result never carries letter spacing. A companion face can be of any measured category, and
|
|
122
|
+
every other option still applies to it. `xTarget` is not read while harmonizing.
|
|
123
|
+
|
|
124
|
+
The reference face itself is left exactly as drawn. Write no corrections for it, and
|
|
125
|
+
do not pass it through `normalize()`.
|
|
126
|
+
|
|
127
|
+
A face qualifies as a reference when it does not measure as handwriting, its x-height
|
|
128
|
+
is inside 0.50 to 0.58, and normalizing would not size it by cap height. It is judged
|
|
129
|
+
with the defaults and its own measured category, whatever else you pass. A refused
|
|
130
|
+
reference does not throw. The call returns what it returns with no reference, with
|
|
131
|
+
`harmonized: false` and the reason in `referenceRefused`:
|
|
132
|
+
|
|
133
|
+
- `measures as handwriting`
|
|
134
|
+
- `x-height outside 0.50 to 0.58`
|
|
135
|
+
- `would be sized by cap height`
|
|
136
|
+
|
|
137
|
+
A `reference` that is not metrics does throw: rules from `normalize()`, say, passed
|
|
138
|
+
where the metrics from `measure()` belong.
|
|
139
|
+
|
|
140
|
+
harmonink refuses only what it can measure. "Display" is an editorial category, and
|
|
141
|
+
many of Google's display families measure as sans-serif, serif or monospace inside
|
|
142
|
+
the band, so keeping a display face from being the reference is the caller's job.
|
|
143
|
+
|
|
95
144
|
## What it does not do
|
|
96
145
|
|
|
97
146
|
harmonink measures type and corrects it. It does not decide whether a face is
|
|
@@ -117,3 +166,6 @@ you are making and is not a property of the font file.
|
|
|
117
166
|
- `measureLive` and `measure` disagree on a handful of faces whose glyphs overhang
|
|
118
167
|
their advances — Pacifico among them — enough to flip a category. The five are
|
|
119
168
|
listed in `test/live-verify.mjs`.
|
|
169
|
+
- In Firefox on macOS, `measureLive` reads Google Sans's optical-size file with an
|
|
170
|
+
x-height of 0.525 where the file and every other browser give 0.51. The result is
|
|
171
|
+
not marked `opticalSizeMoves`, and `variationSettings` does not change it.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "harmonink",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Tiny font metric normalizer — makes any font render at a consistent, readable x-height",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -25,6 +25,11 @@
|
|
|
25
25
|
"scripts": {
|
|
26
26
|
"test": "node test/run.js",
|
|
27
27
|
"census": "node test/census.mjs",
|
|
28
|
+
"guards": "node test/guards.mjs",
|
|
29
|
+
"fuzz": "node test/fuzz.mjs",
|
|
30
|
+
"released": "node test/released.mjs",
|
|
31
|
+
"browser": "node test/live-verify.mjs && node test/live-guards.mjs",
|
|
32
|
+
"browser:linux": "docker run --rm --ipc=host -v \"$PWD\":\"$PWD\":ro -w \"$PWD\" mcr.microsoft.com/playwright:v1.64.1-noble npm run browser",
|
|
28
33
|
"lint": "biome lint --error-on-warnings ."
|
|
29
34
|
},
|
|
30
35
|
"keywords": [
|
|
@@ -135,6 +140,7 @@
|
|
|
135
140
|
"@fontsource/victor-mono": "^5.3.0",
|
|
136
141
|
"@fontsource/vollkorn": "^5.3.0",
|
|
137
142
|
"@fontsource/work-sans": "^5.3.0",
|
|
138
|
-
"@fontsource/yellowtail": "^5.3.0"
|
|
143
|
+
"@fontsource/yellowtail": "^5.3.0",
|
|
144
|
+
"playwright": "1.64.1"
|
|
139
145
|
}
|
|
140
146
|
}
|
package/src/glyf.js
CHANGED
|
@@ -142,6 +142,7 @@ export function decodeWoff2Glyf(glyfTable) {
|
|
|
142
142
|
let total = 0;
|
|
143
143
|
for (let i = 0; i < nc; i++) { counts[i] = npR.u255(); total += counts[i]; }
|
|
144
144
|
const fslice = flags.subarray(flagPos, flagPos + total);
|
|
145
|
+
// shortcut: no test needs this line, a file for it means rebuilding a .woff2 and its directory; write one before changing this decoder
|
|
145
146
|
if (fslice.length < total) throw corrupt('a table ends before its data');
|
|
146
147
|
flagPos += total;
|
|
147
148
|
const pts = decodeTriplets(fslice, glR, total);
|
package/src/live.js
CHANGED
|
@@ -1,11 +1,29 @@
|
|
|
1
1
|
import { LOWER, ASC, DESC, FREQ, BAND, FLAT_TOP, FLAT_FOOT, ASC_LETTER } from './letters.js';
|
|
2
2
|
|
|
3
3
|
const INK = 128;
|
|
4
|
+
// how far advance per px may differ between 16px and 512px before the optical-size axis counts as moving
|
|
5
|
+
const MARGIN = 0.01;
|
|
6
|
+
// a canvas in the page inherits its font styles: all:initial resets every one but direction
|
|
7
|
+
const STYLE = 'all:initial;direction:ltr;display:none;font-optical-sizing:none;-webkit-font-smoothing:antialiased';
|
|
4
8
|
|
|
5
9
|
export function measureLive(family, { em = 512, size, canvas } = {}) {
|
|
6
10
|
const unit = size ?? em;
|
|
7
|
-
|
|
11
|
+
// no page to style a canvas in, as in a worker: the handed canvas is drawn on as it is
|
|
12
|
+
if (!globalThis.document) return read(canvas, family, unit);
|
|
13
|
+
const cv = document.createElement('canvas');
|
|
14
|
+
cv.style.cssText = STYLE;
|
|
15
|
+
document.documentElement.append(cv);
|
|
16
|
+
try { return read(cv, family, unit); } finally { cv.remove(); }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function read(cv, family, unit) {
|
|
8
20
|
let ctx = cv.getContext('2d', { willReadFrequently: true });
|
|
21
|
+
const perPx = s => { ctx.font = `${s}px "${family}"`; return ctx.measureText(LOWER).width / s; };
|
|
22
|
+
ctx.save();
|
|
23
|
+
// Chromium on Linux rounds each advance to a whole pixel at 16px unless asked for this
|
|
24
|
+
ctx.textRendering = 'geometricPrecision';
|
|
25
|
+
const opticalSizeMoves = Math.abs(perPx(16) / perPx(512) - 1) > MARGIN;
|
|
26
|
+
ctx.restore();
|
|
9
27
|
ctx.font = `${unit}px "${family}"`;
|
|
10
28
|
const probe = ctx.measureText('x');
|
|
11
29
|
const need = Math.ceil(Math.max(probe.width * 3, unit * 2.5));
|
|
@@ -130,6 +148,7 @@ export function measureLive(family, { em = 512, size, canvas } = {}) {
|
|
|
130
148
|
ascLetterRatio: ascTops.length ? round(Math.max(...ascTops), 3) : null,
|
|
131
149
|
weightAxis: null,
|
|
132
150
|
weightMeasured: null,
|
|
151
|
+
opticalSizeMoves,
|
|
133
152
|
monospace,
|
|
134
153
|
glyphsMeasured: seen,
|
|
135
154
|
approximate: true,
|
package/src/normalize.js
CHANGED
|
@@ -81,9 +81,25 @@ export function classify(m, opts = {}) {
|
|
|
81
81
|
|
|
82
82
|
const defined = o => Object.fromEntries(Object.entries(o).filter(([, v]) => v !== undefined));
|
|
83
83
|
|
|
84
|
+
// Why a face cannot be a reference face, or null when it can. It is judged with the defaults
|
|
85
|
+
// and its own measured category, so nothing the caller passes for the companion changes the answer.
|
|
86
|
+
function refusal(ref) {
|
|
87
|
+
// anything without an x-height was never measured: rules, say, passed where metrics belong
|
|
88
|
+
if (!(ref.xHeightRatio > 0)) throw new Error('reference must be the metrics of a face, from measure() or measureLive()');
|
|
89
|
+
const alone = normalize(ref);
|
|
90
|
+
if (alone.category === 'handwriting') return 'measures as handwriting';
|
|
91
|
+
const lo = DEFAULTS.xTarget - DEFAULTS.deadband, hi = DEFAULTS.xTarget + DEFAULTS.deadband;
|
|
92
|
+
if (!(ref.xHeightRatio >= lo && ref.xHeightRatio <= hi)) return 'x-height outside 0.50 to 0.58';
|
|
93
|
+
return alone.capAnchored ? 'would be sized by cap height' : null;
|
|
94
|
+
}
|
|
95
|
+
|
|
84
96
|
export function normalize(m, opts = {}) {
|
|
85
97
|
opts = defined(opts);
|
|
86
98
|
const o = { ...DEFAULTS, ...opts };
|
|
99
|
+
const referenceRefused = opts.reference ? refusal(opts.reference) : null;
|
|
100
|
+
const harmonized = !!opts.reference && !referenceRefused;
|
|
101
|
+
// the reference's x-height stands in for 0.54 in the band, the cap-anchor aim and the line height
|
|
102
|
+
if (harmonized) o.xTarget = opts.reference.xHeightRatio;
|
|
87
103
|
const category = opts.category || classify(m, o);
|
|
88
104
|
if (!Object.hasOwn(GAP_IDEAL, category))
|
|
89
105
|
throw new Error(`unknown category "${category}" — use one of ${Object.keys(GAP_IDEAL).join(', ')}`);
|
|
@@ -141,6 +157,8 @@ export function normalize(m, opts = {}) {
|
|
|
141
157
|
// them cascade control and returns nothing. trackDeadband is relative to the category
|
|
142
158
|
// ideal and works on the input; this is absolute and works on what actually ships.
|
|
143
159
|
if (Math.abs(letterSpacing) < o.trackFloor) letterSpacing = 0;
|
|
160
|
+
// spacing is part of a face's character, so harmonizing leaves it as drawn
|
|
161
|
+
if (harmonized) letterSpacing = 0;
|
|
144
162
|
|
|
145
163
|
// a scaled-up face draws heavier at the same nominal weight, so the weight comes down
|
|
146
164
|
// in proportion. Exposed as a multiplier so it applies to whatever weight you chose,
|
|
@@ -166,6 +184,8 @@ export function normalize(m, opts = {}) {
|
|
|
166
184
|
renderedSize: Number(px.toFixed(2)),
|
|
167
185
|
leading: Number((lineHeight - inkPx).toFixed(2)),
|
|
168
186
|
capAnchored,
|
|
169
|
-
trackClamped: Math.abs(raw) > o.trackClamp + 1e-4,
|
|
187
|
+
trackClamped: !harmonized && Math.abs(raw) > o.trackClamp + 1e-4,
|
|
188
|
+
harmonized,
|
|
189
|
+
referenceRefused,
|
|
170
190
|
};
|
|
171
191
|
}
|