harmonink 0.2.0 → 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.
Files changed (3) hide show
  1. package/README.md +10 -1
  2. package/package.json +5 -2
  3. package/src/live.js +20 -1
package/README.md CHANGED
@@ -47,7 +47,13 @@ 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
58
  measured one, `lineMode: 'hold'` to pin line height to a grid, or `reference`
53
59
  to harmonize the face to a reference face instead.
@@ -160,3 +166,6 @@ you are making and is not a property of the font file.
160
166
  - `measureLive` and `measure` disagree on a handful of faces whose glyphs overhang
161
167
  their advances — Pacifico among them — enough to flip a category. The five are
162
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.2.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",
@@ -28,6 +28,8 @@
28
28
  "guards": "node test/guards.mjs",
29
29
  "fuzz": "node test/fuzz.mjs",
30
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",
31
33
  "lint": "biome lint --error-on-warnings ."
32
34
  },
33
35
  "keywords": [
@@ -138,6 +140,7 @@
138
140
  "@fontsource/victor-mono": "^5.3.0",
139
141
  "@fontsource/vollkorn": "^5.3.0",
140
142
  "@fontsource/work-sans": "^5.3.0",
141
- "@fontsource/yellowtail": "^5.3.0"
143
+ "@fontsource/yellowtail": "^5.3.0",
144
+ "playwright": "1.64.1"
142
145
  }
143
146
  }
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
- const cv = canvas ?? document.createElement('canvas');
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,