harmonink 0.3.1 → 0.4.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 +30 -28
- package/bin/cli.js +1 -1
- package/package.json +2 -2
- package/src/css.js +1 -6
- package/src/glyf.js +1 -1
- package/src/gvar.js +16 -12
- package/src/index.js +1 -1
- package/src/measure.js +5 -11
- package/src/normalize.js +8 -67
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# harmonink
|
|
2
2
|
|
|
3
3
|
Tiny font metric normalizer. Reads a font binary, measures it from the glyph
|
|
4
|
-
outlines, and returns the `size-adjust`, `line-height
|
|
5
|
-
|
|
4
|
+
outlines, and returns the `size-adjust`, `line-height` and `font-weight` that
|
|
5
|
+
make it render at a consistent, readable x-height.
|
|
6
6
|
|
|
7
7
|
`font-size: 16px` measures the em box. Nobody reads the em box. Two fonts set at
|
|
8
8
|
16px can differ by a third in the height that actually determines readability,
|
|
@@ -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: 123.46, lineHeight: 27,
|
|
16
|
+
// { sizeAdjust: 123.46, lineHeight: 27, fontWeight: 300, … }
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
```
|
|
@@ -32,24 +32,31 @@ Emitted CSS needs no runtime:
|
|
|
32
32
|
line-height: 1.6875; font-weight: 300 }
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
Relative units, so the class composes wherever it lands. `size-adjust`
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
Relative units, so the class composes wherever it lands. `size-adjust` is left out
|
|
36
|
+
when it comes out as 100%, since writing it claims a correction that did not happen.
|
|
37
|
+
No `letter-spacing` is written: a face keeps the spacing it was drawn with. Pass
|
|
38
38
|
`selector` to rename the class. Given more than one font, the CLI names each class
|
|
39
39
|
after its family (`.eb-garamond`) so the blocks do not overwrite each other.
|
|
40
40
|
|
|
41
41
|
## API
|
|
42
42
|
|
|
43
|
-
- `measure(buffer, { weight })` → objective metrics. TrueType outlines in `.ttf`,
|
|
43
|
+
- `measure(buffer, { weight, axes })` → objective metrics. TrueType outlines in `.ttf`,
|
|
44
44
|
`.otf`, `.woff`, `.woff2`. A variable font is measured at `weight` (400 unless
|
|
45
45
|
set), clamped to its `wght` axis; `weightMeasured` reports the weight used.
|
|
46
|
+
`axes` sets any other axis by its tag, in that axis's own units, such as
|
|
47
|
+
`{ opsz: 16, wdth: 75 }`. Each value is clamped to the axis range, an axis not
|
|
48
|
+
listed stays at its default, and a tag the font does not have is ignored.
|
|
49
|
+
A `weight` or axis value that is not a number throws when the font has that axis.
|
|
50
|
+
`axes.wght` is read only when `weight` is not given. `axesMeasured` reports the
|
|
51
|
+
value used for every axis the font has, and is `null` for a static font.
|
|
46
52
|
- `measureLive(family)` → the same shape from canvas, in the browser. No parsing,
|
|
47
53
|
no brotli. Rasterises the glyph rather than reading `actualBoundingBox*`, which
|
|
48
54
|
Chromium quantises to 1/64 em on Linux and Safari to 1/64 px; advances still come
|
|
49
55
|
from `measureText().width`, which is exact to five decimal places. Flagged
|
|
50
56
|
`approximate: true`. A variable font is measured at weight 400, or at the nearest
|
|
51
57
|
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
|
|
58
|
+
axis is read at the file's default, as `measure` reads it unless `axes` moves it,
|
|
59
|
+
in Chromium and Safari. `measureLive` takes no `axes` and returns no `axesMeasured`.
|
|
53
60
|
Firefox sets that axis from the font size, so there the result carries
|
|
54
61
|
`opticalSizeMoves: true` and its metrics are those of the top of the axis; to
|
|
55
62
|
hold it in Firefox, give the `FontFace` the file's own default in its descriptors,
|
|
@@ -76,12 +83,8 @@ Size is corrected **two-way**, with `size-adjust` clamped to 90% at the bottom
|
|
|
76
83
|
The x-height target is 0.54 with a ±0.04 deadband, so a face already between 0.50
|
|
77
84
|
and 0.58 is left as drawn.
|
|
78
85
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
and the character box — never because a face has thin strokes. Clamped ±0.025em
|
|
82
|
-
and never past the point where terminals crowd. Values under 0.003em are dropped
|
|
83
|
-
entirely — they move a full line by under 1% while still overriding whatever the
|
|
84
|
-
consumer inherited.
|
|
86
|
+
Letter spacing is left as drawn. It is part of a face's character, so harmonink
|
|
87
|
+
returns none and writes none.
|
|
85
88
|
|
|
86
89
|
Two categories are detected rather than declared. A face counts as **handwriting**
|
|
87
90
|
when its letterforms scatter — the spread of flat-topped letters plus the spread
|
|
@@ -101,25 +104,23 @@ normalize(m, { lineMode: 'hold', gridBox: 24 });
|
|
|
101
104
|
|
|
102
105
|
## Harmonizing to a reference face
|
|
103
106
|
|
|
104
|
-
Normalizing corrects every face against harmonink's own fixed targets, so
|
|
105
|
-
|
|
106
|
-
to bring a companion face to a reference face in size instead
|
|
107
|
-
spacing as drawn.
|
|
107
|
+
Normalizing corrects every face against harmonink's own fixed targets, so two faces
|
|
108
|
+
used together are each brought to those targets and not to each other. To harmonize
|
|
109
|
+
is to bring a companion face to a reference face in size instead.
|
|
108
110
|
|
|
109
111
|
```js
|
|
110
112
|
const body = measure(readFileSync('Inter.woff2')); // the reference face
|
|
111
113
|
|
|
112
114
|
normalize(measure(readFileSync('Roboto.woff2')), { reference: body });
|
|
113
|
-
// { sizeAdjust: 100,
|
|
115
|
+
// { sizeAdjust: 100, harmonized: true, referenceRefused: null, … }
|
|
114
116
|
normalize(measure(readFileSync('EBGaramond.woff2')), { reference: body });
|
|
115
|
-
// { sizeAdjust: 124.91,
|
|
117
|
+
// { sizeAdjust: 124.91, harmonized: true, … }
|
|
116
118
|
```
|
|
117
119
|
|
|
118
120
|
The reference's x-height takes the place of 0.54 wherever the size rules use it: the
|
|
119
121
|
±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
|
|
121
|
-
|
|
122
|
-
every other option still applies to it. `xTarget` is not read while harmonizing.
|
|
122
|
+
Line height and weight follow from the size as they do when normalizing. A companion
|
|
123
|
+
face can be of any measured category, and every other option still applies to it. `xTarget` is not read while harmonizing.
|
|
123
124
|
|
|
124
125
|
The reference face itself is left exactly as drawn. Write no corrections for it, and
|
|
125
126
|
do not pass it through `normalize()`.
|
|
@@ -150,8 +151,9 @@ you are making and is not a property of the font file.
|
|
|
150
151
|
## Limits
|
|
151
152
|
|
|
152
153
|
- CFF/OTF (PostScript) outlines are not supported. Throws with a clear message.
|
|
153
|
-
-
|
|
154
|
-
size, width, slant) stays at its default
|
|
154
|
+
- A variable font is moved along `wght` and along the axes named in `axes`. Any
|
|
155
|
+
other axis (optical size, width, slant) stays at its default: harmonink does not
|
|
156
|
+
choose an optical size from a font size. The `wght` axis range is reported so
|
|
155
157
|
weights resolve exactly rather than snapping to 100s.
|
|
156
158
|
- Requires Latin lowercase. Throws if `x` has no outline.
|
|
157
159
|
- A damaged or cut-short file throws `corrupt font file — ` and what was wrong
|
|
@@ -163,9 +165,9 @@ you are making and is not a property of the font file.
|
|
|
163
165
|
style word — "Figtree Light". Unambiguous mangling is cleaned; a bare trailing
|
|
164
166
|
style word is not, because "Shadows Into Light" is a real family. `familyRaw`
|
|
165
167
|
holds the untouched string.
|
|
166
|
-
- `measureLive` and `measure` disagree on
|
|
167
|
-
their advances
|
|
168
|
-
listed in `test/live-verify.mjs`.
|
|
168
|
+
- `measureLive` and `measure` disagree on three faces. Pacifico's glyphs overhang
|
|
169
|
+
their advances, enough to flip its category, and two more faces come out a pixel
|
|
170
|
+
apart in line height. The three are listed in `test/live-verify.mjs`.
|
|
169
171
|
- In Firefox on macOS, `measureLive` reads Google Sans's optical-size file with an
|
|
170
172
|
x-height of 0.525 where the file and every other browser give 0.51. The result is
|
|
171
173
|
not marked `opticalSizeMoves`, and `variationSettings` does not change it.
|
package/bin/cli.js
CHANGED
|
@@ -20,7 +20,7 @@ for (let i = 0; i < argv.length; i++) {
|
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
if (flags.help || !files.length) {
|
|
23
|
-
console.log(`harmonink — normalize a font's rendered size, leading and
|
|
23
|
+
console.log(`harmonink — normalize a font's rendered size, leading and weight
|
|
24
24
|
|
|
25
25
|
npx harmonink <font...> [-o out.css] emit CSS for one or more fonts
|
|
26
26
|
npx harmonink <font...> --json emit measurements and rules as JSON
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "harmonink",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.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",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"released": "node test/released.mjs",
|
|
34
34
|
"browser": "node test/live-verify.mjs && node test/live-guards.mjs",
|
|
35
35
|
"browser:linux": "docker run --rm --ipc=host -v \"$PWD\":\"$PWD\":ro -w \"$PWD\" mcr.microsoft.com/playwright:v1.64.1-noble npm run browser",
|
|
36
|
-
"lint": "biome lint --error-on-warnings ."
|
|
36
|
+
"lint": "biome lint --error-on-warnings . && node test/comments.mjs"
|
|
37
37
|
},
|
|
38
38
|
"keywords": [
|
|
39
39
|
"font",
|
package/src/css.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
//
|
|
2
|
-
// height is the ratio that yields the measured px at the inherited size.
|
|
1
|
+
// The rules as CSS, in relative units.
|
|
3
2
|
export function toCSS(rules, opts = {}) {
|
|
4
3
|
const family = opts.family || rules.family;
|
|
5
4
|
const src = opts.src;
|
|
@@ -8,7 +7,6 @@ export function toCSS(rules, opts = {}) {
|
|
|
8
7
|
|
|
9
8
|
const leading = trim(rules.lineHeight / rules.fontSize);
|
|
10
9
|
const adjusted = rules.sizeAdjust !== 100;
|
|
11
|
-
const tracked = rules.letterSpacing !== 0;
|
|
12
10
|
|
|
13
11
|
const face = src ? [
|
|
14
12
|
`@font-face {`,
|
|
@@ -25,9 +23,6 @@ export function toCSS(rules, opts = {}) {
|
|
|
25
23
|
` font-family: ${q};`,
|
|
26
24
|
` font-size: 1em;`,
|
|
27
25
|
` line-height: ${leading};`,
|
|
28
|
-
// like size-adjust, a zero here is the identity — writing it claims a correction
|
|
29
|
-
// that did not happen, and it would override an inherited value
|
|
30
|
-
...(tracked ? [` letter-spacing: ${rules.letterSpacing}em;`] : []),
|
|
31
26
|
` font-weight: ${rules.fontWeight};`,
|
|
32
27
|
`}`,
|
|
33
28
|
].join('\n');
|
package/src/glyf.js
CHANGED
|
@@ -142,7 +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,
|
|
145
|
+
// shortcut: no test needs this line, write one before changing this decoder
|
|
146
146
|
if (fslice.length < total) throw corrupt('a table ends before its data');
|
|
147
147
|
flagPos += total;
|
|
148
148
|
const pts = decodeTriplets(fslice, glR, total);
|
package/src/gvar.js
CHANGED
|
@@ -158,18 +158,22 @@ function applyGvar(gvar, glyphs, coords) {
|
|
|
158
158
|
return advDelta;
|
|
159
159
|
}
|
|
160
160
|
|
|
161
|
-
// Moves the glyphs of a variable font to the asked
|
|
162
|
-
|
|
163
|
-
export function instantiate(tables, glyphs, weight) {
|
|
161
|
+
// Moves the glyphs of a variable font to the value asked for each axis tag, an axis not asked for left at its default.
|
|
162
|
+
export function instantiate(tables, glyphs, asked) {
|
|
164
163
|
const axes = readAxes(tables.fvar);
|
|
164
|
+
const at = axes.map(a => {
|
|
165
|
+
const v = asked[a.tag];
|
|
166
|
+
if (v != null && !Number.isFinite(v)) throw new Error(`the value asked for the ${a.tag} axis is not a number: ${v}`);
|
|
167
|
+
return v == null || !tables.gvar ? a.def : clamp(a.min, v, a.max);
|
|
168
|
+
});
|
|
169
|
+
const coords = axes.map((a, i) => at[i] === a.def ? 0
|
|
170
|
+
: at[i] < a.def ? (at[i] - a.def) / (a.def - a.min) : (at[i] - a.def) / (a.max - a.def));
|
|
171
|
+
if (tables.avar && tables.gvar) applyAvar(tables.avar, coords);
|
|
165
172
|
const wi = axes.findIndex(a => a.tag === 'wght');
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
: w < a.def ? (w - a.def) / (a.def - a.min) : (w - a.def) / (a.max - a.def));
|
|
173
|
-
if (tables.avar) applyAvar(tables.avar, coords);
|
|
174
|
-
return { weightAxis, weightMeasured: w, advDelta: coords[wi] === 0 ? null : applyGvar(tables.gvar, glyphs, coords) };
|
|
173
|
+
return {
|
|
174
|
+
weightAxis: wi < 0 ? null : [Math.round(axes[wi].min), Math.round(axes[wi].max)],
|
|
175
|
+
weightMeasured: wi < 0 ? null : at[wi],
|
|
176
|
+
axesMeasured: Object.fromEntries(axes.map((a, i) => [a.tag, at[i]])),
|
|
177
|
+
advDelta: coords.some(Boolean) ? applyGvar(tables.gvar, glyphs, coords) : null,
|
|
178
|
+
};
|
|
175
179
|
}
|
package/src/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { measure } from './measure.js';
|
|
2
|
-
export { normalize, classify, weightFor, DEFAULTS,
|
|
2
|
+
export { normalize, classify, weightFor, DEFAULTS, CAP_MEDIAN } from './normalize.js';
|
|
3
3
|
export { toCSS } from './css.js';
|
package/src/measure.js
CHANGED
|
@@ -82,14 +82,7 @@ function parseName(t) {
|
|
|
82
82
|
return raw ? { family: cleanFamily(raw), familyRaw: raw } : null;
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
//
|
|
86
|
-
// "Nunito Sans 12pt ExtraLight 12pt" reaches font-family. An optical-size token is the only
|
|
87
|
-
// unambiguous tell: no real family is called "… 16pt", so a style word beside one is noise.
|
|
88
|
-
//
|
|
89
|
-
// A bare trailing style word is NOT strippable — "Shadows Into Light" and "Archivo Black"
|
|
90
|
-
// are real family names with the same shape as a mangled "Figtree Light". Distinguishing
|
|
91
|
-
// them needs the vendor's family list, which is the caller's to hold; familyRaw is kept so
|
|
92
|
-
// nothing is lost, and `family` can be overridden on normalize().
|
|
85
|
+
// The family name without its optical-size tokens and style words, when it has such a token.
|
|
93
86
|
function cleanFamily(raw) {
|
|
94
87
|
if (!HAS_OPTICAL.test(raw)) return raw;
|
|
95
88
|
const name = raw.replace(OPTICAL_ANY, ' ').replace(STYLE_ANY, ' ').replace(/\s{2,}/g, ' ').trim();
|
|
@@ -119,10 +112,10 @@ export function measure(buf, opts = {}) {
|
|
|
119
112
|
const raw = container === 'woff2' && tables.__t_glyf
|
|
120
113
|
? decodeWoff2Glyf(tables.glyf)
|
|
121
114
|
: parseSfntGlyf(tables.glyf, tables.loca, indexToLocFormat, numGlyphs);
|
|
122
|
-
// a variable font is measured at the asked weight
|
|
115
|
+
// a variable font is measured at the asked weight and axis values, the weight 400 unless asked
|
|
123
116
|
const vary = tables.fvar
|
|
124
|
-
? instantiate(tables, raw, opts.weight ?? 400)
|
|
125
|
-
: { weightAxis: null, weightMeasured: null, advDelta: null };
|
|
117
|
+
? instantiate(tables, raw, { ...opts.axes, wght: opts.weight ?? opts.axes?.wght ?? 400 })
|
|
118
|
+
: { weightAxis: null, weightMeasured: null, axesMeasured: null, advDelta: null };
|
|
126
119
|
const glyphs = resolveComposites(raw);
|
|
127
120
|
|
|
128
121
|
const advance = g => {
|
|
@@ -225,6 +218,7 @@ export function measure(buf, opts = {}) {
|
|
|
225
218
|
ascLetterRatio: ascTops.length ? round(Math.max(...ascTops), 3) : null,
|
|
226
219
|
weightAxis: vary.weightAxis,
|
|
227
220
|
weightMeasured: vary.weightMeasured,
|
|
221
|
+
axesMeasured: vary.axesMeasured,
|
|
228
222
|
monospace,
|
|
229
223
|
glyphsMeasured: seen,
|
|
230
224
|
};
|
package/src/normalize.js
CHANGED
|
@@ -8,9 +8,6 @@ export const DEFAULTS = {
|
|
|
8
8
|
minLeading: 5,
|
|
9
9
|
lineMode: 'follow',
|
|
10
10
|
gridBox: 24,
|
|
11
|
-
trackClamp: 0.025,
|
|
12
|
-
trackDeadband: 0.03,
|
|
13
|
-
trackFloor: 0.003,
|
|
14
11
|
bounceCutoff: 0.15,
|
|
15
12
|
capGate: 0.90,
|
|
16
13
|
capOverX: 1.39,
|
|
@@ -19,30 +16,6 @@ export const DEFAULTS = {
|
|
|
19
16
|
weightBase: 400,
|
|
20
17
|
};
|
|
21
18
|
|
|
22
|
-
// stem-band sidebearing over x-height — what letters cramped at mid-height are opened up towards
|
|
23
|
-
export const GAP_IDEAL = {
|
|
24
|
-
'sans-serif': 0.397,
|
|
25
|
-
serif: 0.483,
|
|
26
|
-
monospace: 0.603,
|
|
27
|
-
handwriting: 0.295,
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
// the point past which terminals crowd; tightening never goes below it
|
|
31
|
-
export const GAP_FLOOR = {
|
|
32
|
-
'sans-serif': 0.130,
|
|
33
|
-
serif: 0.078,
|
|
34
|
-
monospace: 0.223,
|
|
35
|
-
handwriting: -0.140,
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
// median gapBox of the faces in each category that have any air at all
|
|
39
|
-
export const BOX_IDEAL = {
|
|
40
|
-
'sans-serif': 0.1947,
|
|
41
|
-
serif: 0.1111,
|
|
42
|
-
monospace: 0.2833,
|
|
43
|
-
handwriting: 0.0797,
|
|
44
|
-
};
|
|
45
|
-
|
|
46
19
|
// median cap height, against which a face is judged a cap outlier
|
|
47
20
|
export const CAP_MEDIAN = {
|
|
48
21
|
'sans-serif': 0.705,
|
|
@@ -54,9 +27,7 @@ export const CAP_MEDIAN = {
|
|
|
54
27
|
const STEPS = [100, 200, 300, 400, 500, 600, 700, 800, 900];
|
|
55
28
|
const clamp = (lo, v, hi) => Math.min(hi, Math.max(lo, v));
|
|
56
29
|
|
|
57
|
-
// Applies the weight multiplier to a weight you picked.
|
|
58
|
-
// anywhere in its axis; anything else has to snap to a step it actually ships.
|
|
59
|
-
// Pass `steps` when you know which weights the family really has.
|
|
30
|
+
// Applies the weight multiplier to a weight you picked.
|
|
60
31
|
export function weightFor(rules, baseWeight = 400, steps) {
|
|
61
32
|
const want = baseWeight * rules.weightScale;
|
|
62
33
|
const axis = rules.weightAxis;
|
|
@@ -81,8 +52,7 @@ export function classify(m, opts = {}) {
|
|
|
81
52
|
|
|
82
53
|
const defined = o => Object.fromEntries(Object.entries(o).filter(([, v]) => v !== undefined));
|
|
83
54
|
|
|
84
|
-
// Why a face cannot be a reference face, or null when it can.
|
|
85
|
-
// and its own measured category, so nothing the caller passes for the companion changes the answer.
|
|
55
|
+
// Why a face cannot be a reference face, or null when it can.
|
|
86
56
|
function refusal(ref) {
|
|
87
57
|
// anything without an x-height was never measured: rules, say, passed where metrics belong
|
|
88
58
|
if (!(ref.xHeightRatio > 0)) throw new Error('reference must be the metrics of a face, from measure() or measureLive()');
|
|
@@ -101,8 +71,8 @@ export function normalize(m, opts = {}) {
|
|
|
101
71
|
// the reference's x-height stands in for 0.54 in the band, the cap-anchor aim and the line height
|
|
102
72
|
if (harmonized) o.xTarget = opts.reference.xHeightRatio;
|
|
103
73
|
const category = opts.category || classify(m, o);
|
|
104
|
-
if (!Object.hasOwn(
|
|
105
|
-
throw new Error(`unknown category "${category}" — use one of ${Object.keys(
|
|
74
|
+
if (!Object.hasOwn(CAP_MEDIAN, category))
|
|
75
|
+
throw new Error(`unknown category "${category}" — use one of ${Object.keys(CAP_MEDIAN).join(', ')}`);
|
|
106
76
|
const base = o.baseSize;
|
|
107
77
|
if (!(base > 0 && Number.isFinite(base))) throw new Error(`baseSize must be a positive number, got ${base}`);
|
|
108
78
|
const floorPx = base * o.sizeFloor;
|
|
@@ -114,11 +84,10 @@ export function normalize(m, opts = {}) {
|
|
|
114
84
|
|
|
115
85
|
let px = Math.max(floorPx, (base * target) / m.xHeightRatio);
|
|
116
86
|
|
|
117
|
-
// a face whose lowercase are capital forms, or whose capitals fall far below its category,
|
|
118
|
-
// is aimed at cap height instead — x-height alone leaves it looking small
|
|
87
|
+
// a face whose lowercase are capital forms, or whose capitals fall far below its category, is aimed at cap height
|
|
119
88
|
const capsForward = m.ascLetterRatio != null && m.ascLetterRatio < o.capsAscX;
|
|
120
89
|
const capOutlier = m.capHeightRatio != null
|
|
121
|
-
&& m.capHeightRatio <
|
|
90
|
+
&& m.capHeightRatio < CAP_MEDIAN[category] * o.capGate;
|
|
122
91
|
let capAnchored = false;
|
|
123
92
|
if (m.capHeightRatio && (capsForward || capOutlier)) {
|
|
124
93
|
const pxCap = Math.max(floorPx, (base * o.xTarget * o.capOverX) / m.capHeightRatio);
|
|
@@ -128,8 +97,7 @@ export function normalize(m, opts = {}) {
|
|
|
128
97
|
const scale = px / base;
|
|
129
98
|
const inkPx = px * m.inkRatio;
|
|
130
99
|
|
|
131
|
-
// follow: leading is measured, and grows with the face. hold:
|
|
132
|
-
// box the ink floor is still allowed to break, for callers who need one rhythm
|
|
100
|
+
// follow: leading is measured, and grows with the face. hold: a fixed box the ink floor may still break
|
|
133
101
|
const floorLine = inkPx + o.minLeading;
|
|
134
102
|
const wanted = o.lineMode === 'hold'
|
|
135
103
|
? o.gridBox
|
|
@@ -137,32 +105,7 @@ export function normalize(m, opts = {}) {
|
|
|
137
105
|
const lineHeight = Math.round(Math.max(wanted, floorLine));
|
|
138
106
|
const gridBroken = o.lineMode === 'hold' && lineHeight > Math.round(o.gridBox);
|
|
139
107
|
|
|
140
|
-
//
|
|
141
|
-
const idealStem = GAP_IDEAL[category];
|
|
142
|
-
const openAt = idealStem * (1 - o.trackDeadband);
|
|
143
|
-
const looser = m.gapStem < openAt ? (openAt - m.gapStem) * m.xHeightRatio : 0;
|
|
144
|
-
|
|
145
|
-
const idealBox = BOX_IDEAL[category];
|
|
146
|
-
const tightAt = idealBox == null ? null : idealBox * (1 + o.trackDeadband);
|
|
147
|
-
const tighter = tightAt != null && m.gapBox > 0 && m.gapBox > tightAt
|
|
148
|
-
? (tightAt - m.gapBox) * m.xHeightRatio : 0;
|
|
149
|
-
|
|
150
|
-
const raw = looser + tighter;
|
|
151
|
-
let letterSpacing = clamp(-o.trackClamp, raw, o.trackClamp);
|
|
152
|
-
if (letterSpacing < 0)
|
|
153
|
-
letterSpacing = Math.max(letterSpacing, -Math.max(0, (m.gapBox - GAP_FLOOR[category]) * m.xHeightRatio));
|
|
154
|
-
// Below the floor the correction cannot be seen: 0.003em moves a 78-character line by
|
|
155
|
-
// 3.7px in 519, and no single gap shifts even half a pixel at any value we emit. A
|
|
156
|
-
// declaration that small still overrides whatever the consumer inherited, so it costs
|
|
157
|
-
// them cascade control and returns nothing. trackDeadband is relative to the category
|
|
158
|
-
// ideal and works on the input; this is absolute and works on what actually ships.
|
|
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;
|
|
162
|
-
|
|
163
|
-
// a scaled-up face draws heavier at the same nominal weight, so the weight comes down
|
|
164
|
-
// in proportion. Exposed as a multiplier so it applies to whatever weight you chose,
|
|
165
|
-
// not only to 400
|
|
108
|
+
// the multiplier that brings the weight down in proportion as the face is scaled up
|
|
166
109
|
const weightScale = Number((1 / scale).toFixed(4));
|
|
167
110
|
const weightAxis = m.weightAxis ?? null;
|
|
168
111
|
const weight = weightFor({ weightScale, weightAxis }, o.weightBase);
|
|
@@ -175,7 +118,6 @@ export function normalize(m, opts = {}) {
|
|
|
175
118
|
lineHeight,
|
|
176
119
|
lineMode: o.lineMode,
|
|
177
120
|
gridBroken,
|
|
178
|
-
letterSpacing: Number(letterSpacing.toFixed(4)),
|
|
179
121
|
fontWeight: weight,
|
|
180
122
|
weightScale,
|
|
181
123
|
weightAxis,
|
|
@@ -184,7 +126,6 @@ export function normalize(m, opts = {}) {
|
|
|
184
126
|
renderedSize: Number(px.toFixed(2)),
|
|
185
127
|
leading: Number((lineHeight - inkPx).toFixed(2)),
|
|
186
128
|
capAnchored,
|
|
187
|
-
trackClamped: !harmonized && Math.abs(raw) > o.trackClamp + 1e-4,
|
|
188
129
|
harmonized,
|
|
189
130
|
referenceRefused,
|
|
190
131
|
};
|